Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Getting rid of nokogiri segfaults

This readme is a mixture of everything I read on SO+nokogiri wiki, which ultimately worked out for me.

Here are the steps which worked for me to get rid of segfaults with Nokogiri 1.4.4, on both Lion and Snow Leopard, with Ruby 1.8.7 (patchlevel 334 and +).

First diagnose which version of libxml2 you're using:

bundle exec nokogiri -v

If you have 2.7.3 listed somewhere, you're in bad waters (known to segfault). Install this:

brew install libxml2
brew install libiconv
brew install libxslt

Don't miss a single one, or you'll still dynamically link with libxml2 2.7.3, bundled with the OS apparently).

Uninstall the existing nokogiri:

gem uninstall nokogiri

Tweak your building config for nokogiri and rebuild:

bundle config build.nokogiri --with-xml2-include=/usr/local/Cellar/libxml2/2.7.8/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.7.8/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26 --with-iconv-dir=/usr/local/Cellar/libiconv/1.14
bundle install

Be extra careful for missing - (eg: -with-xml2 instead of --with-xml2...) or bad path (double check these works for you - here with homebrew).

Then you can test again:

bundle exec nokogiri -v

You should see libxml 2.7.8 (not 2.7.3) listed as compiled and loaded.

No more segfaults on either Snow Leopard or Lion for me after that.

Good luck!

Copy link

simeonwillbanks commented Dec 14, 2011

Thanks for these instructions!

Copy link

jwarzech commented May 2, 2012

Thank you for your clear concise instructions!

Copy link

thbar commented May 2, 2012

you welcome guys :)

Copy link

NilsHaldenwang commented Jun 25, 2012

When I try to install libiconv brew says:

Error: No available formula for libiconv
Apple distributes libiconv with OS X, you can find it in /usr/lib.
Some build scripts fail to detect it correctly, please check existing
formulae for solutions.

bundle exec nokogiri -v says:

warnings: []
nokogiri: 1.4.7
  version: 1.9.3
  platform: x86_64-darwin11.3.0
  description: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.3.0]
  engine: ruby
  binding: extension
  compiled: 2.8.0
  loaded: 2.8.0

Still not working. :/

Copy link

simeonwillbanks commented Jun 26, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment