LoginSignup
81
74

More than 5 years have passed since last update.

Railsでnokogiriのインストールが出来ない

Last updated at Posted at 2014-10-13

はじめに

railsnokogiriを使ってスクレイピングをしようと思いgemをインストールしましたが上手く出来ずハマったので紹介します。

現象

nokogirigemインストールは無事出来たと思っていましたが、requierするとエラーになってしまい使えませんでした。
調べてみるとnokogiriは色々なgemが依存しているようです。homebrewからlibiconv formulaが削除されてしまってビルドに失敗するケースなどがあるそうです。
今回は色々試してみて途中に色々エラーが出ました。
ざっと紹介したいと思います。

入力コマンド
$ gem install nokogiri
出力結果
Building native extensions.  This could take a while...
Building nokogiri using packaged libraries.
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

    /Users/user/.rbenv/versions/2.0.0-p247/bin/ruby -r ./siteconf20141013-14762-qxfygt.rb extconf.rb
Building nokogiri using packaged libraries.
checking for iconv.h... yes
checking for iconv_open() in iconv.h... no
checking for iconv_open() in -liconv... no
checking for libiconv_open() in iconv.h... no
checking for libiconv_open() in -liconv... no
-----
libiconv is missing.  please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.
-----
*** 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/user/.rbenv/versions/2.0.0-p247/bin/ruby
    --help
    --clean
    --use-system-libraries
    --enable-static
    --disable-static
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --enable-cross-build
    --disable-cross-build

extconf failed, exit code 1

Gem files will remain installed in /Users/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.3.1 for inspection.
Results logged to /Users/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/extensions/x86_64-darwin-12/2.0.0-static/nokogiri-1.6.3.1/gem_make.out
入力コマンド
$ bundle install
出力結果
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Using rake 10.3.2
Using i18n 0.6.11
Using json 1.8.1
Using minitest 5.4.2
Using thread_safe 0.3.4
Using tzinfo 1.2.2
Using activesupport 4.1.4
Using builder 3.2.2
Using erubis 2.7.0
Using actionview 4.1.4
Using rack 1.5.2
Using rack-test 0.6.2
Using actionpack 4.1.4
Using mime-types 1.25.1
Using polyglot 0.3.5
Using treetop 1.4.15
Using mail 2.5.4
Using actionmailer 4.1.4
Using activemodel 4.1.4
Using arel 5.0.1.20140414130214
Using activerecord 4.1.4
Using bundler 1.6.5
Using coffee-script-source 1.8.0
Using execjs 2.2.1
Using coffee-script 2.3.0
Using thor 0.19.1
Using railties 4.1.4
Using coffee-rails 4.0.1
Using hike 1.2.3
Using multi_json 1.10.1
Using jbuilder 2.2.2
Using jquery-rails 3.1.2
Using mini_portile 0.6.0
Building nokogiri using system libraries.

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /Users/user/.rbenv/versions/2.0.0-p247/bin/ruby -r ./siteconf20141013-14847-1g83srl.rb extconf.rb --use-system-libraries
Building nokogiri using system libraries.
libxml2 version 2.6.21 or later is required!
*** 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/user/.rbenv/versions/2.0.0-p247/bin/ruby
    --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-pkg-config
    --without-pkg-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
    --with-pkg-config
    --without-pkg-config

extconf failed, exit code 1

Gem files will remain installed in /Users/user/Documents/study/rails/igr_times/vendor/bundle/ruby/2.0.0/gems/nokogiri-1.6.3.1 for inspection.
Results logged to /Users/user/Documents/study/rails/igr_times/vendor/bundle/ruby/2.0.0/extensions/x86_64-darwin-12/2.0.0-static/nokogiri-1.6.3.1/gem_make.out
An error occurred while installing nokogiri (1.6.3.1), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.6.3.1'` succeeds before bundling.
入力コマンド
gem install nokogiri -v '1.6.3.1'
出力結果
Building native extensions.  This could take a while...
Building nokogiri using packaged libraries.
Building libxml2-2.8.0 for nokogiri with the following patches applied:
    - 0001-Fix-parser-local-buffers-size-problems.patch
    - 0002-Fix-entities-local-buffers-size-problems.patch
    - 0003-Fix-an-error-in-previous-commit.patch
    - 0004-Fix-potential-out-of-bound-access.patch
    - 0005-Detect-excessive-entities-expansion-upon-replacement.patch
    - 0006-Do-not-fetch-external-parsed-entities.patch
    - 0007-Enforce-XML_PARSER_EOF-state-handling-through-the-pa.patch
    - 0008-Improve-handling-of-xmlStopParser.patch
    - 0009-Fix-a-couple-of-return-without-value.patch
    - 0010-Keep-non-significant-blanks-node-in-HTML-parser.patch
    - 0011-Do-not-fetch-external-parameter-entities.patch
************************************************************************
IMPORTANT!  Nokogiri builds and uses a packaged version of libxml2.

If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:

    gem install nokogiri -- --use-system-libraries

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

However, note that nokogiri does not necessarily support all versions
of libxml2.

For example, libxml2-2.9.0 and higher are currently known to be broken
and thus unsupported by nokogiri, due to compatibility problems and
XPath optimization bugs.
************************************************************************

では最終的に行なったことをまとめたいと思います。

必須ファイルの導入

以下のコマンドを実行して必須ファイルを導入を行います。

入力コマンド
$ brew tap homebrew/dupes                   #リポジトリの追加
$ brew install libxml2 libxslt libiconv     #必須ファイルのインストール
$ brew link --force libxml2
$ brew link --force libxslt
$ brew link --force libiconv

これでgemインストール。

入力コマンド
$ gem install nokogiri -- --use-system-libraries --with-iconv-dir="$(brew --prefix libiconv)" --with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config" --with-xslt-config="$(brew --prefix libxslt)/bin/xslt-config"

インストール完了。と思ったのですが、上手く行かず...。

いろいろ試した(失敗例?)

記事を漁っていろいろしてみました。
途中でこのようなコマンドを実行。

入力コマンド
$ NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri --no-rdoc --no-ri

その他こんなコマンドも。

入力コマンド
bundle config build.nokogiri --use-system-libraries
出力結果一部
...
Building nokogiri using system libraries.
...

結果的にインストール済みのlibxml2を使う様に指定はされているようでしたが、libxml2が無いと言われて上手く行きませんでした。

また途中bundle install時にパスを指定してbundle install --path vendor/bundleとしましたがダメでした。

最終的に(私の場合)

最終的にあと一歩、自分の場合何が行けなかったのかというと、bundle/configの中身の変更でした。
というか途中でいじりすぎたせいですねorz
おそらく上記「必須ファイルの導入」をすればインストール出来たのだと思います。

自分の場合bundle install --path vendor/bundleをしたので.bundle/configの中にBUNDLE_PATH: vendor/bundleの設定がされてました。
途中もしやと思い、この設定を削除したらbundle install出来ました。

おわりに

この解決に1日かけてしまったのでとてもショックでした。。。
でもなんだかんだ勉強になったので良かったです。
今回は以下の記事を主に参考にさせて頂きました。ありがとうございます。

Mac OS X Mavericksで”gem i nokogiri”失敗の解決方法
http://qiita.com/t_732_twit/items/a7956a170b1694f7ffc2

nokogiriのインストールでハマった件
http://heroween.hateblo.jp/entry/2014/05/25/164302

Mac OS X Mavericks で bundle install した時 nokogiri が "libiconv is missing."でインストールできなかった
http://qiita.com/niwatako/items/1fd769d679ea6cd9a476

Installing Nokogiri
http://www.nokogiri.org/tutorials/installing_nokogiri.html

81
74
1

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
81
74