Skip to content

Instantly share code, notes, and snippets.

@SteveBenner
Last active August 13, 2019 14:52
  • Star 26 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save SteveBenner/de51738222e92d606487 to your computer and use it in GitHub Desktop.
Mac fix - Install Nokogiri gem on OS X 10.9 Mavericks
#!/usr/bin/env ruby
#
# Mac fix 1 - Install the Nokogiri gem on Mac OS 10.9 Mavericks
#
# Usage: to configure and install using Bundler, pass in 'bundle' as an argument to the script.
#
# Nokogiri works at a very low level, so it has many issues on various platforms.
# As a result, the command `install gem nokogiri` often will fail. This fix is for
# errors involving 'libiconv', such as the following one I encountered:
#
# > libiconv is missing. please visit http://nokogiri.org/tutorials/installing_nokogiri.html
# > for help with installing dependencies.
# > -----
# > *** 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.
#
# This script is basically just carrying out the install process illustrated on the
# Nokogiri website, specifically for OS X 10.9. Other systems are not covered.
# More information can be found at: http://nokogiri.org/tutorials/installing_nokogiri.html
#
# Some additional resources I came across while troubleshooting this issue:
# - http://stackoverflow.com/questions/5528839/why-does-installing-nokogiri-on-mac-os-fail-with-libiconv-is-missing
# - http://stackoverflow.com/questions/23401174/nokogiri-gem-fails-to-install-in-os-x-mavericks
# - http://stackoverflow.com/questions/19643153/error-to-install-nokogiri-on-osx-10-9-maverick
# - http://jasdeep.ca/2013/10/installing-nokogiri-fails-os-x-mavericks/
#
# NOTE:
# There are many factors involved in Nokogiri's installation. This script was tested
# on a system with the following characteristics and installations:
#
# - Mac OS X 10.9.4
# - 'xcode-select' installed
# - Homebrew 0.9.5
# - Ruby 2.1.2
# - Nokogiri 1.6.3.1
# There are three libraries required to compile Nokogiri's native extensions
libs = %w[libxml2 libxslt libiconv]
# Install and link them using Homebrew
`brew update`
%w[install link].each { |command| `brew #{command} #{libs.join(' ')}` }
# Use the latest versions of installed libraries from your Homebrew installation
paths = libs.inject({}) do |libs, lib|
# It's easy to find the library paths using the 'brew' executable, but keep in mind
# they are just symlinks; the real installation path may vary from system to system.
libs[lib.to_sym] = File.realpath `brew --prefix #{lib}`.chomp
libs
end
# If you have any Nokogiri gem files, they need to be removed obviously
`gem uninstall nokogiri`
# Install Nokogiri gem using either RubyGems or Bundler (defaults to RubyGems)
flags = [
'--',
"--with-xml2-dir=#{paths[:libxml2]}",
"--with-xslt-dir=#{paths[:libxslt]}",
"--with-iconv-dir=#{paths[:libxml2]}",
"--with-xml2-config=#{paths[:libxml2]}/bin/xml2-config",
"--with-xslt-config=#{paths[:libxslt]}/bin/xslt-config"
]
if ARGV[0] == 'bundle'
`bundle config build.nokogiri #{flags.drop(1).join(' ')}`
print `bundle exec install nokogiri`
else
print `gem install nokogiri #{flags.join(' ')}`
end
puts
puts "Mac 'Nokogiri installation' fix applied successfully! Latest version at:"\
"https://gist.github.com/SteveBenner/de51738222e92d606487"
@krzysztofmajewski
Copy link

Thanks Steve Benner, this (mostly) worked for me. I had to add --use-system-libraries and --with-xml2-include=/usr/local/opt/libxml2/include/libxml2 after the --, and I skipped the brew link [library].

The full command was:

sudo gem install nokogiri -- --use-system-libraries --with-xslt-dir=/usr/local/opt/libxslt --with-iconv-dir=/usr/local/opt/libiconv --with-xml2-dir=/usr/local/opt/libxml2 --with-xml2-config=/usr/local/opt/libxml2/bin/xml2-config --with-xml2-include=/usr/local/opt/libxml2/include/libxml2 --with-xslt-config=/usr/local/opt/libxslt/bin/xslt-config

@ramijames
Copy link

@krzysztofmajewski this was a working command for El Capitan. Thanks for that.

@visoft
Copy link

visoft commented Aug 13, 2019

Thanks @krzysztofmajewski! Worked great for install Nokogiri 1.6.6.2 (just had to add the version number to the command nokogiri -v 1.6.6.2

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