66

I'm running bundle install and I'm getting this error:

Building nokogiri using system libraries.

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb --use-system-libraries
Building nokogiri using system libraries.
libxml2 version 2.6.21 or later is required!
*** 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=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
    --help
    --clean
    --use-system-libraries
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --with-xml2-dir
    --without-xml2-dir
    --with-xml2-include
    --without-xml2-include=${xml2-dir}/include
    --with-xml2-lib
    --without-xml2-lib=${xml2-dir}/lib
    --with-libxml-2.0-config
    --without-libxml-2.0-config
    --with-pkg-config
    --without-pkg-config
    --with-xslt-dir
    --without-xslt-dir
    --with-xslt-include
    --without-xslt-include=${xslt-dir}/include
    --with-xslt-lib
    --without-xslt-lib=${xslt-dir}/lib
    --with-libxslt-config
    --without-libxslt-config
    --with-pkg-config
    --without-pkg-config
    --with-exslt-dir
    --without-exslt-dir
    --with-exslt-include
    --without-exslt-include=${exslt-dir}/include
    --with-exslt-lib
    --without-exslt-lib=${exslt-dir}/lib
    --with-libexslt-config
    --without-libexslt-config
    --with-pkg-config
    --without-pkg-config

extconf failed, exit code 1

Gem files will remain installed in /Users/myuser/projectpath/vendor/bundle/ruby/2.0.0/gems/nokogiri-1.6.2.rc2 for inspection.
Results logged to /Users/myuser/projectpath/vendor/bundle/ruby/2.0.0/extensions/universal-darwin-13/2.0.0/nokogiri-1.6.2.rc2/gem_make.out
An error occurred while installing nokogiri (1.6.2.rc2), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.6.2.rc2'` succeeds before bundling.

Any ideas what could this be?

EDIT:

I also have installed nokogiri following the instructions in the tutorial, and the weird thing is that the gem was successfully installed. When I run gem list, I have nokogiri (1.6.2.1), but when running bundle install it displays the error.

yozzz
  • 1,107
  • 1
  • 17
  • 28
FabKremer
  • 2,039
  • 2
  • 14
  • 26

18 Answers18

231

Previous advises didn't help me, here is the solution for OS 10.9:

brew install libxml2
bundle config build.nokogiri "--use-system-libraries --with-xml2-include=/usr/local/opt/libxml2/include/libxml2"
bundle install
Cœur
  • 32,421
  • 21
  • 173
  • 232
Kir
  • 7,453
  • 10
  • 49
  • 66
  • 4
    Thank you - this worked for me as well on Mac OS X 10.9.5 when everything else didn't. – Dane Balia Sep 22 '14 at 08:28
  • 1
    Great! Wonderful! Awesome! Outstanding! Hours of pain are over! Hell knows who wrote instruction on http://www.nokogiri.org/tutorials/installing_nokogiri.html that required me to uninstall gcc and mess with Xcode. – Aldekein Nov 15 '14 at 16:27
  • 1
    Thank you so much. Could somebody explain why this is this way and how to prevent in the future? – rpearce Nov 25 '14 at 21:08
  • This helped me, but on Ubuntu I had to figure out the install path using dpkg: dpkg -L libxml2-dev Using dpkg on libxml2 didn't work, only libxml2-dev (after making sure it was installed). Thanks! – saranicole May 19 '15 at 19:18
  • 1
    I got this error :`Could not locate Gemfile or .bundle/ directory` after running `bundle install` – Zigii Wong Jan 05 '16 at 08:04
  • Yep, this fixed my problem after upgrade to el capitan 10.11.3 – thewoolleyman Feb 22 '16 at 07:05
  • Received this warning when running `brew install libxml2`. ``` Warning: libxml2 is a keg-only and another version is linked to opt. Use `brew install --force` if you want to install this version ``` Since it said I had one installed, (which I know why I have there) rather than force, I just skipped it. The remainder of the steps (using the one provided by the system) worked perfectly. Thank you, all, for saving me hours trying to figure this out. – bvarner Mar 06 '17 at 15:25
  • Worked with Mojave also...Thanks – Swapnil Chincholkar Nov 06 '18 at 13:06
52

On Mavericks this workaround worked for me:

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

See: https://github.com/sparklemotion/nokogiri/issues/1099#issuecomment-43023208

pcv
  • 1,895
  • 20
  • 24
  • Thank you for this! I've been Googling forever to figure this out and your solution finally installed successfully and I was able to complete the instructions to get Jekyll running (successfully) on OSX 10.9.5 – Terri Ann Oct 03 '14 at 01:43
  • 5
    Works on Yosemite too, if that helps. – Kevin Nov 15 '14 at 17:08
  • 1
    This seems to work as well: `gem install nokogiri -- --with-xml2-include=/usr/local/opt/libxml2/include/libxml2` – btiernay Nov 17 '14 at 01:03
  • 1
    Had to change 10.9 to 10.10 (cuz Yosemite). Otherwise, it went swimmingly. Thanks! – Bryce Anderson Jan 09 '15 at 00:22
  • 2
    Updated for 10.11 El Capitan: `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` – robenkleene Mar 01 '16 at 01:24
  • Updated for 10.13 High Sierra: gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libxml2 – SevenDays Jun 15 '18 at 20:14
15

On Amazon Linux, I had to install these two devel libraries, and then specify the include path for libxml2

$ yum install libxml2-devel libxslt-devel
$ gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/usr/include/libxml2/
andrewtweber
  • 20,851
  • 19
  • 78
  • 105
  • 2
    Thanks. Saved me some time. Amazon Linux (as often) a bit more tricky than others. :) – Anton Babenko Oct 16 '14 at 19:55
  • Marvelous! I've been trying to get this working all day on Amazons Ubuntu install, and your solution finally got Nokogiri built. Yay! – John Judd Oct 20 '14 at 04:32
  • Also `sudo apt-get install binutils-doc cpp-doc gcc-4.8-locales gcc-multilib make autoconf automake1.9 libtool flex bison gdb gcc-doc gcc-4.8-multilib gcc-4.8-doc libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg libasan0-dbg libtsan0-dbg libbacktrace1-dbg libquadmath0-dbg binutils-gold glibc-doc libgmp10-doc libmpfr-dev libstdc++-4.8-doc` – Todd Baur Feb 19 '15 at 16:58
  • I had to first do `yum install -y gcc ruby-devel zlib-devel` before this work – jbnunn Sep 01 '15 at 15:39
  • Thank you! Worked like a charm for a clean CentOS docker image ;) – Denys Vitali Sep 18 '15 at 15:42
4

I also had some problems installing nokogiri, Than I installed following packages, It worked for me:

libxslt-dev
libxml2-dev
libruby1.8
libreadline-ruby1.8
libopenssl-ruby
libxml2

I am not sure, which one was exactly needed.

Saurabh
  • 58,805
  • 34
  • 169
  • 222
3

http://nokogiri.org/tutorials/installing_nokogiri.html. Refer to this link and install the dependencies required for nokogiri. Then run this

  • bundle config build.nokogiri --use-system-libraries
  • bundle install
dTron
  • 146
  • 5
2

On Yosemite 10.10.1, this step did not help (may be it works for others)

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

Basically issue on my local was due to libxml2, so following workaround did the trick

bundle config build.nokogiri "--use-system-libraries --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/libxml2

bundle install
Rohan Daxini
  • 456
  • 4
  • 12
2

This worked for OS X El Capitan:

brew install libxml2 libxslt libiconv 

After running the'brew install libxml2 libxslt libiconv ' You may find, that they are already installed. No worries, it never hurts to double check.

sudo gem install nokogiri -v '1.6.7' -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib

bundle install

Change the version of nokogiri, if needed. e.g.

sudo gem install nokogiri -v '1.6.5' -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib
DaedalusZ
  • 56
  • 3
2

For mac, the reason why it failed is that you didn't have latest Xcode Developer Tools. Nokogiri has already been packaged with libxml2. For the lack of latest Xcode Developer Tools, it can't build native extensions.

To update Xcode Developer Tools using:

xcode-select --install

And then:

gem install nokogiri

If you fix it by using

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

may get a similar warning later:

WARNING: Nokogiri was built against LibXML version 2.9.3, but has dynamically loaded 2.9.0
Kay Wu
  • 609
  • 6
  • 9
1

The error states that your libxml2 system library is out of date. You need version 2.6.21 or later.

On my Debian/ubuntu system, nokogiri compiles for me, and I can see I have version 2.9.1

dpkg -l | grep libxml2-dev
ii  libxml2-dev:amd64                      2.9.1+dfsg1-3                      amd64        Development files for the GNOME XML library

You have three options here.

  1. Check if your OS package manager provides a newer version of libxml2 library

  2. Download the latest libxml2 source code, compile and install it

  3. Manually specify an older version of nokogiri in your Gemfile that works with older libxml2 library. The nokogiri changelog shows that you won't have this problem with nokogiri version 1.4.7 (although using an older version can expose you to security issues)

For solution #3, you would put this in your gemfile: gem 'nokogiri', '1.4.7'

Martin Konecny
  • 50,691
  • 18
  • 119
  • 145
  • 3
    I actually have the latest version of libxml2 (2.9.1), that's what's wierd! – FabKremer May 15 '14 at 14:37
  • @FabKremer do you also have the "-dev" package installed? Assuming you have Debian/Ubuntu, try `sudo apt-get install libxml2-dev` – Martin Konecny May 15 '14 at 15:00
  • @FabKremer, looks to me like you're using a Mac. In that case read here on how to install the latest packages: http://nokogiri.org/tutorials/installing_nokogiri.html – Martin Konecny May 15 '14 at 15:10
  • Yes, I'm using mac. Look my edit please! I've already installed the nokogiri, have the gem in the system but that error persist while doing bundle install on my project – FabKremer May 15 '14 at 19:00
  • That link I posted tells you how to install libxml which nokogiri depends on. From the error message, it looks like nokogiri is not installed. Gem list and bundle install gems in different locations so do not rely on `gem list` to see the results of `bundle install` – Martin Konecny May 15 '14 at 21:02
  • So the problem now is: how could I to stop the bundle install error after having downloaded the nokogiri gem following nokogiri.org/tutorials/installing_nokogiri.html steps ? – FabKremer May 15 '14 at 23:00
  • If you did for example `brew install libxml2 libxslt` and `brew link libxml2 libxslt` then you should be able to do bundle install afterwards. In your gemfile, make sure you are specifying the latest stable nokogiri (1.5.5). At the moment you have 1.6.2 specified which in not a final release. – Martin Konecny May 16 '14 at 03:05
  • 1
    Call me stupid but nokogiri says you need later than 2.6.21, and you have 2.9.1, so why is it complaining? I have 2.7.8 and seeing the same problem. Nokogiri should be fine, as IMO 2.7.8 is greater than 2.6.21, unless I failed basic math. – tigris Jun 08 '14 at 02:01
1

On Yosemite 10.10 the following steps solved my issues completely:

  • sudo xcode-select -switch /Library/Developer/CommandLineTools
  • gem uninstall nokogiri libxml-ruby
  • gem install nokogiri
Nathan Tuggy
  • 2,239
  • 27
  • 28
  • 36
0

This thread on GitHub gave me the trick I needed get Nokogiri 1.6.2 to install:

My ~/.gitconfig had the setting autocrlf = input. When I remove this setting the nokogiri build succeeds.

0

On Yosemite 10.10 this workaround worked for me:

gem install nokogiri -- --use-system-libraries
RckLN
  • 3,714
  • 4
  • 27
  • 34
0

For #CentOS I needed to do the following:

gem update --system
yum install libxml2-devel libxslt-devel ruby-devel
gem install nokogiri -- --use-system-libraries
DaShaun
  • 3,144
  • 2
  • 24
  • 26
0

This is an old thread, but I hit similar issues, and the solution in my case was quite different to those posted so far.

After some digging, I found this thread: https://github.com/bundler/bundler/issues/2648#issuecomment-25124800

It talks about bundler using a different version of ruby to that used by gem.

From OP's paste, we can see bundler is using the Mac system copy of ruby: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby

In my case I found that gem was coming from brew; which gem gave /usr/local/bin/gem which symlinks to /Cellar/ruby/2.3.3/bin/gem. My guess is that the OP's case was the same.

So, gem and bundler are not working off the same ruby installation, which explains why the OP sees this:

When I run gem list, I have nokogiri (1.6.2.1), but when running bundle install it displays the error.

In my case, I simply ran gem install bundler, which made bundler start using the brew version of ruby, and all problems disappeared.

Dave Cahill
  • 414
  • 2
  • 12
0

On OSX 10.12 (Sierra) this worked for me:

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

Harlan T Wood
  • 1,685
  • 18
  • 18
0

I had this exact issue. I run a rails environment on a mac. After I upgraded from Yosemite to Sierra I believe thats when it started.

To fix the problem I just had to install xcode command line tools. I don't know if the Sierra upgrade removed them or what.

xcode-select --install

(this will install the xcode command line tools)

Also another symptom..things like git status won't work. After this i was able to do rails new appname and it was fine.

Jemshit Iskenderov
  • 7,417
  • 5
  • 48
  • 91
0

This can be fixed by installing libxml2:

brew install libxml2
bundle config build.nokogiri --use-system-libraries
bundle install
errakeshpd
  • 2,343
  • 2
  • 24
  • 34
0

This is a problem with macOS, I tried everything above but nothing was working, So tried to install versions of libxml2 but everything in vain.

So use the below command to you will be in good shape then,

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /