73

I know there are a lot of questions about this gem but no answer has worked for me.

When I run in SSH gem install nokogiri I get this error:

Extracting libxml2-2.8.0.tar.gz into tmp/x86_64-unknown-linux-gnu/ports/libxml2/2.8.0... OK
Running patch with /home/user58952277/.gem/ruby/1.9.3/gems/nokogiri-1.6.2.1/ports/patches/libxml2/0001-Fix-parser-local-buffers-size-problems.patch...
Running 'patch' for libxml2 2.8.0... ERROR, review 'tmp/x86_64-unknown-linux-gnu/ports/libxml2/2.8.0/patch.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.

My host told me that all libs are installed. Here are the full logs after executing the install nokogiri command:

Building native extensions.  This could take a while...
Building nokogiri using packaged libraries.
Building libxml2-2.8.0 for nokogiri with the following patches applied:
    - 0001-Fix-parser-local-buffers-size-problems.patch
    - 0002-Fix-entities-local-buffers-size-problems.patch
    - 0003-Fix-an-error-in-previous-commit.patch
    - 0004-Fix-potential-out-of-bound-access.patch
    - 0005-Detect-excessive-entities-expansion-upon-replacement.patch
    - 0006-Do-not-fetch-external-parsed-entities.patch
    - 0007-Enforce-XML_PARSER_EOF-state-handling-through-the-pa.patch
    - 0008-Improve-handling-of-xmlStopParser.patch
    - 0009-Fix-a-couple-of-return-without-value.patch
    - 0010-Keep-non-significant-blanks-node-in-HTML-parser.patch
    - 0011-Do-not-fetch-external-parameter-entities.patch
************************************************************************
IMPORTANT!  Nokogiri builds and uses a packaged version of libxml2.

If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:

    gem install nokogiri -- --use-system-libraries

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

However, note that nokogiri does not necessarily support all versions
of libxml2.

For example, libxml2-2.9.0 and higher are currently known to be broken
and thus unsupported by nokogiri, due to compatibility problems and
XPath optimization bugs.
************************************************************************
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

        /opt/rubies/ruby-1.9.3/bin/ruby extconf.rb
Building nokogiri using packaged libraries.
checking for iconv.h... yes
checking for iconv_open() in iconv.h... yes
Building libxml2-2.8.0 for nokogiri with the following patches applied:
    - 0001-Fix-parser-local-buffers-size-problems.patch
    - 0002-Fix-entities-local-buffers-size-problems.patch
    - 0003-Fix-an-error-in-previous-commit.patch
    - 0004-Fix-potential-out-of-bound-access.patch
    - 0005-Detect-excessive-entities-expansion-upon-replacement.patch
    - 0006-Do-not-fetch-external-parsed-entities.patch
    - 0007-Enforce-XML_PARSER_EOF-state-handling-through-the-pa.patch
    - 0008-Improve-handling-of-xmlStopParser.patch
    - 0009-Fix-a-couple-of-return-without-value.patch
    - 0010-Keep-non-significant-blanks-node-in-HTML-parser.patch
    - 0011-Do-not-fetch-external-parameter-entities.patch
************************************************************************
IMPORTANT!  Nokogiri builds and uses a packaged version of libxml2.

If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:

    gem install nokogiri -- --use-system-libraries

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

However, note that nokogiri does not necessarily support all versions
of libxml2.

For example, libxml2-2.9.0 and higher are currently known to be broken
and thus unsupported by nokogiri, due to compatibility problems and
XPath optimization bugs.
************************************************************************
Extracting libxml2-2.8.0.tar.gz into tmp/x86_64-unknown-linux-gnu/ports/libxml2/2.8.0... OK
Running patch with /home/user58952277/.gem/ruby/1.9.3/gems/nokogiri-1.6.2.1/ports/patches/libxml2/0001-Fix-parser-local-buffers-size-problems.patch...
Running 'patch' for libxml2 2.8.0... ERROR, review 'tmp/x86_64-unknown-linux-gnu/ports/libxml2/2.8.0/patch.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=/opt/rubies/ruby-1.9.3/bin/ruby
    --help
    --clean
    --use-system-libraries
    --enable-static
    --disable-static
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --enable-cross-build
    --disable-cross-build
/home/user58952277/.gem/ruby/1.9.3/gems/mini_portile-0.6.0/lib/mini_portile.rb:279:in `block in execute': Failed to complete patch task (RuntimeError)
    from /home/user58952277/.gem/ruby/1.9.3/gems/mini_portile-0.6.0/lib/mini_portile.rb:271:in `chdir'
    from /home/user58952277/.gem/ruby/1.9.3/gems/mini_portile-0.6.0/lib/mini_portile.rb:271:in `execute'
    from extconf.rb:282:in `block in patch'
    from extconf.rb:279:in `each'
    from extconf.rb:279:in `patch'
    from /home/user58952277/.gem/ruby/1.9.3/gems/mini_portile-0.6.0/lib/mini_portile.rb:108:in `cook'
    from extconf.rb:253:in `block in process_recipe'
    from extconf.rb:154:in `tap'
    from extconf.rb:154:in `process_recipe'
    from extconf.rb:419:in `<main>'
7
  • Sounds like there is more info in /tmp/x86_64-unknown-linux-gnu/ports/libxml2/2.8.0/patch.log Jun 16, 2014 at 20:24
  • I would also check the mkmf.log file for more details. Jun 16, 2014 at 20:26
  • It says /opt/rubies/ruby-1.9.3/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. Jun 16, 2014 at 20:39
  • So they are not installed as my host says, right? Jun 16, 2014 at 20:39
  • Correct, you need a compiler it seems.
    – CWitty
    Jun 16, 2014 at 20:42

18 Answers 18

144

2020 April 6th Update:

macOS Catalina 10.15

gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/libxml2/

macOS Mojave 10.14

gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/libxml2/

macOS High Sierra 10.13

gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/libxml2/

macOS Sierra 10.12:

gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libxml2/

OS X El Capitan 10.11

gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2/

Consider to add sudo if you don't have permission.


For some reason Apple’s Yosemite version of OSX does not have a system accessible installation of libxml2. Nokogiri requires this in order to compile and luckily Xcode has a version of libxml2 bundled with it — we just need to specify it when installing the gem. It’s important to get Nokogiri installed correctly because as of right now Rails 4.2.1.rc4 automatically attempts to install it and you will feel pain.

10
  • Thanks, I've been looking for this for a long time. Aug 22, 2016 at 20:49
  • 1
    Yes, this worked for me but the accepted answer does not. I am running El Capitan Sep 25, 2016 at 5:32
  • 1
    This should be the correct answer. Upvoted. Please note for Mac OS Sierra v.10.12 users, you need to change the command to: gem install nokogiri -- --use-system-libraries --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libxml2/ Oct 7, 2016 at 19:47
  • Even though I'm on macOS Sierra, I still needed to use "gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2/ " path to make it work. Oct 26, 2016 at 22:32
  • Using suggested one for macOS Sierra with sudo permission let me install nokogiri and rails then. Thanks a lot.
    – dnzprmksz
    Dec 11, 2016 at 16:18
55

Finally, the problem was caused by nokogiri itself by shipping it's own libxml2 that's incompatible with some systems.
So to install nokogiri I had to tell it that it should use the system libraries.

I installed it manually by:

gem install nokogiri -v 1.6.2.1 -- --use-system-libraries

And it worked well. Other answers didn't solve it.

6
  • 3
    I dropped the -v with version specifics and it solved a similar problem. Jun 22, 2014 at 7:24
  • Working with Ubuntu 14.04 as well :) Jan 28, 2016 at 13:15
  • Another (cheating ?) solution on Ubuntu 15.10 : sudo apt install ruby-nokogiri Feb 8, 2016 at 13:06
  • @FrédéricGrosshans bad solution for rvm/rbenv users
    – sunki
    Mar 14, 2016 at 8:13
  • This ended up being the key for me to get my env up and running - I'm using rbenv and bundler on the new mac M1 chip. I installed some special system libraries that worked with my box (V8, etc..), and then install each gem (nokogiri, bcrypt, mini_racer, rinku, etc...) manually with the version specified and -- --use-system-libraries. This is obviously not ideal (and maybe not recommended), but its a functional work around for now.
    – BananaNeil
    Mar 18, 2021 at 12:12
24

I ran into this same problem, because of an unlisted build dependency. When I found the tmp directory in question:

find ~/.rbenv/ -name patch.log

It said:

sh: patch: command not found

Fixed that with a simple:

sudo yum install -y patch
1
  • 3
    This helped me get Nokogiri installed on Amazon Linux! Mar 26, 2015 at 22:06
18

On OSX Yosemite, I did xcode-select --install in a terminal. After which, bundle worked fine with that gem.

3
  • 3
    this worked for me, I thought I installed developer tools already but I guess not thanks Oct 21, 2015 at 1:24
  • worked for me too. I upgraded to OS X El Capitan and had to get the latest xcode dev tools Nov 30, 2015 at 0:42
  • 1
    xcode-select --install needs to be done after every os upgrade I think. Worked for me after I upgraded to Sierra.
    – Harish Ved
    Oct 23, 2016 at 3:24
4

This works for me in Windows 7 Home Basic 64 Bit:

gem install nokogiri -v 1.6.2.1 -- --use-system-libraries
4

Nothing in current answers worked for me (I use Ubuntu 16.04, not Mac).

However, by following the instructions in Nokogiri site and this part did the trick:

# See http://www.nokogiri.org/tutorials/installing_nokogiri.html#ubuntu___debian
sudo apt-get install build-essential patch
sudo apt-get install ruby-dev zlib1g-dev liblzma-dev

Note I've been using Nokogiri since 2009 and I've always had trouble installing it. Any further comment would probably not comply with SO T&Cs :)

3

This worked for me with OS X Yosemite.

gem install nokogiri -v 1.6.5 -- --use-system-libraries
3

Installing Nokogiri in Mac OS El Capitan:

brew install libxml2 bundle config build.nokogiri "--use-system-libraries --with-xml2-include=/usr/local/opt/libxml2/include/libxml2"

bundle install

It working for me :)

1
  • This worked for me on OSX Yosemite. None of the other answers worked. Thanks!
    – rayryeng
    Jul 10, 2019 at 19:38
3

Nokogiri didn't find Xcode libs on my OSX Sierra machine until I ran:

sudo xcodebuild -license accept

1
  • This was the issue for me. I had already agreed to the CLI tools before but I had just updated it via app store.app progrom. I found a hint in the file mkmf.log which was mentioned in my error output. Once I checked the file I saw that there was an error message of: Agreeing to the Xcode/iOS license requires admin privileges, please run “sudo xcodebuild -license” and then retry this command.. After that Nokogiri could install without errors.
    – Sgnl
    Dec 18, 2017 at 20:48
2

This is what worked for me on OS X 10.10.3 Yosemite:

sudo gem install nokogiri -- --use-system-libraries --with-xslt-dir=/usr/local/opt/libxslt --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/libxml2 --with-xml2-lib=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/lib
2

Just Like wongzigii says the install command works with those flags with macOS Sierra

gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libxml2/

2

I figured this out the hard way and took a few hours to follow the breadcrumbs and read up on what people were trying out. Nothing helped. Everything on this overflow page just didn't help so here's how I fixed this:

If you use your machine for other development projects that aren't in ruby. You might want to check package manager installations for other languages. More importantly - you might want to check for other c compiler installations - namely clang

In my case - I had installed anaconda/conda(python) and this replaced a bunch of paths in my .bash_profile to use clang as the main c compiler where as xcode command line tools and rebenv were all using gcc installed through Homebrew earlier.

I removed anaconda(using anaconda-clean) and the clang installation and then tried to bundle. That's when I got the missing libxml2 issue. I then REINSTALLED libxml2 through homebrew to ensure the symlinks would be redone through homebrew and followed the instructions that followed to add the appropriate export flags to my ~/bash_profile file.

Successfully bundles now. The reason is, the gems are now being bundled with the same compiler as the ruby version I am using for that project.

Docker would have made this a non issue for a polyglot dev machine like many of us I assume.

So heads up.

1

This worked for me:

sudo gem install nokogiri -v 'versionNumber'  -- --use-system-libraries --with-xslt-dir=/usr/local/opt/libxslt --with-xml2-include=/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2 --with-xml2-lib=/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/lib
1

step 1:

brew install libxml2

Step 2:

bundle config build.nokogiri --use-system-libraries

Step 3: then try

bundle install

0

Finally after much struggle the following worked for me (OS X 10.8.5, macports). (I am installing github pages i.e. jekyll)

/opt/local/bin/gem install nokogiri -v 1.6.6.2 -- --use-system-libraries --with-xslt-dir=/usr/local/opt/libxslt --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/libxml2 --with-xml2-lib=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/lib 
0

In my case, I am using Mac OSX Mojave and had to install a package described here: https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes#3035624

Here is the path to the package: /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

After installing it, the installation finished without problems.

0

If using bundler, run this command first:

bundle config build.nokogiri --use-system-libraries

-2

Am using macOS Sierra.

Went to nokogiri documentation and followed this:

ON MAC OSX

Installation should Just Work™ using Nokogiri’s vendored libxml2 and libxslt.

First, make sure you have the latest version of RubyGems and xcode commandline tools:

1 gem update --system

2 xcode-select --install

Then install nokogiri:

1 gem install nokogiri

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.