Why does installing Nokogiri on Mac OS fail with l

2020-01-23 13:25发布

I've been trying to install Nokogiri on Mac OS 10.9.3 and whatever I try, the install fails in the end with the following error message:

$ sudo gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 --with-iconv-include=/usr/local/Cellar/libiconv/1.14/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib
Building native extensions with: '--with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 --with-iconv-include=/usr/local/Cellar/libiconv/1.14/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib'
This could take a while...
Building nokogiri using packaged libraries.
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 --with-iconv-include=/usr/local/Cellar/libiconv/1.14/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib
Building nokogiri using packaged libraries.
checking for iconv.h... yes
checking for iconv_open() in iconv.h... no
checking for iconv_open() in -liconv... no
checking for libiconv_open() in iconv.h... no
checking for libiconv_open() in -liconv... no
-----
libiconv is missing.  please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.
-----
*** 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=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/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


Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.2.1 for inspection.
Results logged to /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.2.1/ext/nokogiri/gem_make.out

After following a lot of help found around the web including building and installing libxml2 and libxslt using brew and building libiconv from the sources (as described in "Installing Nokogiri"), the error remains the same.

When trying to run the installation for Nokogiri, libxml2 and libxslt seem to be found fine, but not libiconv.

Anyone with a better knowledge of these things know how to get Nokogiri installed?

30条回答
甜甜的少女心
2楼-- · 2020-01-23 13:48

You need to upgrade your homebrew to 0.9

then follow these steps

brew install libxml2 libxslt
brew link libxml2 libxslt
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
tar xvfz libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr/local/Cellar/libiconv/1.13.1
make
sudo make install

sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2

gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.0/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.0/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 --with-iconv-include=/usr/local/Cellar/libiconv/1.13.1/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib

You should double check the folder library version.

查看更多
放荡不羁爱自由
3楼-- · 2020-01-23 13:50

Try using the system libraries. OSX comes with libiconv in newer versions, but the defaults install script seems to have a problem

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

Edit: If using bundler, as mentioned by Geoff you can do:

bundle config build.nokogiri --use-system-libraries
查看更多
霸刀☆藐视天下
4楼-- · 2020-01-23 13:50

I was fighting with Nokogiri for a while today on OS X 10.10 Yosemite

My environment was messed up for some reason.

which bundle and which gem were giving me /usr/bin/bundle and /usr/bin/gem instead of ~/.rbenv/shims/gem

What helped correct it for me was sudo rm -i /usr/bin/gem /usr/bin/bundle

After that I: 1. switched back to my project directory 2. uninstalled the dependent 3. (re)-installed the dependent libs: did a brew install libxml2 libiconv libxslt 4. installed my ruby version fresh (with rbenv) 5. did gem install bundler 6. and bundle install ran without any problems.

Nokogiri was fine after that.

For reference:

╰─% cat .bundle/config           
---
BUNDLE_PATH: vendor/bundle
BUNDLE_DISABLE_SHARED_GEMS: '1'
BUNDLE_JOBS: 4

╰─% which ruby bundle gem
~/.rbenv/shims/ruby
~/.rbenv/shims/bundle
~/.rbenv/shims/gem
查看更多
Rolldiameter
5楼-- · 2020-01-23 13:50

This blog post proposes to install libiconv manually.

Subsequently, nokogiri can be installed with a number of switches telling it where to find libiconv (see the blog post).

As a side note: After installing nokogiri, I managed to install gollum (installation of which also failed because it could not find iconv). Now I am still facing problems though, because when I start up gollum, Python crashes.

查看更多
Root(大扎)
6楼-- · 2020-01-23 13:51

@Cory's solution contains the correct answer, but the solution on Mavericks is actually much simpler than the top solution so I'm reposting with only the necessary steps.

On Mavericks (OSX 10.9+):

Install Xcode Command Line Tools:

xcode-select --install

then install your gem:

gem install nokogiri
查看更多
劫难
7楼-- · 2020-01-23 13:51

Simple steps to follow before you start uninstalling and installing anything. Check if you have CLT (command line tools installed):

brew config

see the CLT version here if its installed no need for reinstallation.

This means the issue is with permission you have reinstall only bundler gem make its not sudo. Uninstall bundler with gem uninstall bundler reinstall gem bundler gem install bundler

Most important point here is the permission under which bundler gem is installed it should never be sudoed.

查看更多
登录 后发表回答