LoginSignup
3
2

More than 5 years have passed since last update.

nokogiriのインストールに失敗する問題

Posted at

nokogiriのインストールに失敗

railsでgemを追加してbundle installしたらnokogiriがインストールできないと下記のようなエラーが出ました。

--use-system-libraries
checking if the C compiler accepts  -I
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/libxml2... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.

ネットの情報だと昔からあるエラーらしくgemにpkg-config追加すると直るなど情報がありますが自分はこれだとなおりませんでした。
たぶんmacのOSをアップデートしたりXcodeのバージョンを変更したりなどなどシステム内部の変更で何か問題が起きてしまうのが原因のように思われます。
ということで一応自分のmacのバージョンも下記に記します。

mac os mojave
macOS 10.14.3 (18D109)

直し方

homebrewのリンクの問題なのかまず下記コマンドを入れます。

brew install --force libxml2
brew link libxml2

上記のlinkを入れた後に下記のwarningが出たので念の為pathを入れておく。

Warning: Refusing to link macOS-provided software: libxml2
If you need to have libxml2 first in your PATH run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.zshrc

For compilers to find libxml2 you may need to set:
  export LDFLAGS="-L/usr/local/opt/libxml2/lib"
  export CPPFLAGS="-I/usr/local/opt/libxml2/include"

For pkg-config to find libxml2 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"

上記のechoとexportしたのちにnokogiriを入れて見ました。

nokogiriのインストール

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

上記コマンドを入れたところ無事にインストールできました。

Building native extensions with: '--use-system-libraries'
This could take a while...
Successfully installed nokogiri-1.10.2
Parsing documentation for nokogiri-1.10.2
Installing ri documentation for nokogiri-1.10.2
Done installing documentation for nokogiri after 8 seconds
1 gem installed
3
2
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
2