Installing Nokogiri on Leopard

Nokogiri is a popular (and fast) XML parsing library for Ruby. One of the reasons that it's so fast is that it's written in C, and makes use of the excellent libxml2 and libxslt C libraries.

I just installed the Nokogiri Ruby gem on my Mac, which runs OS X 10.5.8 (i.e. Leopard). Imagine my surprise (given that I'm supposed to be running one of the most advanced operating systems in the world!) when Nokogiri printed this message in my terminal:

HI. You're using libxml2 version 2.6.16 which is over 4 years old and has
plenty of bugs. We suggest that for maximum HTML/XML parsing pleasure, you
upgrade your version of libxml2 and re-install nokogiri. If you like using
libxml2 version 2.6.16, but don't like this warning, please define the
constant I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 before
requring nokogiri.

Good one, Apple.

Aaron Kalin tells us how to solve this problem on his blog, by compiling new versions of libxml2 and libxslt from source.

I'd rather use MacPorts. Taking Aaron's advice and modifying it for ports, here's what you do:

$ sudo port install libxml2 libxslt -d
$ sudo gem install nokogiri -- \
    --with-xml2-include=/opt/local/include/libxml2 \
    --with-xml2-lib=/opt/local/lib

Yes, this post is largely here to remind me what to do next time this crops up. As you were...

I love feedback and questions — please feel free to get in touch on Mastodon or Twitter, or leave a comment.