Nokogiri installation fails -libxml2 is missing

I always worked my way around Nokogiri installation issues by following the documentation in the "Installing Nokogiri" tutorial.

But this time, even after installing all the dependencies, Nokogiri hasn't been installed. I get the following error:

libxml2 is missing.  please visit <http://nokogiri.org/tutorials/installing_nokogiri.html>

I tried installing it by specifying the libxml2 and libxslt directories:

sudo gem install nokogiri -- --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib --with-xslt-dir=/usr/

but it returned the same error.

I followed all the other related Stack Overflow articles and none helped. Does anyone have a solution?


Solution 1:

You may actually need to install both of these packages

sudo apt-get install libxslt-dev libxml2-dev

Solution 2:

First, install the dependencies:

sudo apt-get install libxslt-dev libxml2-dev

If you still receive the error, you may be missing a compiler toolchain:

sudo apt-get install build-essential

You'll get the "libxml2 is missing" error if you're missing a build toolchain (at least I ran into this issue on Debian Lenny).

The Nokogiri build test-compiles a libxml2 header file to verify that it is present, however, it doesn't differentiate between "libxml2 is missing" and "a compiler to test libxml2 is missing".

Solution 3:

In Mac OS X (Mavericks), installing the libraries with brew and setting NOKOGIRI_USE_SYSTEM_LIBRARIES=1 before installing the gem did the trick for me.

Summarising:

  • If previously installed, uninstall the gem:

    gem uninstall nokogiri
    
  • Use Homebrew to install libxml2, libxslt and libiconv:

    brew install libxml2 libxslt libiconv
    
  • Install the gem specifying the paths to the libraries to be linked against:

    NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri -- --use-system-libraries --with-iconv-dir="$(brew --prefix libiconv)" --with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config" --with-xslt-config="$(brew --prefix libxslt)/bin/xslt-config"
    

Solution 4:

I just had the same issue on Fedora 13. After a frustrating and unsuccessful search to make

gem install nokogiri

work for me, I was able to install it and get around the libxml2 error via yum.

Simply install the gem via yum instead of the gem command:

su
yum search rubygem-nokogiri   #this find the proper package name
yum install rubygem-nokogiri.i686

This helped me find the right answer for Fedora and, as I am using RVM for Ruby package management,

yum install rubygem-nokogiri

will pull in all the Ruby gems and dependencies into the system, not into my RVM environment, and in my experience that leads to a very frustrating and humbling experience.

So, taking your find of the Nokogiri yum gem you can use:

yum provides  rubygem-nokogiri

and get a list of the dependencies for rubygem-Nokogiri which showed me the libraries that were missing. After that I ran:

yum install libxml2-devel libxslt libxslt-devel

Now Nokogiri compiles in Fedora and Nokogiri installs. D'oh!, we need the headers to compile Nokogiri from the devel libraries.

Solution 5:

In Mac OS X (Mavericks) if none of these solutions work, try:

ARCHFLAGS="-arch x86_64" gem install nokogiri

or

ARCHFLAGS="-arch i386" gem install nokogiri

depending on your system's architecture.