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


$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


再びbundle installを行う。

その後、brew unlink libxml2 を行う。

$bundle install


$brew unlink libxml2


macOS Sierra での gem install nokogiri でエラー