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

后端 未结 5 1922
[愿得一人]
[愿得一人] 2020-12-25 11:10

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

How to diagnose this and solve it?

# gem install nokogiri
Building native extensio         


        
相关标签:
5条回答
  • 2020-12-25 11:47

    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.

    0 讨论(0)
  • 2020-12-25 11:56

    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
    
    0 讨论(0)
  • 2020-12-25 12:07

    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!

    0 讨论(0)
  • 2020-12-25 12:07

    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.

    0 讨论(0)
  • 2020-12-25 12:08

    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
    
    0 讨论(0)
提交回复
热议问题