Edited at

bundle intallでAn error occurred while installing nokogiri (1.8.4)のエラー発生

More than 1 year has passed since last update.


概要

macOSのversionはHigh Sierra 10.13.6

rails new した後、bundle installをしたら以下のエラーが発生。

指示されている様にgem install nokogiri -v '1.8.4'

を打っても解決せず同じエラーが発生する。

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

(中略)
Building nokogiri using system libraries.
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.8.4), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.8.4'` succeeds before bundling.


解決手順

brew updateした後、

brew install libxml2およびbrew link --force libxml2

(またはbrew install --force libxml2で2つまとめて行う)を打ちgem install nokogiri -- --use-system-libraries

でnokogiriをインストールする。

$brew update

(中略)
$brew install libxml2
(中略)

$brew link --force libxml2

Linking /usr/local/Cellar/libxml2/2.9.7... 21 symlinks created

If you need to have this software first in your PATH instead consider running:
echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.bash_profile

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

Building native extensions with: '--use-system-libraries'
This could take a while...
Successfully installed nokogiri-1.8.4
Parsing documentation for nokogiri-1.8.4
Done installing documentation for nokogiri after 2 seconds
1 gem installed

無事にnokogiriがインストールされたら

再びbundle installを行う。

その後、brew unlink libxml2 を行う。

$bundle install

(中略)

$brew unlink libxml2


参考

macOS Sierra での gem install nokogiri でエラー