Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does installing Nokogiri on Mac OS fail with libiconv is missing?

I had the same issue. Unfortunately the "Installing Nokogiri" doesn't cover Iconv issues. Here's how I resolved the issue.

First install homebrew, it'll make your life easier. If you already have it installed, be sure to grab the latest formulae by updating like so:

brew update

Note: In OSX 10.9+ you may need to install xCode command tools to allow you to install libiconv.

xcode-select --install

then install a newer version of libiconv

brew install libiconv

then install your gem

gem install nokogiri -- --with-iconv-dir=/usr/local/Cellar/libiconv/1.14

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

@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

I was finally able to solve this problem. None of the above solutions completely fixed it for me.

I was getting this error when trying to gem install nokogiri on OSX Lion 10.7.2. First of all, this error masks the real problem by saying libiconv is missing, because you will get the same error even if nokogiri can't find libxslt or libxml2, which in my case, it couldn't.

So I followed the instructions on http://nokogiri.org/tutorials/installing_nokogiri.html under the Homebrew section (slightly modified to account for a more current version of libxml2):

brew install libxml2
brew link libxml2

# install libxslt from source
wget ftp://xmlsoft.org/libxml2/libxslt-1.1.26.tar.gz
tar -zxvf libxslt-1.1.26.tar.gz
cd libxslt-1.1.26
./configure --prefix=/usr/local/Cellar/libxslt/1.1.26 --with-libxml-prefix=/usr/local/Cellar/libxml2/2.7.8
make
sudo make install

At this point I followed the directions on the nokogiri site and tried

gem install nokogiri -- --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26

However, this still failed because when building libxslt from source, it installs the /include folder in a funky place. So you need to specify the lib and include folders separately like so:

gem install nokogiri -- --with-xslt-lib=/usr/local/Cellar/libxslt/1.1.26/lib --with-xslt-include=/usr/local/Cellar/libxslt/1.1.26/include/libxslt

This still didn't work (same libiconv error), so I tried to specify all three required libraries (libxslt, libxml2 and libiconv):

gem install nokogiri -- --with-xslt-lib=/usr/local/Cellar/libxslt/1.1.26/lib --with-xslt-include=/usr/local/Cellar/libxslt/1.1.26/include/libxslt --with-iconv-dir=/usr/local/Cellar/libiconv/1.14 --with-xml2-dir=/usr/local/Cellar/libxml2/2.7.8

Now I got a different error! It was still an error but at least it was different. The make process failed with:

in /opt/local/lib/libz.1.dylib, file was built for unsupported file format which is not the architecture being linked (x86_64) for architecture x86_64

Uhh, what? After a lot of googling, I came across this miracle post: http://www.refresherate.com/2010/01/08/fixing-ld-warning-in-usrlocalliblibz-dylib-file-is-not-of-required-architecture/

Apparently OSX Lion ships with some bad versions of the libz library (libz.dylib, libz.1.dylib, libz.1.2.4.dylib) and and they need to be replaced with the latest versions from the Xcode SDK. The article explains it better than I can so read the above link for specific instructions.

Once these were replaced, I ran

gem install nokogiri -- --with-xslt-lib=/usr/local/Cellar/libxslt/1.1.26/lib --with-xslt-include=/usr/local/Cellar/libxslt/1.1.26/include/libxslt --with-iconv-dir=/usr/local/Cellar/libiconv/1.14 --with-xml2-dir=/usr/local/Cellar/libxml2/2.7.8

again and all was well. I hope this helps someone else.