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' ...
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.
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.
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).
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With