Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install Nokogiri Ruby gem with mkmf.log saying libiconv not found?

I'm installing the Ruby Nokogiri gem and finding the error below.

How to diagnose this and solve it?

# gem install nokogiri Building native extensions.  This could take a while... ERROR:  Error installing nokogiri: ERROR: Failed to build gem native extension. ... /opt/ruby/1.9.3-p194/bin/ruby extconf.rb checking for libxml/parser.h... *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers.  Check the mkmf.log file for more details.  You may need configuration options. ... /opt/ruby/1.9.3-p194/lib/ruby/1.9.1/mkmf.rb:381:in `try_do':  The compiler failed to generate an executable file. (RuntimeError) You have to install development tools first. from /opt/ruby/1.9.3-p194/lib/ruby/1.9.1/mkmf.rb:506:in `try_cpp' ... 
like image 329
joelparkerhenderson Avatar asked Sep 09 '12 02:09

joelparkerhenderson


People also ask

Can t install Nokogiri gem?

Solution. Uninstall all versions of Nokogiri on your system, and then re-resolve your dependencies (using bundle or gem install ). This error can occur when a version of Nokogiri installed for a different version of Ruby is used by an unsupported version of Ruby.

Is Nokogiri a Ruby gem?

Imagine a tool and you most probably have it in your Ruby kit. One of the best gems for Ruby on Rails is Nokogiri which is a library to deal with XML and HTML documents. The most common use for a parser like Nokogiri is to extract data from structured documents.

What is the use of Nokogiri gem?

Nokogiri (鋸) makes it easy and painless to work with XML and HTML from Ruby. It provides a sensible, easy-to-understand API for reading, writing, modifying, and querying documents. It is fast and standards-compliant by relying on native parsers like libxml2 (C) and xerces (Java).


1 Answers

To diagnose and solve, here's what worked for me.

To find out what failed, go to your ruby gems directory.

For example:

$ cd <MY RUBY DIRECTORY>/lib/ruby/gems/2.0.0/gems 

If you don't know your gem directory, try this:

$ echo $GEM_HOME /opt/gems/2.0.0 $ cd /opt/gems/2.0.0/gems 

What version of nokogiri am I installing?

$ ls -ladg nokogiri-* nokogiri-1.5.5 

Go to the installer directory:

$ cd nokogiri-1.5.5/ext/nokogiri 

Try installing manually:

$ ruby extconf.rb 

Result:

checking for libxml/parser.h... *** extconf.rb failed *** ... 

I'm using Ubuntu so I search for any similar packages:

$ aptitude search libxml 

Results:

p libxml2 - GNOME XML library p libxml2-dev - Development files for the GNOME XML library ... 

I believe that libxml2 will work fine.

$ apt-get install libxml2 

Ruby native gems often need the *-dev packages for headers so install them:

$ apt-get install libxml2-dev 

Now do I have the parser file?

$ find / | grep libxml/parser.h 

Yes, the result shows the parser:

/usr/include/libxml2/libxml/parser.h 

Now try installing again, this time providing the libxml2 path:

$ gem install nokogiri -- --with-xml2-dir=/usr/include/libxml2 

It still fails, so read the mkmf error log:

$ more mkmf.log  

The error log shows what failed and has these lines that look promising:

package configuration for libxslt is not found 

Is there a package for it?

$ aptitude search libxslt 

Results:

v   libxslt-dev i   libxslt-ruby ... 

Install the dev package:

$ apt-get install libxslt-dev 

Now try installing again, and also put xslt on the path:

$ gem install nokogiri -- \ --with-xml2-dir=/usr/include/libxml2 \ --with-xslt-dir=/usr/include/libxslt 

Success!

like image 156
joelparkerhenderson Avatar answered Nov 10 '22 15:11

joelparkerhenderson