157

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Rohit A.
  • 1,775
  • 2
  • 11
  • 9

19 Answers19

248

You may actually need to install both of these packages

sudo apt-get install libxslt-dev libxml2-dev
Ryan
  • 9,340
  • 5
  • 39
  • 42
167

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Erik Peterson
  • 4,281
  • 1
  • 22
  • 32
65

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"
    
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Eduardo
  • 2,704
  • 18
  • 17
  • I got some errors about libiconv: "Apple distributes libiconv with OS X, you can find it in /usr/lib." But simply copying your gem install script worked :) Thanks! – Automatico Apr 29 '15 at 08:02
  • Thank you! after an entire morning trying to get nokogiri installed, your answer (specifically with the configuration paths) finally got it working -- rather than the install stating (rather unhelpfully) that libxml2 didn't exist, even when the paths were correct. – Pebbl Jul 04 '15 at 10:56
  • 2
    Using port instead of brew on OS X and just going `$ NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri` did the trick – Andres Kütt Sep 27 '15 at 05:31
  • 2
    In OSX El Capitan 10.11.2, I got to use `brew install libxml2 libxslt homebrew/dupes/libiconv` for it to work – Hai Feng Kao Apr 22 '16 at 03:08
  • 2
    Besides, I need to add `--with-xml2-include="/usr/local/opt/libxml2/include/libxml2"` for `gem install nokogiri` command to work. Otherwse, it will show `libxml2 version 2.6.21 or later is required!` (my libxml2 version is 2.9.2) – Hai Feng Kao Apr 22 '16 at 03:20
23

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Sojoodi
  • 552
  • 5
  • 10
  • This did it for me, although in ubuntu I had to use *apt-get install libnokogiri-ruby1.9.1* , then edit the Gemfile to ask for nokogiri 1.5.5 instead of 1.5.9 – ffflabs Feb 05 '14 at 13:58
13

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
rilla
  • 638
  • 6
  • 9
13

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
pdu
  • 10,295
  • 4
  • 58
  • 95
11

It will be:

sudo yum install -y libxml2 libxml2-devel

on RHEL servers.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
hohner
  • 11,498
  • 8
  • 49
  • 84
11

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"
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Chloe
  • 25,162
  • 40
  • 190
  • 357
  • I get 'Unable to find package libxml2'. What sources do you have in Chocolately? – Isaac Waller Mar 21 '14 at 05:15
  • 3
    Found the problem! Nuget sources was disabled in sources file of Chocolatey! But gem does not work: [tid-1789500] 126: The specified module could not be found. - C:/Ruby200/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.1/lib/nokogiri/nokogiri.so (LoadError) – Ratko Stibric Mar 24 '14 at 19:26
  • @RatkoStibric I think you have to build with the dynamic libs: `...\x64\Release\dynamic\cdecl`. – Chloe Mar 28 '14 at 04:22
  • 1
    Once chocolately is installed, add a source using the following command: `chocolatey sources add -name NuGet -source "https://go.microsoft.com/fwlink/?LinkID=230477"` – Richard Peck Nov 04 '15 at 10:03
  • 1
    Thanks for Chocolatey, never knew about that before! – Richard Peck Nov 04 '15 at 10:22
6

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
sobstel
  • 1,432
  • 16
  • 11
4

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
Simone
  • 20,302
  • 14
  • 79
  • 103
4

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

Jbur43
  • 1,284
  • 17
  • 38
2

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.

d11wtq
  • 34,788
  • 19
  • 120
  • 195
  • i am on ubuntu and i have followed all the above installations as told in the any the other stackoverflow questions. – Rohit A. Jun 08 '11 at 11:14
1

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Romi
  • 11
  • 1
1

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Aleks N.
  • 6,051
  • 3
  • 42
  • 42
  • Using `sudo` is OK *IF* you understand how it works, where it's going to take effect, why the files being installed or updated should be installed and how to undo the action if it fails. Otherwise, using a sandbox like rbenv or RVM is a _REALLY_ good idea. – the Tin Man Oct 02 '15 at 17:09
1

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Paul Fioravanti
  • 16,423
  • 7
  • 71
  • 122
1

For Windows x64:

gem inst nokogiri --pre --platform ruby

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

magiccrafter
  • 5,175
  • 1
  • 56
  • 50
0
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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Liber
  • 422
  • 4
  • 12
0

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Johnster
  • 133
  • 1
  • 7
0

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user1455180
  • 511
  • 4
  • 9