Skip to content

Instantly share code, notes, and snippets.

@cbetta
Created November 25, 2011 17:00
Show Gist options
  • Save cbetta/1393974 to your computer and use it in GitHub Desktop.
Save cbetta/1393974 to your computer and use it in GitHub Desktop.
How to fix a segfault for Nokogiri on Ruby 1.8.7 on OS X
# First install libxslt and libxml2 using Homebrew
brew install libxslt
brew install libxml2
# Now rebuild nokogiri with these new libs (make sure to update version numbers if your homebrew installed a different version)
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.7.8/include --with-xml2-lib=/usr/local/Cellar/libxml2/2.7.8/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/
@caius
Copy link

caius commented Dec 6, 2011

$ gem install nokogiri -- --with-xml2-include="$(brew --prefix libxml2)/include" --with-xml2-lib="$(brew --prefix libxml2)/lib" --with-xslt-dir="$(brew --prefix libxslt)"

@cbetta
Copy link
Author

cbetta commented Dec 6, 2011

Show off :P

@cbetta
Copy link
Author

cbetta commented Jan 25, 2012

Ok, so here is a little other thing I figured out.

When you have done this correct and then run nokogiri -v you will see something like:

# Nokogiri (1.5.0)
    --- 
    warnings: []

    ruby: 
      engine: mri
      version: 1.8.7
      description: ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin11.2.0]
      platform: i686-darwin11.2.0
    libxml: 
      loaded: 2.7.8
      binding: extension
      compiled: 2.7.8
    nokogiri: 1.5.0

Sadly, even if Nokogiri is nicely compiled against and loads libxml2 2.7.8, it might still give a warning in the form of something like:

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

What this means is that a different gem has decided to also load LibXML, and when it loaded it it only knew about 2.7.3. Nokogiri is a bit lazy, and it will force itself to use 2.7.3 if this was already loaded by a different gem.

The solution? For me the problem was solved by explicitly adding gem 'nokogiri' to the start of my Gemfile, in front of all my other gems. This will make sure that Nokogiri gets loaded first and has first dibs on loading LibXML.

@caius
Copy link

caius commented Feb 6, 2012

What I suggested (--with-xml2-dir) doesn't work, as the include directory of libxml2 as installed by homebrew is nested, not just include/. I also had to install libiconv and supply --with-libiconv-dir for it to both compile and link against libxml2 2.7.8.

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