zimathon blog

フルリモート開発エンジニア

【RubyOnRails】nokogiri をインストールしようとしてエラーが出るときの解決方法

開発環境

発生したエラー

bundle install 時に nokogiri インストール時にエラーになる

Building nokogiri using packaged libraries.

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /Users/sasajimay/.rbenv/versions/2.0.0-p353/bin/ruby extconf.rb vendor/bundle
Building nokogiri using packaged libraries.
-----
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/sasajimay/.rbenv/versions/2.0.0-p353/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


Gem files will remain installed in /Users/sasajimay/projects/ruby/rails_projects/helloworld/vendor/bundle/ruby/2.0.0/gems/nokogiri-1.6.3.1 for inspection.
Results logged to /Users/sasajimay/projects/ruby/rails_projects/helloworld/vendor/bundle/ruby/2.0.0/gems/nokogiri-1.6.3.1/ext/nokogiri/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.

解決方法

必要なライブラリをインストール

Nokogiri 鋸 インストールチュートリアルがわかりやすい

brew install

上記チュートリアルとは異なるバージョンがインストールされるから注意

brew install libxml2 libxslt
brew link libxml2 libxslt

libiconvはソースから

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
tar xvfz libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr/local/Cellar/libiconv/1.13.1
make
sudo make install

brew link libiconv

設定後bundle installでまだエラー orz

libiconv is missing.  please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.

どうもライブラリの参照がおかしいっぽい

brew install する時にバージョンを指定

以下のコマンドを実行

bundle config build.nokogiri --use-system-libraries --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 --with-iconv-include=/usr/local/Cellar/libiconv/1.13.1/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib

実行後の ~/.bundle/config

---
BUNDLE_BUILD__NOKOGIRI: --use-system-libraries --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 --with-iconv-include=/usr/local/Cellar/libiconv/1.13.1/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib

この設定でbundle installできた!!!!