Rohit A.
Rohit A.

Reputation: 1775

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?

Upvotes: 157

Views: 93331

Answers (19)

sobstel
sobstel

Reputation: 1462

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

Upvotes: 6

Jbur43
Jbur43

Reputation: 1312

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

Upvotes: 4

magiccrafter
magiccrafter

Reputation: 5482

For Windows x64:

gem inst nokogiri --pre --platform ruby

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

Upvotes: 1

Sojoodi
Sojoodi

Reputation: 562

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.

Upvotes: 23

hohner
hohner

Reputation: 11588

It will be:

sudo yum install -y libxml2 libxml2-devel

on RHEL servers.

Upvotes: 11

pdu
pdu

Reputation: 10413

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.)

Upvotes: 13

Eduardo
Eduardo

Reputation: 2704

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"
    

Upvotes: 65

Romi
Romi

Reputation: 11

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

Upvotes: 1

Chloe
Chloe

Reputation: 26284

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"

Upvotes: 11

Liber
Liber

Reputation: 870

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.

Upvotes: 0

rilla
rilla

Reputation: 638

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.

Upvotes: 13

Aleks N.
Aleks N.

Reputation: 6247

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.

Upvotes: 1

Johnster
Johnster

Reputation: 152

=== 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.

Upvotes: 0

user1455180
user1455180

Reputation: 561

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

Upvotes: 0

Paul Fioravanti
Paul Fioravanti

Reputation: 16793

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.

Upvotes: 1

Erik Peterson
Erik Peterson

Reputation: 4311

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".

Upvotes: 167

Simone
Simone

Reputation: 21272

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

Upvotes: 4

Ryan
Ryan

Reputation: 9430

You may actually need to install both of these packages

sudo apt-get install libxslt-dev libxml2-dev

Upvotes: 249

d11wtq
d11wtq

Reputation: 35318

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.

Upvotes: 2

Related Questions