Skip to content

Instantly share code, notes, and snippets.

@hiveer
Forked from SteveBenner/macfix1-install-nokogiri.rb
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiveer/7ba1f0d79df03867eae8 to your computer and use it in GitHub Desktop.
Save hiveer/7ba1f0d79df03867eae8 to your computer and use it in GitHub Desktop.
#!/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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment