5

I seem to be in a recursive loop of frustration. I'm trying to "bundle install" a project, but it results in:

An error occurred while installing nokogiri (1.6.6.2), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.6.6.2'` succeeds before bundling.

So I try that:

$ gem install nokogiri -v '1.6.6.2'
Building native extensions.  This could take a while...
Successfully installed nokogiri-1.6.6.2
Parsing documentation for nokogiri-1.6.6.2
Done installing documentation for nokogiri after 2 seconds
1 gem installed

All good! Except that when I run "bundle install", I am again told I need to get "gem install nokogiri -v '1.6.6.2'" to install first. AHHHHH.

The mkmf.log file suggests this is an issue with libxml2 (surprise surprise).

conftest.c:3:10: fatal error: 'libxml/xmlversion.h' file not found
#include <libxml/xmlversion.h>
         ^
1 error generated.
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: #include <libxml/xmlversion.h>
4: 
5: #if LIBXML_VERSION < 20621
6: #error libxml2 is way too old
7: #endif
/* end */

So, searching for libxml2 (with "sudo find / -name libxml2") reveals these directories, all of which appear to be at least 2.9.0 or higher:

  • /usr/include/libxml2
  • /usr/local/Cellar/libxml2
  • /usr/local/opt/libxml2

So, is the issue that libxml2 can't be found? And if so, how do I get bundle install to realize that nokogiri is already installed, or at least be able to install it the same way "gem install" does? While I'd like to find a solution for this, I'm really interested in learning what is going on "under the covers" so I can troubleshoot in the future.

Todd R
  • 18,236
  • 8
  • 31
  • 39
  • I solved this by running `gem install libxml-ruby -v '3.0.0' -- --with-xml2-config=/usr/local/opt/libxml2/bin/xml2-config --with-xml2-dir=/usr/local/opt/libxml2 --with-xml2-lib=/usr/local/opt/libxml2/lib --with-xml2-include=/usr/local/opt/libxml2/include` – Korayem Jan 12 '19 at 00:46

2 Answers2

2

On my Mac OS X, I ran these commands, and the problem is fixed.

brew unlink gcc-4.2      # you might not need this step
gem uninstall nokogiri
xcode-select --install
gem install nokogiri
Châu Hồng Lĩnh
  • 1,986
  • 1
  • 20
  • 23
0

I know this recursive loop of frustration. In fact I was on it again today myself. Not with nokogiri, but with libxml-ruby gem. I compiled a little document:

Installing/debugging libxml on mac for ruby/rails.

You can pass a --with-xml2-include argument when installing gem directly. When installing within bundler you can do a bundle config command first as follows.

Use this to build against you system libxml headers:

bundle config build.libxml-ruby --with-xml2-include=`xcrun --show-sdk-path`/usr/include/libxml2

Use this if you've installed libxml using homebrew:

bundle config build.libxml-ruby --with-xml2-include=`brew --prefix libxml2`/usr/include/libxml2

What about nokogiri? I imagine the same will help with problems mentioning libxml while installing nokogiri gem, although hopefully nokogiri is easier (and maybe this has been improved in nokogiri since you asked this question) because...

"nokogiri includes a modified version of libxml2 and libxslt, but also allows you to use a system libxml if you choose to fight that battle yourself. In any case, nokogiri does a good job of dealing with the dependency so it works on just about any platform" teeparham said in Nov 2017

Harry Wood
  • 2,220
  • 2
  • 24
  • 46