Installing Nokogiri on El Capitan

EDIT: This post has been updated and retitled for El Capitan.

 

If you have attempted to install the Nokogiri gem and encountered errors similar to the output below… I feel your pain and have a solution.

The Pain

➜ z gem install nokogiri Building native extensions. This could take a while... ERROR: Error installing nokogiri: ERROR: Failed to build gem native extension.
/Users/mariozig/.rvm/rubies/ruby-2.2.1/bin/ruby -r ./siteconf20150318-11691-1hlore5.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
checking for gzdopen() in -lz... yes
checking for iconv using --with-opt-* flags... yes
************************************************************************
IMPORTANT NOTICE:
Building Nokogiri with a packaged version of libxml2-2.9.2
with the following patches applied:
 - 0001-Revert-Missing-initialization-for-the-catalog-module.patch
 - 0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch
Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:
gem install nokogiri -- --use-system-libraries
 [--with-xml2-config=/path/to/xml2-config]
 [--with-xslt-config=/path/to/xslt-config]
If you are using Bundler, tell it to use the option:
bundle config build.nokogiri --use-system-libraries
 bundle install
Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************
Extracting libxml2-2.9.2.tar.gz into tmp/x86_64-apple-darwin14.1.0/ports/libxml2/2.9.2... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxml2/0001-Revert-Missing-initialization-for-the-catalog-module.patch...
Running 'patch' for libxml2 2.9.2... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxml2/0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch...
Running 'patch' for libxml2 2.9.2... OK
Running 'configure' for libxml2 2.9.2... OK
Running 'compile' for libxml2 2.9.2... OK
Running 'install' for libxml2 2.9.2... OK
Activating libxml2 2.9.2 (from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/x86_64-apple-darwin14.1.0/libxml2/2.9.2)...
************************************************************************
IMPORTANT NOTICE:
Building Nokogiri with a packaged version of libxslt-1.1.28
with the following patches applied:
 - 0001-Adding-doc-update-related-to-1.1.28.patch
 - 0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch
 - 0003-Initialize-pseudo-random-number-generator-with-curre.patch
 - 0004-EXSLT-function-str-replace-is-broken-as-is.patch
 - 0006-Fix-str-padding-to-work-with-UTF-8-strings.patch
 - 0007-Separate-function-for-predicate-matching-in-patterns.patch
 - 0008-Fix-direct-pattern-matching.patch
 - 0009-Fix-certain-patterns-with-predicates.patch
 - 0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch
 - 0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch
 - 0014-Fix-for-bug-436589.patch
 - 0015-Fix-mkdir-for-mingw.patch
Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:
gem install nokogiri -- --use-system-libraries
 [--with-xml2-config=/path/to/xml2-config]
 [--with-xslt-config=/path/to/xslt-config]
If you are using Bundler, tell it to use the option:
bundle config build.nokogiri --use-system-libraries
 bundle install
************************************************************************
Extracting libxslt-1.1.28.tar.gz into tmp/x86_64-apple-darwin14.1.0/ports/libxslt/1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0001-Adding-doc-update-related-to-1.1.28.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0003-Initialize-pseudo-random-number-generator-with-curre.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0004-EXSLT-function-str-replace-is-broken-as-is.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0006-Fix-str-padding-to-work-with-UTF-8-strings.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0007-Separate-function-for-predicate-matching-in-patterns.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0008-Fix-direct-pattern-matching.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0009-Fix-certain-patterns-with-predicates.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0014-Fix-for-bug-436589.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0015-Fix-mkdir-for-mingw.patch...
Running 'patch' for libxslt 1.1.28... OK
Running 'configure' for libxslt 1.1.28... ERROR, review '/Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ext/nokogiri/tmp/x86_64-apple-darwin14.1.0/ports/libxslt/1.1.28/configure.log' to see what happened.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
 --with-opt-dir
 --with-opt-include
 --without-opt-include=${opt-dir}/include
 --with-opt-lib
 --without-opt-lib=${opt-dir}/lib
 --with-make-prog
 --without-make-prog
 --srcdir=.
 --curdir
 --ruby=/Users/mariozig/.rvm/rubies/ruby-2.2.1/bin/$(RUBY_BASE_NAME)
 --help
 --clean
 --use-system-libraries
 --enable-static
 --disable-static
 --with-zlib-dir
 --without-zlib-dir
 --with-zlib-include
 --without-zlib-include=${zlib-dir}/include
 --with-zlib-lib
 --without-zlib-lib=${zlib-dir}/lib
 --enable-cross-build
 --disable-cross-build
/Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:279:in `block in execute': Failed to complete configure task (RuntimeError)
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `chdir'
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `execute'
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:66:in `configure'
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:109:in `cook'
 from extconf.rb:278:in `block in process_recipe'
 from extconf.rb:177:in `tap'
 from extconf.rb:177:in `process_recipe'
 from extconf.rb:487:in `<main>'
extconf failed, exit code 1
Gem files will remain installed in /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /Users/mariozig/.rvm/gems/ruby-2.2.1/extensions/x86_64-darwin-14/2.2.0/nokogiri-1.6.6.2/gem_make.out

The Problem

For some reason Apple’s Yosemite version of OSX does not have a system accessible installation of libxml2.  Nokogiri requires this in order to compile and luckily Xcode has a version of libxml2 bundled with it — we just need to specify it when installing the gem.  It’s important to get Nokogiri installed correctly because as of right now Rails 4.2.1.rc4 automatically attempts to install it and you will feel pain.

How I Solved It

gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2/