しめ鯖日記

swift, iPhoneアプリ開発, ruby on rails等のTipsや入門記事書いてます

NokogiriのインストールでERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.というエラーが出た時の対策

Nokogiriをインストール中に表題のエラーが出た時の対処法です。
メッセージは下の通りです。

   current directory: /Users/xxxxxx/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/nokogiri-1.8.3/ext/nokogiri
/Users/xxxxxx/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20180619-62442-1vrm1wp.rb extconf.rb --use-system-libraries
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
pkg-config could not be used to find libxml-2.0
Please install either `pkg-config` or the pkg-config gem per

    gem install pkg-config -v "~> 1.1"

pkg-config could not be used to find libxslt
Please install either `pkg-config` or the pkg-config gem per

    gem install pkg-config -v "~> 1.1"

pkg-config could not be used to find libexslt
Please install either `pkg-config` or the pkg-config gem per

    gem install pkg-config -v "~> 1.1"

ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/xxxxxx/.rbenv/versions/2.5.1/bin/$(RUBY_BASE_NAME)
    --help
    --clean
    --use-system-libraries
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --with-xml2-dir
    --without-xml2-dir
    --with-xml2-include
    --without-xml2-include=${xml2-dir}/include
    --with-xml2-lib
    --without-xml2-lib=${xml2-dir}/lib
    --with-libxml-2.0-config
    --without-libxml-2.0-config
    --with-pkg-config
    --without-pkg-config
    --with-xslt-dir
    --without-xslt-dir
    --with-xslt-include
    --without-xslt-include=${xslt-dir}/include
    --with-xslt-lib
    --without-xslt-lib=${xslt-dir}/lib
    --with-libxslt-config
    --without-libxslt-config
    --with-exslt-dir
    --without-exslt-dir
    --with-exslt-include
    --without-exslt-include=${exslt-dir}/include
    --with-exslt-lib
    --without-exslt-lib=${exslt-dir}/lib
    --with-libexslt-config
    --without-libexslt-config

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/xxxxxx/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/nokogiri-1.8.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/xxxxxx/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/nokogiri-1.8.3 for
inspection.
Results logged to
/Users/xxxxxx/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/nokogiri-1.8.3/gem_make.out

An error occurred while installing nokogiri (1.8.3), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.8.3' --source 'https://rubygems.org/'` succeeds before bundling.

メッセージに記載の通り、pkg-configを入れたら解決しました。

gem install pkg-config

pkg-configとは何か

解決はしたのですが、気持ち悪いのでもう少し深掘りしてみます。
まずは下のエラーメッセージに出てきたpkg-configについて調べます。

ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.

pkg-configのGemはpkg-configというライブラリをRubyで実装したものになります。
リポジトリは下URLです。

github.com

pkg-configとはライブラリを使う際に必要な情報を共通したインターフェースで提供してくれるツールです。
今回のエラーではpkg-configを使ってNokogiriのビルドに必要な情報を取得しようとしたんだと思います。

pkg-configとは、ライブラリを利用する際に必要となる各種フラグやパス等を、共通したインターフェースで提供でするための手段である。
pkg-configは、環境変数PKG_CONFIG_PATHのパスに存在する *.pc ファイルに記録された情報を元に、ビルドの際に必要な文字列を返す。

pkg-config - Wikipediaより

今回の件では下のようなメッセージがあったので、おそらくpkg-configを使ってlibxml2のパスを探そうとしたけどpkg-configがないからエラーになったという事だと思います。

pkg-config could not be used to find libxml-2.0

libxml2とは何か

これは名前の通りXMLを操作するライブラリです。
末尾に付いている2はlibxml1の改良版だからです。

元々libxml1があったんですが、色々な問題があってlibxml2を作ったようです。
詳しい使い方などはまた機会あれば調べてみようと思います。

The XML C parser and toolkit of Gnome

まとめ

時々名前を聞くpkg-configですが、詳細が分かってすっきりしました。
今後もエラーなどに遭遇したら少しでも詳しく調べるようにしたいと思います。