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>'
kenorb
  • 155,785
  • 88
  • 678
  • 743
Claudiu Creanga
  • 8,031
  • 10
  • 71
  • 110

18 Answers18

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.

Checkout this post for more info.

Zigii Wong
  • 7,766
  • 8
  • 51
  • 79
  • Thanks, I've been looking for this for a long time. – Stoic Alchemist Aug 22 '16 at 20:49
  • 1
    Yes, this worked for me but the accepted answer does not. I am running El Capitan – Shawn Lauzon Sep 25 '16 at 05: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/ – colemerrick Oct 07 '16 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. – René Michel Oct 26 '16 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 '16 at 16:18
  • MacOSX10.12.sdk and MacOSX10.11.sdk are just symlinks to MacOSX.sdk and the ```=true``` isn't necessary, so you could use ```--use-system-libraries --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libxml2/``` – Gary S. Weaver Jan 05 '17 at 19:01
  • If this works for you but a ```rails new``` command keeps failing, you can tell bundler that this is how you ALWAYS want nokogiri installed: ```bundle config build.nokogiri --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libxml2/``` – Dave Collins May 09 '17 at 02:34
  • Despite being on High Sierra, I needed to use the command for Sierra. I noticed that I did not have the MacOSX10.13 SDK installed. – Huey Nov 28 '17 at 23:54
  • For MacOs High Sierra, after i installed 'gem install pkg-config -v "~> 1.1"' and run the command mentioned in this answer helped to successfully install nokogiri. – maniempire Sep 13 '18 at 15:27
  • I had been stuck on this for a long time. This answer fixed it. I also had to run `bundle update` followed by `bundle install` after running the code in this answer – stevec Feb 19 '19 at 11:55
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.

Claudiu Creanga
  • 8,031
  • 10
  • 71
  • 110
  • 3
    I dropped the -v with version specifics and it solved a similar problem. – analyticsPierce Jun 22 '14 at 07:24
  • Working with Ubuntu 14.04 as well :) – Maria Ines Parnisari Jan 28 '16 at 13:15
  • Another (cheating ?) solution on Ubuntu 15.10 : sudo apt install ruby-nokogiri – Frédéric Grosshans Feb 08 '16 at 13:06
  • @FrédéricGrosshans bad solution for rvm/rbenv users – sunki Mar 14 '16 at 08: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 '21 at 12:12
  • This one saved me a hell lot of time. Worked for installing ruby 2.4.10, rails 4.2.8 on ubuntu 20.04 – risingStark Jul 17 '21 at 15:57
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
billkw
  • 3,350
  • 3
  • 28
  • 32
18

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

Christian Fazzini
  • 19,613
  • 21
  • 110
  • 215
4

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

gem install nokogiri -v 1.6.2.1 -- --use-system-libraries
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
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 :)

Riccardo
  • 1,104
  • 13
  • 22
3

This worked for me with OS X Yosemite.

gem install nokogiri -v 1.6.5 -- --use-system-libraries
thank_you
  • 11,001
  • 19
  • 101
  • 185
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 :)

Community
  • 1
  • 1
Anil Yadav
  • 1,086
  • 7
  • 18
3

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

sudo xcodebuild -license accept

John Skiles Skinner
  • 1,611
  • 1
  • 8
  • 21
  • 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 '17 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
Adam
  • 26,549
  • 8
  • 62
  • 79
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/

Community
  • 1
  • 1
Gus
  • 90
  • 1
  • 9
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.

9_Dave_9
  • 694
  • 1
  • 7
  • 20
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
IKavanagh
  • 6,089
  • 11
  • 42
  • 47
1

step 1:

brew install libxml2

Step 2:

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

Step 3: then try

bundle install

errakeshpd
  • 2,544
  • 2
  • 28
  • 35
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 
alijandro
  • 11,627
  • 2
  • 58
  • 74
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

CoderDave
  • 935
  • 1
  • 9
  • 11
-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

Anna S
  • 157
  • 1
  • 9