Nokogiri installation fails -libxml2 is missing

Ruby on-RailsRubyRubygems

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You may actually need to install both of these packages

sudo apt-get install libxslt-dev libxml2-dev

Solution 2 - Ruby on-Rails

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 - Ruby on-Rails

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 - Ruby on-Rails

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 - Ruby on-Rails

You usually need development files for building gems. Try:

sudo apt-get install libxslt-dev libxml2-dev

(I just saw that Eric suggested the same in a comment.)

Solution 6 - Ruby on-Rails

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.

Solution 7 - Ruby on-Rails

It will be:

sudo yum install -y libxml2 libxml2-devel

on RHEL servers.

Solution 8 - Ruby on-Rails

I was able to get this installed with Chocolatey, Windows 8.1 x64, and DevKit x64.

cinst libxml2
cinst libxslt
cinst libiconv

gem install nokogiri -- 
  --with-xml2-include=C:\Chocolatey\lib\libxml2.2.7.8.7\build\native\include 
  --with-xml2-lib=C:\Chocolatey\lib\libxml2.redist.2.7.8.7\build\native\bin\v110\x64\Release\dynamic\cdecl 
  --with-iconv-include=C:\Chocolatey\lib\libiconv.1.14.0.11\build\native\include 
  --with-iconv-lib=C:\Chocolatey\lib\libiconv.redist.1.14.0.11\build\native\bin\v110\x64\Release\dynamic\cdecl 
  --with-xslt-include=C:\Chocolatey\lib\libxslt.1.1.28.0\build\native\include 
  --with-xslt-lib=C:\Chocolatey\lib\libxslt.redist.1.1.28.0\build\native\bin\v110\x64\Release\dynamic

You'll have to verify the version number in the paths are correct.

You may possibly need to add Microsoft's NuGet repository:

-Source "https://go.microsoft.com/fwlink/?LinkID=230477"

Solution 9 - Ruby on-Rails

At macOS none of above/below had really worked for me until I explicitly provided XCode libxml2 path to --with-xml2-include.

gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libxml2 --use-system-libraries

Solution 10 - Ruby on-Rails

I was able to install Nokogiri 1.6.5 on Fedora 20 by doing:

export NOKOGIRI_USE_SYSTEM_LIBRARIES=true

Then running:

gem install nokogiri

Solution 11 - Ruby on-Rails

For anyone else experiencing this issue, I solved it by running this command within the project directory

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

Update: I ran into this again and tried just updating the gem which worked for me...

gem update nokogiri

Solution 12 - Ruby on-Rails

Have you tried installing libxml2? Not from rubygems, but via the standard install process for your operating system. On Ubuntu/Debian for example:

sudo apt-get install libxml2

On any recent version of OS X it should already be installed.

Solution 13 - Ruby on-Rails

Was able to install vagrant-awe by following the above post but using the command as follow:

NOKOGIRI_USE_SYSTEM_LIBRARIES=1 vagrant plugin install vagrant-aws

Solution 14 - Ruby on-Rails

On Mac OS X Yosemite my mistake was that I tried to use sudo gem install when it's a rule of thumb to not use superuser privileges when installing gems.

In my case it tried to modify the system installation of Ruby, and that's not a good idea. I installed rbenv, installed Ruby 2.2.2 thru it and set it as global, which is a term from rbenv's documentation. After that Nokogiri could install itself with a simple

gem install nokogiri

No hacks or workarounds were necessary, just a properly set environment; Nokogiri does the rest perfectly.

Solution 15 - Ruby on-Rails

For OSX users, if you've had success installing Nokogiri before, yet are getting errors installing it on, say, using a new version of Ruby that you've added and that error that includes a message like:

The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.

This could be indicative of a XCode upgrade having happened via the App Store, either manually or automatically, and you not having opened it since that time.

If so, you should be able to open XCode, agree to the new license, and then install Nokogiri successfully.

Solution 16 - Ruby on-Rails

For Windows x64:

gem inst nokogiri --pre --platform ruby

For more information check this thread: https://github.com/sparklemotion/nokogiri/issues/864

Solution 17 - Ruby on-Rails

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

Change your version with it.

Solution 18 - Ruby on-Rails

=== TLDR ===

As the instructions state, build Ruby with the latest clang compiler, or at least the same version that Rubygems will use to compile Nokogiri's native extensions libxml and libxsl.

If using RVM, building from source with the --with-ggc=clang flag was what did it for me:

rvm install 2.2 --with-gcc=clang

Replace 2.2 with whatever version you want. --with-gcc=clang ensures RVM builds from source and uses clang to do so; otherwise RVM may a pre-built binary Ruby, which is what tripped me up.

Then, install Nokogiri as normal using Bundler or Rubygems.

=== IN DEPTH ===

I struggled with this for a while. gem install nokogiri gave me:

checking for xmlParseDoc() in libxml/parser.h... no
checking for xmlParseDoc() in -lxml2... no
checking for xmlParseDoc() in -llibxml2... no

Digging into mkmf.log I saw:

conftest.c:15:27: error: too few arguments to function call, single argument 'cur' was not specified
int t(void) { xmlParseDoc(); return 0; }  

Nokogiri supplies its own libxml and libxsl (as of 1.6.4). The signature defined in Nokogiri's local copy of parser.h (found under the gem install directory) is:

xmlParseDoc (const xmlChar *cur);                                                                                                                                                                                

So I was at a loss as to how the method call in the hermetically sealed conftest.c file usage couldn't match up with the header file for parser.h.

When I realized I had probably installed a binary Ruby I removed and reinstalled using --with-gcc=clang (to force compilation and use clang) and the problem was solved:

rvm uninstall 2.2
rvm install 2.2 --with-gcc=clang
gem install nokogiri

I'm not exactly sure why that works as the system libxml header /usr/include/libxml2/libxml/parser.h has the same signature as Nokogiri's local copy.

It's weird, but it worked. Just make sure you compile a Ruby with clang.

Solution 19 - Ruby on-Rails

I was able to install Nokogiri by running the following packages:

$ dnf group install "C Development Tools and Libraries"
$ dnf install ruby-devel libxml2-devel patch

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRohit A.View Question on Stackoverflow
Solution 1 - Ruby on-RailsRyanView Answer on Stackoverflow
Solution 2 - Ruby on-RailsErik PetersonView Answer on Stackoverflow
Solution 3 - Ruby on-RailsEduardoView Answer on Stackoverflow
Solution 4 - Ruby on-RailsSojoodiView Answer on Stackoverflow
Solution 5 - Ruby on-RailspduView Answer on Stackoverflow
Solution 6 - Ruby on-RailsrillaView Answer on Stackoverflow
Solution 7 - Ruby on-RailshohnerView Answer on Stackoverflow
Solution 8 - Ruby on-RailsChloeView Answer on Stackoverflow
Solution 9 - Ruby on-RailssobstelView Answer on Stackoverflow
Solution 10 - Ruby on-RailsSimoneView Answer on Stackoverflow
Solution 11 - Ruby on-RailsJbur43View Answer on Stackoverflow
Solution 12 - Ruby on-Railsd11wtqView Answer on Stackoverflow
Solution 13 - Ruby on-RailsRomiView Answer on Stackoverflow
Solution 14 - Ruby on-RailsAleks N.View Answer on Stackoverflow
Solution 15 - Ruby on-RailsPaul FioravantiView Answer on Stackoverflow
Solution 16 - Ruby on-RailsmagiccrafterView Answer on Stackoverflow
Solution 17 - Ruby on-RailsLiberView Answer on Stackoverflow
Solution 18 - Ruby on-RailsJohnsterView Answer on Stackoverflow
Solution 19 - Ruby on-Railsuser1455180View Answer on Stackoverflow