Nokogiri: Unable to install nokogiri gem

Created on 17 Apr 2014  路  18Comments  路  Source: sparklemotion/nokogiri

Hi, i'm using OSX 10.9.2 and Ruby 2.1.1 installed with rbenv.
When running $ sudo gem install nokogiri -v '1.6.1' I receive the following error:

Building native extensions.  This could take a while...
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

    /usr/local/var/rbenv/versions/2.1.1/bin/ruby extconf.rb
Extracting libxml2-2.8.0.tar.gz into tmp/i686-apple-darwin11/ports/libxml2/2.8.0... OK
Running 'configure' for libxml2 2.8.0... ERROR, review 'tmp/i686-apple-darwin11/ports/libxml2/2.8.0/configure.log' to see what happened.
*** 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.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/usr/local/var/rbenv/versions/2.1.1/bin/ruby
/usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mini_portile-0.5.3/lib/mini_portile.rb:278:in `block in execute': Failed to complete configure task (RuntimeError)
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mini_portile-0.5.3/lib/mini_portile.rb:270:in `chdir'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mini_portile-0.5.3/lib/mini_portile.rb:270:in `execute'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mini_portile-0.5.3/lib/mini_portile.rb:65:in `configure'
    from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mini_portile-0.5.3/lib/mini_portile.rb:108:in `cook'
    from extconf.rb:101:in `block in <main>'
    from extconf.rb:119:in `call'
    from extconf.rb:119:in `block in <main>'
    from extconf.rb:109:in `tap'
    from extconf.rb:109:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/nokogiri-1.6.1 for inspection.
Results logged to /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/extensions/x86_64-darwin-13/2.1.0-static/nokogiri-1.6.1/gem_make.out

I am using Homebrew and tried the installation steps listed on the nokogiri site:
brew install libxml2 libxslt
brew link libxml2 libxslt
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
tar xvfz libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr/local/Cellar/libiconv/1.13.1
make
sudo make install
Then I tried installing passing in the options as specified below:
sudo gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.8.0/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.8.0/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 --with-iconv-include=/usr/local/Cellar/libiconv/1.13.1/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib'

This shows the same error as per above.

A google search shows that this can be a common error https://www.google.co.uk/search?q=Running+%27configure%27+for+libxml2+2.8.0...+ERROR%2C+review&oq=Running+%27configure%27+for+libxml2+2.8.0...+ERROR%2C+review&aqs=chrome..69i57j69i59j0.238j0j4&sourceid=chrome&es_sm=91&ie=UTF-8#q=Running+'configure'+for+libxml2+2.8.0...+ERROR%2C+review&safe=off

Some solutions recommend installing command line tools although I have xcode 5.1.1 which comes with command line tools I believe.

I have also tried specifying that nokogiri should use the system libraries as one answer suggested:
$ NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri

Most helpful comment

Thanks @hackjoy for adding the solution, I just added sudo in front of it and it works and saved my day.

sudo NOKOGIRI_USE_SYSTEM_LIBRARIES=1 \
gem install nokogiri -- \
  --with-xml2-config=/usr/local/opt/libxml2/bin/xml2-config \
  --with-xslt-config=/usr/local/opt/libxslt/bin/xslt-config

All 18 comments

try without sudo.
dont use sudo in OS X if you are using homebrew

Running $ gem install nokogiri gives the following:

ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied @ rb_sysopen - /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/nokogiri-1.6.1/.autotest

failed to install nokogiri gem on 10.9.2 too

visualtekiMac-mini:racc-master ihacku$ gem install nokogiri -v '1.6.1'
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
visualtekiMac-mini:racc-master ihacku$ sudo gem install nokogiri -v '1.6.1'
Password:
Building native extensions.  This could take a while...
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
Extracting libxml2-2.8.0.tar.gz into tmp/x86_64-apple-darwin13.1.0/ports/libxml2/2.8.0... OK
Running 'configure' for libxml2 2.8.0... OK
Running 'compile' for libxml2 2.8.0... OK
Running 'install' for libxml2 2.8.0... OK
Activating libxml2 2.8.0 (from /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.1/ports/x86_64-apple-darwin13.1.0/libxml2/2.8.0)...
Extracting libxslt-1.1.26.tar.gz into tmp/x86_64-apple-darwin13.1.0/ports/libxslt/1.1.26... OK
Running 'configure' for libxslt 1.1.26... OK
Running 'compile' for libxslt 1.1.26... OK
Running 'install' for libxslt 1.1.26... OK
Activating libxslt 1.1.26 (from /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.1/ports/x86_64-apple-darwin13.1.0/libxslt/1.1.26)...
checking for libxml/parser.h... yes
checking for libxslt/xslt.h... yes
checking for libexslt/exslt.h... yes
checking for iconv_open() in iconv.h... no
checking for iconv_open() in -liconv... no
checking for iconv_open() in -liconv... yes
checking for xmlParseDoc() in -lxml2... yes
checking for xsltParseStylesheetDoc() in -lxslt... yes
checking for exsltFuncRegister() in -lexslt... yes
checking for xmlHasFeature()... yes
checking for xmlFirstElementChild()... yes
checking for xmlRelaxNGSetParserStructuredErrors()... yes
checking for xmlRelaxNGSetParserStructuredErrors()... yes
checking for xmlRelaxNGSetValidStructuredErrors()... yes
checking for xmlSchemaSetValidStructuredErrors()... yes
checking for xmlSchemaSetParserStructuredErrors()... yes
creating Makefile

make "DESTDIR="
compiling html_document.c
compiling html_element_description.c
compiling html_entity_lookup.c
compiling html_sax_parser_context.c
compiling html_sax_push_parser.c
compiling nokogiri.c
compiling xml_attr.c
compiling xml_attribute_decl.c
compiling xml_cdata.c
compiling xml_comment.c
compiling xml_document.c
compiling xml_document_fragment.c
compiling xml_dtd.c
compiling xml_element_content.c
compiling xml_element_decl.c
compiling xml_encoding_handler.c
compiling xml_entity_decl.c
compiling xml_entity_reference.c
compiling xml_io.c
compiling xml_libxml2_hacks.c
compiling xml_namespace.c
compiling xml_node.c
compiling xml_node_set.c
compiling xml_processing_instruction.c
compiling xml_reader.c
compiling xml_relax_ng.c
compiling xml_sax_parser.c
compiling xml_sax_parser_context.c
compiling xml_sax_push_parser.c
compiling xml_schema.c
compiling xml_syntax_error.c
compiling xml_text.c
compiling xml_xpath_context.c
compiling xslt_stylesheet.c
linking shared-object nokogiri/nokogiri.bundle
clang: error: unknown argument: '-multiply_definedsuppress' [-Wunused-command-line-argument-hard-error-in-future]
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future
make: *** [nokogiri.bundle] Error 1


Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.1 for inspection.
Results logged to /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.1/ext/nokogiri/gem_make.out

@hackjoy your rbenv installation is not good. you should never use sudo.
you can easily fix thou by changing owner of your /usr/local folder

sudo chown -R your_username /usr/local/

also you can try pre version of nokogiri,
gem install nokogiri --pre

and make sure you have homebrew up to date

@fred Thanks for the advice. I re-installed rbenv so no longer get that access error when trying to install the gem without sudo. I have made sure that the ownership of /usr/local/ is my username. Now running gem install nokogiri gives the same error message as before. Same for gem install nokogiri --pre and

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.13.1/include 
                        --with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib

I have the latest homebrew and installed the versions of the packages specified above (as per the nokogiri site)

Building native extensions.  This could take a while...
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

    /Users/jackhoy/.rbenv/versions/2.1.1/bin/ruby extconf.rb
Extracting libxml2-2.8.0.tar.gz into tmp/x86_64-apple-darwin13.1.0/ports/libxml2/2.8.0... OK
Running 'configure' for libxml2 2.8.0... ERROR, review 'tmp/x86_64-apple-darwin13.1.0/ports/libxml2/2.8.0/configure.log' to see what happened.
*** 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.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/jackhoy/.rbenv/versions/2.1.1/bin/ruby
/Users/jackhoy/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mini_portile-0.5.3/lib/mini_portile.rb:278:in `block in execute': Failed to complete configure task (RuntimeError)
    from /Users/jackhoy/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mini_portile-0.5.3/lib/mini_portile.rb:270:in `chdir'
    from /Users/jackhoy/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mini_portile-0.5.3/lib/mini_portile.rb:270:in `execute'
    from /Users/jackhoy/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mini_portile-0.5.3/lib/mini_portile.rb:65:in `configure'
    from /Users/jackhoy/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mini_portile-0.5.3/lib/mini_portile.rb:108:in `cook'
    from extconf.rb:101:in `block in <main>'
    from extconf.rb:119:in `call'
    from extconf.rb:119:in `block in <main>'
    from extconf.rb:109:in `tap'
    from extconf.rb:109:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /Users/jackhoy/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/nokogiri-1.6.1 for inspection.
Results logged to /Users/jackhoy/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/extensions/x86_64-darwin-13/2.1.0-static/nokogiri-1.6.1/gem_make.out

have a look at the mkmf.log file
have you tried "gem install nokogiri" ?

what is your "brew list" content.
I have installed libffi only and nokogiri installs fine. no libxml2 or libxslt

http://jasdeep.ca/2013/10/installing-nokogiri-fails-os-x-mavericks/

Finally was able to get it to work with the following:

NOKOGIRI_USE_SYSTEM_LIBRARIES=1 \
gem install nokogiri -- \
  --with-xml2-config=/usr/local/opt/libxml2/bin/xml2-config \
  --with-xslt-config=/usr/local/opt/libxslt/bin/xslt-config

Solution referenced in this issue: https://github.com/sparklemotion/nokogiri/issues/1049

@fred I re-installed rbenv with homebrew and followed it's instructions. I did a

sudo chown -R my_username /usr/local/

but I still get permission errors when trying to

gem install nokogiri
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

I'm on my second hour of trying to get this damn gem installed.

Ok, I solved my issue. I needed to point rbenv to homebrew's rbenv folder instead of ~/.rbenv and then reinstall all of the ruby versions I wanted. Now nokogiri is installing.

In my profile:

export RBENV_ROOT=/usr/local/var/rbenv

Then

rbenv install '2.1.1'
rbenv global '2.1.1'

Then nokogiri installed fine and without sudo

Well, I solved my issue as far as installing nokogiri globally. But if I run:

bundle install

in my project that lists a gem with nokogiri as a dependency in the Gemfile, then I'm back to the old error:

libxml2 is missing

WTF!?

@joelcdoyle Are you using homebrew? Try brew install libxml2.

Also I had the same issue as above when re-installing nokogiri with a new version of ruby. Found that I had npm in my path which was not needed: http://stackoverflow.com/a/23455513/1102357

@joelcdoyle it looks like your environment is pretty messy. You've mixed your system ruby with your rbenv ruby. Depending on where / when you installed bundler, you might be dealing with your system ruby again. Same with gem. This is all dependent on both your PATH and on the shebang lines in the generated binstubs you're executing.

Look at which bundler you're using:

head -1 $(which bundle)

and get all that skew sorted out. If you have any other issues, please file a separate issue. This one is closed.

@zenspider That was actually quite helpful in my situation as well

which bundle and which gem were giving me /usr/bin/bundle and /usr/bin/gem instead of ~/.rbenv/shims/gem

What helped correct it for me was sudo rm -i /usr/bin/gem /usr/bin/bundle

After that I:

  1. switched back to my project directory
  2. uninstalled the dependent
  3. (re)-installed the dependent libs: did a brew install libxml2 libiconv libxslt
  4. installed my ruby version fresh (with rbenv)
  5. did gem install bundler
  6. and bundle install ran without any problems.

Nokogiri was fine after that.

For reference:

 鈺扳攢% cat .bundle/config           
---
BUNDLE_PATH: vendor/bundle
BUNDLE_DISABLE_SHARED_GEMS: '1'
BUNDLE_JOBS: 4

 鈺扳攢% which ruby bundle gem
~/.rbenv/shims/ruby
~/.rbenv/shims/bundle
~/.rbenv/shims/gem

Thanks @hackjoy for adding the solution, I just added sudo in front of it and it works and saved my day.

sudo NOKOGIRI_USE_SYSTEM_LIBRARIES=1 \
gem install nokogiri -- \
  --with-xml2-config=/usr/local/opt/libxml2/bin/xml2-config \
  --with-xslt-config=/usr/local/opt/libxslt/bin/xslt-config

I resolved all the issues (OS X 10.10.1) above by updating my Ruby version through homebrew

brew install ruby
echo "export PATH=/usr/local/opt/ruby/bin:\$PATH" >> ~/.bash_profile
source ~/.bash_profile
gem install bundle
gem install nokogiri

@ahmadajmi thank you.

@unimatrixZxero Thanks for the solution.

sudo apt-get install libpq-dev
and then
sudo gem install nokogiri

Was this page helpful?
0 / 5 - 0 ratings