LoginSignup
3
3

More than 5 years have passed since last update.

nokogiriのインストールで An error occurred while installing nokogiri (1.8.4), and Bundler cannot continue.と出た時の対処法

Last updated at Posted at 2019-02-04

こちらに沿ってrspecの勉強をしようと思い、手順にしたがって作業をしていたところ

みんな大好きnokogiriのエラーが発生しました(´・ω・)

$ bundle

ここのコマンドを叩いたら

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
(中略)
An error occurred while installing nokogiri (1.8.4), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.8.4' --source 'https://rubygems.org/'` succeeds before bundling.

とのエラーが。

あるあるなのですが、結構解決策がわからなくなってしまったりするのでメモ。

まずは公式の手法に沿ってやってみよう。

$ gem update --system
$ xcode-select --install 

$ gem install nokogiri

とある、、ふむふむ。
しかし会社のPCで色々制限があり、この方法は取れなさそうなのでパスしました。

もう少し読み進めていくとhomebrewを使っているならこれでもいけるよ、的なことが書いてありました

公式に載っている手法1

brew unlink xz
gem install nokogiri # or bundle install
brew link xz

公式に載っている手法2

brew install libxml2
# If installing directly
gem install nokogiri -- --use-system-libraries \
  --with-xml2-include=$(brew --prefix libxml2)/include/libxml2
# If using Bundle
bundle config build.nokogiri --use-system-libraries \
  --with-xml2-include=$(brew --prefix libxml2)/include/libxml2
bundle install

ほう…

ただ、gemはinstallできているようなので、


$ bundle config build.nokogiri この後ろのオプションをごにょごにょ

をすることでビルドオプションを指定してみました。

手順

  1. $(brew --prefix libxml2)でlibxml2がインストールされているファイルパスを取得。
$ echo (brew --prefix libxml2)

僕の場合は

/Users/user_name/homebrew/opt/libxml2

と返ってきました。

  1. 上記のパスを使ってビルドオプションを指定
$ bundle config build.nokogiri --with-xml2-config=/Users/user_name/homebrew/opt/libxml2/bin/xml2-config --with-xslt-config=/Users/user_name/homebrew/opt/libxslt/bin/xslt-config

長ったらしいですが

--with-xml2-config

--with-xslt-config

のオプションにパスを指定しています。

  1. bundle installする

これで正常にnokogiriがインストールされました!

ちなみにこちらに書いてあったのですが、localをつけるとglobalでなくても設定できるようです


$ bundle config --local build.nokogiri

参考になれば幸いです。

3
3
0

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
3
3