Nokogiri installation fails -libxml2 is missing
Asked Answered
C

19

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?

Cutty answered 8/6, 2011 at 10:38 Comment(2)
What's your operating system?Sudderth
This is the more apt answer- sudo apt-get install libxslt-dev libxml2-devRepairer
W
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".

Warrington answered 8/6, 2011 at 10:56 Comment(4)
If that isn't the issue, you may need to install libxml2-devWarrington
i have run the above command and got the 'libxml2 is missing' error. can you tell me more about the build tool chain installation. b'cos i didnt find much help on it.Cutty
I got that message because I didn't have gcc (or any other compiler) installed. I didn't have a compiler toolchain.Warrington
If you're on a mac: brew install libxslt then brew install libxml2Differ
L
249

You may actually need to install both of these packages

sudo apt-get install libxslt-dev libxml2-dev
Loyceloyd answered 18/10, 2011 at 17:45 Comment(2)
How can a comment rated this highly sit below the accepted answer?Toomer
Because the question stated "even after installing all the dependencies". So the accepted answer is the correct one.Make
W
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".

Warrington answered 8/6, 2011 at 10:56 Comment(4)
If that isn't the issue, you may need to install libxml2-devWarrington
i have run the above command and got the 'libxml2 is missing' error. can you tell me more about the build tool chain installation. b'cos i didnt find much help on it.Cutty
I got that message because I didn't have gcc (or any other compiler) installed. I didn't have a compiler toolchain.Warrington
If you're on a mac: brew install libxslt then brew install libxml2Differ
P
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"
    
Prowl answered 10/5, 2014 at 18:45 Comment(5)
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!Tuinal
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.Nernst
Using port instead of brew on OS X and just going $ NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri did the trickOrly
In OSX El Capitan 10.11.2, I got to use brew install libxml2 libxslt homebrew/dupes/libiconv for it to workToque
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)Toque
D
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.

Dialectology answered 16/2, 2012 at 13:21 Comment(1)
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.9Eristic
B
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.)

Braille answered 8/6, 2011 at 12:14 Comment(0)
F
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.

Furnary answered 21/11, 2014 at 15:38 Comment(0)
F
11

It will be:

sudo yum install -y libxml2 libxml2-devel

on RHEL servers.

Fm answered 24/3, 2013 at 18:20 Comment(0)
K
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"
Kerwon answered 5/3, 2014 at 21:58 Comment(6)
I get 'Unable to find package libxml2'. What sources do you have in Chocolately?Degauss
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)Swayback
@RatkoStibric I think you have to build with the dynamic libs: ...\x64\Release\dynamic\cdecl.Kerwon
Once chocolately is installed, add a source using the following command: chocolatey sources add -name NuGet -source "https://go.microsoft.com/fwlink/?LinkID=230477"Milkmaid
Thanks for Chocolatey, never knew about that before!Milkmaid
Is this still correct? I am facing the same error, and choco is unable to install libxml2 ` The package was not found with the source(s) listed. Source(s): 'community.chocolatey.org/api/v2/;https://go.microsoft.com/…'`Kaitlin
W
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
Wallacewallach answered 4/1, 2017 at 13:38 Comment(0)
G
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
Gormandize answered 3/12, 2014 at 19:32 Comment(0)
F
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

Feasible answered 25/11, 2015 at 16:47 Comment(0)
R
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.

Rabah answered 8/6, 2011 at 10:53 Comment(1)
i am on ubuntu and i have followed all the above installations as told in the any the other stackoverflow questions.Cutty
B
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
Burdick answered 13/5, 2014 at 23:54 Comment(0)
O
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.

Octillion answered 2/6, 2015 at 18:17 Comment(1)
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.Dalesman
A
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.

Austral answered 27/9, 2015 at 2:55 Comment(0)
L
1

For Windows x64:

gem inst nokogiri --pre --platform ruby

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

Losse answered 16/12, 2015 at 9:48 Comment(0)
S
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.

Sergeant answered 11/11, 2014 at 7:35 Comment(0)
P
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.

Paraesthesia answered 25/7, 2015 at 19:54 Comment(0)
D
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
Duarte answered 22/9, 2015 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.