83

I have done all kinds of research and tried many different things. I know this question has been answered many times, but none of the suggested solutions are working for me.

After upgrading to Lion I am getting segmentation faults in Ruby. I'm fairly confident it's Nokogiri. So I installed libxml2 via Homebrew. I ran brew link libxml2. Then I reinstalled Nokogiri using that version of the library.

For proof:

$ nokogiri -v
# Nokogiri (1.5.0)
---
warnings: []
nokogiri: 1.5.0
ruby:
  version: 1.9.2
  platform: x86_64-darwin11.0.0
  description: ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0]
  engine: ruby
libxml:
  binding: extension
  compiled: 2.7.8
  loaded: 2.7.8

I've already included Nokogiri at the top of my gemfile and I've also required it in my environment file. I have no idea why I am still getting that warning.

Any suggestions or ideas to make sure it's loading the right version libxml2?

4
  • 9
    Nokogiri 1.6 and higher now bundles libxml2 with the gem, so you can fix this error by just removing libxml2 from Homebrew. brew remove --force libxml2 bundle config --delete build.nokogiri gem uninstall nokogiri libxml-ruby bundle Jun 11, 2013 at 19:38
  • @NateBerkopec. Your solution worked for me while the accepted answer didn't.
    – egyamado
    Apr 12, 2014 at 4:49
  • 1.6.2.1 and Ubuntu 12.04 don't play nice. Dropping back to 1.6.1 fixed it after trying every other fix I could find.
    – user419017
    May 18, 2014 at 3:38

16 Answers 16

99

If you installed Nokogiri with gem install nokogiri, you can resolve this warning by running gem pristine nokogiri to recompile the gem's C extension.

If you installed Nokogiri with bundle install, you can resolve this warning by running bundle exec gem pristine nokogiri to recompile the C extension of the gem wherever Bundler installed it.

5
  • 5
    Excellent, thank you. bundle exec gem pristine nokogiri nailed it.
    – toxaq
    May 16, 2013 at 8:19
  • 1
    Worked perfectly for me with warning about being built against 2.8.0 by dynamically loading 2.7.8 on Ubuntu 12.04
    – richard
    Aug 2, 2013 at 6:50
  • to get the warning to go away on Lion and rails 3.2.16, I had to do both 'bundle exec gem pristine nokogiri' and also move 'gem nokogiri' ahead of 'gem rails'
    – jpw
    Dec 14, 2013 at 22:04
  • 1
    This worked for me in OS X Mavericks, accepted answer did not.
    – CBanga
    Mar 17, 2014 at 16:07
  • This should be the accepted answer, not something that could potentially brick your machine, let alone being extremely verbose and confusing.
    – Greg Blass
    Dec 16, 2016 at 15:56
58

To fix this if you're using homebrew and bundler, add gem 'nokogiri' to the top of your Gemfile, then run these commands:

gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
brew install libxml2 --with-xml2-config
brew install libxslt
bundle config build.nokogiri --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/
bundle install

If you don't use bundler, run these commands instead:

gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
brew install libxml2 --with-xml2-config
brew install libxslt
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/

In your app, you should require nokogiri first, to force the app to load the dynamic libxml2 library instead of the older system version of libxml2 loaded by gems that failed to specify which library to load.

14
  • I don't think the link and unlink commands are needed. I was able get away without using them. Jul 10, 2012 at 19:59
  • note that you might have to prepend the gem commands with bundle exec - for example if you're using rbenv.
    – asymmetric
    Sep 20, 2012 at 11:31
  • 4
    Is there any reason you aren't including brew uninstall libxslt? Oct 11, 2012 at 20:04
  • 5
    For those copy-pasting this answer, the lastest version of libxml2 is 2.9.0 - be sure to edit this when copy pasting! Mar 11, 2013 at 17:30
  • 7
    It bears highlighting that the last sentence is important: require nokogiri first Do this by putting nokogiri in your Gemfile ahead of rails. Jul 1, 2013 at 23:51
39

I just spent the better part of the morning working through this warning. This fix is for people using Mac OS Lion. The fix above using

bundle config build.nokogiri --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib --with-xslt-dir=/opt/local

is for Snow Leopard with libxml2 installed via MacPorts.

With Lion, libxml2 is loaded as part of the bootstrap process. Regardless of which libxml2 Nokogiri is pointing to, the Lion system default library for libxml2 will be used at runtime. Lion uses libxml2.2.7.3 found in /usr (not /usr/local).

As mentioned many other places, one can just ignore the warning. If, like me, the warning drives you crazy, you can do this:

bundle config build.nokogiri --with-xml2-dir=/usr --with-xslt-dir=/opt/local --with-iconv-dir=/opt/local

Interestingly, if you type nokogiri -v at the command line you will get the opposite warning:

WARNING: Nokogiri was built against LibXML version 2.7.3, but has dynamically loaded 2.7.8

This suggests there is more to how libxml2 is being loaded, with Ruby and Rails using the system loaded libxml2 and the command line using libxml2 from the environment path. Anyway, this silences the error for me.

I’ll say it again – this is only for Lion. The previous fix will work for Snow Leopard.

This is the end of the answer. Stop reading here.


OK, you didn’t stop reading... well...

NOT RECOMMENDED!!!!!!

You have been warned. You can verify that Mac OSX is loading the libxml2 library in its bootstrap by disabling libxml2 found in /usr/lib. Do something like copying all versions of libxml2*.dylib to libxml2*.dylib.old (on my machine this was libxml2.2.7.3, libxml2.2 and libxml2).

After you have done this, running Nokogiri will not produce any errors. That is because it can’t find the loaded libxml2 and will now follow the environment path, eventually finding libxml2.2.7.8 in /opt/local.

BUT you won’t be able to copy the old libxml files back. This is because the OS needs the libxml2 that was loaded in the bootstrap.

Powering off and powering on again will brick your machine. The login screen will hang and hang and hang. Power off and power on again in single-user mode (hold Command-S while rebooting). You can watch the bootstrap occur. Low and behold, it throws an error that it can’t load libxml2 and then stops working.

Power off and power on again. This time boot into recovery mode (either hold Command-R or hold Option and then select the recovery disk). In recovery mode open the terminal (utilities/terminal). Mount /usr/lib on your HD (try /Volumes/Macintosh\ HD/usr/lib) and copy the libxml2 files back. Reboot and all will be fine.

7
  • 1
    heh...your instructions to split the build options between /usr and /opt/local (MacPorts) did it for me ... thanks Dec 13, 2011 at 3:01
  • Glad to see someone smarter than me also fell afoul of the Apple Bootstrap libxml2 nightmare :)
    – Kenton
    Mar 19, 2012 at 19:21
  • I just stumbled across this post randomly and had no use for it, but this detailed answer deserves many upvotes for the effort! Aug 29, 2012 at 14:39
  • 2
    I used a variation on this solution (where my dynamically loaded version was 2.9.0): bundle config build.nokogiri --with-xml2-include=/usr/local/Cellar/libxml2/2.9.0/include/libxml2/
    – sberkley
    Apr 2, 2013 at 19:13
  • Thanks to @sberkley for the updated tip. I noticed, when I ran it, that if you do it at ~, it seems to set this value globally. So perhaps you can just do this once and never have to worry about it again.
    – duma
    Apr 30, 2013 at 14:20
28

None of this worked for me.

I had libxml2 installed at a later version (2.7.8) with brew. This caused nokogiri to compile against it and the later problems. Solution, remove it, then build, then install if desired.

Here's what worked:

  • brew uninstall libxml2 (if previously installed)
  • gem uninstall nokogiri
  • gem install nokogiri
  • brew install libxml2 (optional)
7
  • 3
    This fixed it for me on OS X 10.8.2 with libxml 2.7.8 / 2.8.0. Just removing the nokogiri gem and reinstalling it did not solve it, but brew uninstall libxml2 beforehand did. thanks! Feb 17, 2013 at 23:02
  • 1
    perfect answer if using homebrew
    – awenkhh
    Apr 16, 2013 at 21:16
  • +1 Perfect. The key is to NOT have any other version of libxml2 installed when you install nokogiri.
    – Nate Cook
    Jun 15, 2013 at 15:40
  • Also, just in case anyone else is RubyMine, this is what I helped me fixed the error I was getting with bundle install from within RubyMine. You can see the same error in the command line if you type rake --tasks
    – Nate Cook
    Jun 15, 2013 at 15:43
  • This was the correct solution for me. I'm running Mountain Lion and am using homebrew (and Intellij IDEA).
    – duma
    Jul 11, 2013 at 15:02
16

The solution (for me) after updating to Mountain Lion was much simplier:

gem uninstall nokogiri
# (and ignore the warnings about dependencies)
gem install nokogiri
4
  • 4
    Thanks for the feedback, Old Pro. I encourage you to consider re-reading the SO Privileges Q&A. "Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect." This answer clearly worked for several people including the OP so I don't think it warrants a downvote.
    – IAmNaN
    Mar 12, 2013 at 21:39
  • 1
    Due to the recently discovered cpu consumption DOS vulnerability (vuxml.org/freebsd/843a4641-9816-11e2-9c51-080027019be0.html) we updated libxml2 on FreeBSD, causing the nokogiri warning in question. Reinstalling as described, solved the issue.
    – user569825
    Apr 10, 2013 at 18:18
  • Worked for me on OSX 10.8.5 Jan 17, 2014 at 0:58
  • 1
    No, @Tass. update simply checks if there is a new version of the gem, which there isn't in this case. It's been a more than two years but I seem to remember without running uninstall, update was a NOP. I suspect the version of nokogiri doesn't change but the libs need to compiled with different headers after upgrading the OS.This method, by the way, has worked after several OSX updates now.
    – IAmNaN
    Aug 15, 2014 at 18:06
9

As per the comment from patrickmcgraw above, simply putting nokogiri as the first entry in my Gemfile worked for me. I'm putting it as a separate answer because the original comment has been buried.

source 'http://rubygems.org'
gem 'nokogiri'
gem 'rails', '3.0.20'
etc...
2
  • 1
    For an explanation of why this works, see this answer by @Jarrett
    – Jared Beck
    Sep 28, 2013 at 23:53
  • Yeah! Helps me with 'WARNING: Nokogiri was built against LibXML version 2.8.0, but has dynamically loaded 2.7.8' Nov 24, 2013 at 7:53
4

Bundler has options to set the default build locations. So for instance, with libxml2 installed via macports:

$ bundle config build.nokogiri --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib --with-xslt-dir=/opt/local

After doing this and bundle install, the warning went away.

There's also some helpful examples for setting build options on the nokogiri wiki.

4

Looks like you have updated your system libraries after installing the gem, so you have to update Nokogiri. To use the current lib version:

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

I had similar problem and just solved this way:

In my case, I have RVM installed, and I had @global and @project gem sets. Both of them had nokogiri installed and one of them had built with with different libxml.

Rebuilding both of them (I have reason to do this) solved the problem.

Hope this helps..

1
gem uninstall nokogiri
bundle  #install nokogiri again

If that fails with "libxml2 is missing." and you see gems/nokogiri-1.5.0/ext/nokogiri/mkmf.log trying to use "/usr/bin/gcc-4.2 ...", then you're missing /usr/bin/gcc-4.2

Solution:

sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2

Before:

$ ll /usr/bin/gcc*
lrwxr-xr-x  1 root  wheel  12 Jan 15 00:16 /usr/bin/gcc -> llvm-gcc-4.2

After:

$ ll /usr/bin/gcc*
lrwxr-xr-x  1 root  wheel  12 Jan 15 00:16 /usr/bin/gcc -> llvm-gcc-4.2
lrwxr-xr-x  1 root  wheel  12 Jan 15 21:07 /usr/bin/gcc-4.2 -> /usr/bin/gcc

If you're really missing libxml2 libxslt, then

brew update
brew install libxml2 libxslt
brew link libxml2 libxslt
bundle config build.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.26/
bundle

FYI: I'm running Mountain Lion with brew, and bundler.

1

gem install libxml-ruby helps me

0

I actually had 2 versions of libxml installed, one from source, one from an RPM.

The following is my complete solution

I uninstalled source (from the source directory)

sudo make uninstall

Remove bundles

rm -rf ~/.bundle ~/.bundler

Updated LD (might have to do this as root, not sudo)

sudo ldconfig

Then reinstalled the bundle

bundle install
0

Just ran into this myself (OS X Lion 10.7.5). My exact message was: Nokogiri was built against LibXML version 2.8.0, but has dynamically loaded 2.7.3

I tried a few suggestions mentioned here, none worked, but this did:

gem install nokogiri -- --with-xml2-dir=/usr --with-xslt-dir=/opt/local --with-iconv-dir=/opt/local

The explanation is: "This happens because the Lion system default libxml2 (loaded at bootstrap) is used, regardless of which libxml2 Nokogiri was built against."

Credits to: https://coderwall.com/p/o5ewia

0

OS : Maverick 10.9.3

Ruby 1.9.3

WARNING: Nokogiri was built against LibXML version 2.9.1, but has dynamically loaded 2.9.0

My solution:

gem uninstall nokogiri
brew update
cd /usr/local
brew versions libxml2
git checkout 5dd45d7 /usr/local/Library/Formula/libxml2.rb # libxml version 2.9.0
brew install libxml2
bundle install or gem install nokogiri -v "1.5.11"

Hope this help

0

If you have this message and your nokogiri is out-of-date with the version available from the gem source, simply run bundle update nokogiri to get the new code and recompile. Your error should go away.

0

OS: Catalina

Warning: warning nokogiri was built against libxml version 2.9.10 but has dynamically loaded 2.9.4

I followed Michiel de Mare steps, but brew install libxml2 --with-xml2-config failed with invalid option error. So I installed libxml2 and libxslt and took note of the output from both commands.

 brew install libxml2                   
==> Downloading https://homebrew.bintray.com/bottles/libxml2-2.9.10_2.catalina.bottle.tar.gz
Already downloaded: /Users/alberto/Library/Caches/Homebrew/downloads/9ddf5cb90fd16a7eb531e37bb748fd392f30214d9fe1568b2b70d28cc368c8f7--libxml2-2.9.10_2.catalina.bottle.tar.gz
==> Pouring libxml2-2.9.10_2.catalina.bottle.tar.gz
==> Caveats
libxml2 is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libxml2 first in your PATH run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.zshrc

For compilers to find libxml2 you may need to set:
  export LDFLAGS="-L/usr/local/opt/libxml2/lib"
  export CPPFLAGS="-I/usr/local/opt/libxml2/include"

For pkg-config to find libxml2 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/libxml2/2.9.10_2: 280 files, 10.6MB

brew install libxslt
==> Downloading https://homebrew.bintray.com/bottles/libxslt-1.1.34.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/cbadecf3186f45754220dff4cbdfbb576882a211d615b52249a4c9d8ba4d7c3a?response-content-disposition=attachment%3Bfil
######################################################################## 100.0%
==> Pouring libxslt-1.1.34.catalina.bottle.tar.gz
==> Caveats
To allow the nokogiri gem to link against this libxslt run:
  gem install nokogiri -- --with-xslt-dir=/usr/local/opt/libxslt

libxslt is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libxslt first in your PATH run:
  echo 'export PATH="/usr/local/opt/libxslt/bin:$PATH"' >> ~/.zshrc

For compilers to find libxslt you may need to set:
  export LDFLAGS="-L/usr/local/opt/libxslt/lib"
  export CPPFLAGS="-I/usr/local/opt/libxslt/include"

For pkg-config to find libxslt you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libxslt/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/libxslt/1.1.34: 136 files, 2.8MB

And I used those directories when configuring bundle for nokogiri

bundle config build.nokogiri --with-xml2-include=/usr/local/opt/libxml2/include --with-xml2-lib=/usr/local/opt/libxml2/lib --with-xslt-dir=/usr/local/opt/libxslt

To summarize I executed these steps

gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
brew install libxml2
brew install libxslt
bundle config build.nokogiri --with-xml2-include=/usr/local/opt/libxml2/include --with-xml2-lib=/usr/local/opt/libxml2/lib --with-xslt-dir=/usr/local/opt/libxslt
bundle install

Your Answer

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

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