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

5

34

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'
...
Ravens answered 9/9, 2012 at 2:53 Comment(0)
R
66

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!

Ravens answered 9/9, 2012 at 3:51 Comment(4)
Good god, this answer needs to be nominated as one of the best answers EVAR. Not only do you provide the answer, but you also provide a way to get the answer. Thanks!Eldwen
Thank you. This worked, but an additional "checking for libxml/parser.h... no" install failure was presented. I then referenced this page and applied the gcc symlink as noted. If the error is not verbose enough, check the /gems/nokogiri-..*/ext/nokogiri/mkmf.log for additional insight. I additionally had issues with libiconv, which it's now recommended you install from source. Reference the Installing Nokogiri page.Ordeal
also if all of the above still doesn't work, like it didn't in my ubuntu, just try 'sudo gem install nokogiri'Gayle
also if that also doesn't work, then uninstall everything, rvm implode, etc. and use rvm from rvm.io/rvm/install to install ruby and afterwards railsGayle
H
12

Installing Nokogiri Website

'Installing Nokogiri' is a website dedicated to installing Nokogiri on the major platforms - Here is an excerpt about Installing Nokogiri on Ubuntu:

Because Nokogiri needs to be compiled and dynamically linked against both libxml2 and libxslt, it has gained a reputation for being complicated to install.

As of Nokogiri 1.6, libxml2 and libxslt source code is bundled with Nokogiri, and compiled at gem-install-time. The instructions in this document should work for all versions 1.6.4 and later.

Ubuntu / Debian

Installation should Just Work™ on Ubuntu and Debian using Nokogiri’s vendored libxml2 and libxslt:

gem install nokogiri

[...]

Using Your System Libraries

If, instead of Nokogiri’s vendored libraries, you’d like to use your system’s libxml2, libxslt and related libraries, please first understand that you may be asking Nokogiri to work with an unsupported version of libxml2.

sudo apt-get install pkg-config
gem install nokogiri -- --use-system-libraries

FYI - I am using Nokogiri 1.6.6.2 and it didn't 'just work'. I got it going with the --use-system-libraries.


Mac OS X

The website's advice also covers OS X - this worked for me:
gem update --system xcode-select --install gem install nokogiri


Conclusion

If you have a Nokogiri problem on any platform you should check out the website.

Hounding answered 24/5, 2015 at 10:53 Comment(0)
S
4

On CentOS here is what I needed to do:

gem update --system
yum install libxml2-devel libxslt-devel ruby-devel
gem install nokogiri -- --use-system-libraries
Swell answered 5/11, 2015 at 18:29 Comment(0)
K
3

Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers.

You are probably missing zlib headers which are required for -lz flag in order to compile the sources correctly. Install on Linux by:

sudo apt-get install libz-dev

For missing libiconv, try installing libiconv-hook-dev package which has header files of libiconv-hook, e.g.

sudo apt-get install libiconv-hook1 libiconv-hook-dev

On OS X, try installing development tools via: xcode-select --install.

If there is still problem, check mkmf.log file for more specific details about your error.


On Ubuntu, try the following dependency fix combo:

sudo apt-get install gcc ruby-dev libxslt-dev libxml2-dev zlib1g-dev
Kirk answered 9/7, 2016 at 1:4 Comment(0)
T
2

The 'could not create Makefile' error you're seeing could also be because you haven't agreed to the Xcode license (you have to agree to it after each time you update Xcode). Running sudo xcodebuild -license accept should eliminate this error for you and allow you to then run gem install nokogiri successfully.

Terzetto answered 25/9, 2017 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.