Edited at

Mac OS X Mavericksでrailsのbundle install時にnokogiriインストールエラー

More than 3 years have passed since last update.


環境

OS Version: OS X 10.9.5 Mavericks

Ruby Version: ruby 2.1.5p273

Rails Version: Rails 4.2.0


どういう時にエラーになったか

rails new hoge

cd hoge
bundle install --path vendor/bundle

すると下記のようなエラーが出た(抜粋)

Fetching gem metadata from https://rubygems.org/.........

Using json 1.8.1
Installing i18n 0.7.0

(中略)

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

/Users/kytiken/.rbenv/versions/2.1.5/bin/ruby extconf.rb
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 packaged libraries.
checking for iconv... no
-----
libiconv is missing. Please locate mkmf.log to investigate how it is failing.
-----
*** 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/kytiken/.rbenv/versions/2.1.5/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/kytiken/hoge/vendor/bundle/ruby/2.1.0/gems/nokogiri-1.6.5 for inspection.
Results logged to /Users/kytiken/hoge/vendor/bundle/ruby/2.1.0/extensions/x86_64-darwin-13/2.1.0-static/nokogiri-1.6.5/gem_make.out
An error occurred while installing nokogiri (1.6.5), and Bundler cannot
continue.
Make sure that `gem install nokogiri -v '1.6.5'` succeeds before bundling.


対処法

1.libxml2 libxslt libiconvをインストール&リンク

brew tap homebrew/dupes

brew install libxml2 libxslt libiconv
brew link --force libxml2 libxslt libiconv

2.bundle installするときのオプションを指定

bundle config build.nokogiri --use-system-libraries --with-iconv-dir="$(brew --prefix libiconv)"

3.bundle install

bundle install --path vendor/bundle

4.成功

Installing nokogiri 1.6.5

Installing rails-dom-testing 1.0.5
Installing loofah 2.0.1
Installing rails-html-sanitizer 1.0.1
Installing actionview 4.2.0
Installing actionpack 4.2.0
Installing sprockets-rails 2.2.2
Installing actionmailer 4.2.0
Installing railties 4.2.0
Installing sass-rails 5.0.0
Installing coffee-rails 4.1.0
Installing jquery-rails 4.0.2
Installing turbolinks 2.5.3
Installing rails 4.2.0
Installing web-console 2.0.0
Your bundle is complete!
It was installed into ./vendor/bundle

以上