LoginSignup
2
2

More than 5 years have passed since last update.

nokogiriのインストールでエラー(v1.7/MacOSX)

Last updated at Posted at 2017-05-08

当初の記事の誤りについて

nokogiriはv1.6以降、libxml2とlibxsltのソースコードをバンドルして、gemインストール時にコンパイルするようになりました。
v1.6以降のインストール方法については nokogiri公式サイト でOSごとに説明されています。
公式サイトではシステムライブラリを使ったインストールは非推奨となっています。
よって、当初紹介した解決方法は不適切でした。

今回のケースではnokogiriインストール時の特定エラーに対する対処法を記載します。

※ご指摘いただいたtaka-oyamaさん、ありがとうございます!

環境

  • OS: Max OS X 10.11.4
  • ruby: 2.2.5
  • bundlerを使っています

発端:nokogiri 1.7.xをbundle install

GemFileの中身

% cat GemFile                                                                                                                                        [15:44:03]
source 'https://rubygems.org'

gem 'nokogiri', '~> 1.7'

インストール開始

% bundle install                                                                                                                                     
Fetching gem metadata from https://rubygems.org/..............
Fetching version metadata from https://rubygems.org/..
Resolving dependencies...
Using mini_portile2 2.1.0
Using bundler 1.14.6
Installing nokogiri 1.7.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

~~~省略~~~
    current directory: /Users/ユーザー/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/nokogiri-1.7.2/ext/nokogiri
/Users/ユーザー/.rbenv/versions/2.2.5/bin/ruby -r ./siteconf20170510-94315-17ccuae.rb extconf.rb --use-system-libraries
--with-xml2-config=/usr/local/bin/xml2-config
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.
Using pkg-config gem version 1.1.7
checking for libxml-2.0... no
checking for libxslt... no
checking for libexslt... no
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.

~~~省略~~~

An error occurred while installing nokogiri (1.7.2), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.7.2'` succeeds before bundling.

原因

直接的には、~/.bundle/configでnokogiriに--user-system-librariesを設定していたことでした。

~/.bundle/config
BUNDLE_BUILD__NOKOGIRI: --use-system-libraries --with-xml2-config=/usr/local/bin/xml2-config --with-xslt-config=/usr/local/bin/xslt-config

--use-system-librariesをつけていたせいで、システムのlibxml2を参照しにいこうとしていました。

current directory: /Users/ユーザー/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/nokogiri-1.7.2/ext/nokogiri
/Users/ユーザー/.rbenv/versions/2.2.5/bin/ruby -r ./siteconf20170510-94315-17ccuae.rb extconf.rb --use-system-libraries

しかしシステムのlibxml2が存在しない場合にlibxml2が検知できずエラーが出たことが原因です。

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

なおった!

上記の~/.bundle/configの設定を消してからinstallしたら直りました

% bundle install
Fetching gem metadata from https://rubygems.org/.............
Fetching version metadata from https://rubygems.org/.
Resolving dependencies...
Using mini_portile2 2.1.0
Using bundler 1.14.6
Installing nokogiri 1.7.2 with native extensions
Bundle complete! 1 Gemfile dependency, 3 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

おわりに

社内でも~/.bundle/config--use-system-librariesを入れている方が何人か居たので、nokogiriのversionが1.6以上の人は見直したほうがいいかもです。

2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2