74

I'm having troubles installing Nokogiri (1.6.8.1) on Mac OS Sierra 10.12.

I tried using brew install libxml2 libxslt and then referencing the install directories using command line options but it didn't help.

jwpfox
  • 5,124
  • 11
  • 45
  • 42
Julian Popov
  • 17,401
  • 12
  • 55
  • 81
  • If trying to install Nokogiri > v1.6.8.1 after installing Mac OS Sierra, I found that v1.7.1 will have problems if that's the first version being installed with that version of Ruby. Installing v1.6.8.1 then updating using `gem update nokogiri` through the intervening versions until v1.7.1 allowed me to get it installed. It's a hack but worked and once I had it installed I didn't feel like digging in more. – the Tin Man Mar 20 '17 at 21:11

9 Answers9

181

Open Xcode and, from the menu XCode -> Preferences update your Command Line Tools (Xcode 8.0).

Then do:

bundle config build.nokogiri --use-system-libraries=true --with-xml2-include="$(xcrun --show-sdk-path)"/usr/include/libxml2
bundle install

or just:

gem install nokogiri -v 1.6.8.1 -- --use-system-libraries=true --with-xml2-include="$(xcrun --show-sdk-path)"/usr/include/libxml2
rgov
  • 3,516
  • 1
  • 31
  • 51
Julian Popov
  • 17,401
  • 12
  • 55
  • 81
  • 9
    The answer by NDan is simpler – Sanjay Oct 27 '16 at 07:38
  • Thanks, really helpful – pkrawat1 Jan 07 '17 at 10:21
  • New versions of Mac OS require Xcode to be updated, which invalidates the command-line tools, so they need to be reinstalled. Nokogiri is bundled with its own version of libxml2, which helps on many systems, but sometimes gem needs to be told where to find things. gem actually will do the right thing once Xcode is updated. – the Tin Man Mar 20 '17 at 20:45
  • +1 Great! It works also if you have XCode 9.x. Here you don't need to update command line tools; if not updated just open XCode and it will take care of it. – gsscoder Mar 12 '18 at 14:41
  • 2
    this worked for me while the supposedly simpler answer mentioned in the comments didn't – Cpt. Senkfuss Jun 29 '18 at 14:41
126

The more simple solution is to execute:

xcode-select --install
gem install nokogiri

Update

For Mojave I'm using gem install nokogiri -v '1.6.6.2' -- --use-system-libraries

NDan
  • 2,132
  • 1
  • 15
  • 12
47

Try install libxml2 first with Homebrew.

brew install libxml2

Then if installing with bundle

bundle config build.nokogiri --use-system-libraries \
  --with-xml2-include=$(brew --prefix libxml2)/include/libxml2
bundle install

If installing directly with gem

gem install nokogiri -- --use-system-libraries \
  --with-xml2-include=$(brew --prefix libxml2)/include/libxml2
Quanlong
  • 24,028
  • 16
  • 69
  • 79
11

This might be a duplicate of gem install nokogiri -v '1.6.8.1' fails ... latest accepted answer there was to do:

brew unlink xz; bundle install; brew link xz

Re-linking xz might not be necessary ... if for example you only have that dependency because of the_silver_searcher (which links directly against the non-symlinked library).

Community
  • 1
  • 1
Orangenhain
  • 3,724
  • 1
  • 22
  • 16
11
gem update --system
xcode-select --install
brew unlink xz
gem install nokogiri -v '1.6.8.1'
brew link xz

If the above steps fail to fix the issue, what has also worked for me is running brew doctor and cleaning up any unbrewed header files.

yuяi
  • 2,617
  • 1
  • 23
  • 46
  • This is what worked for me. For some reason 'bundle update' would fail. I had to install nokogiri as suggested in this answer. – Nick Kanellopoulos Jan 05 '17 at 19:41
  • I had previously installed Nokogiri just a few months ago, but changed Ruby versions and install of 1.8.0 was failing. I was able to install it via the unlink xz / gem isntall pattern (without gem update or xcode select) – SciPhi Sep 05 '17 at 16:43
10

Similar to yuяi's answer and from Nokogiri's help:

brew unlink xz
gem install nokogiri
brew link xz

http://www.nokogiri.org/tutorials/installing_nokogiri.html#mac_os_x

This worked for 1.7.0.1

Rimian
  • 36,864
  • 16
  • 117
  • 117
1

For Middleman

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

lraboteau
  • 71
  • 2
  • 5
0

If you have earlier installed nokogiri using bundler with bundle config build.nokogiri --use-system-libraries this setting will still be there. If you then meanwhile have solved your Xcode setup (as suggested as the primary method of installation in the nokogiri installation) then you might wanna try removing that fra from bundler bundle config --delete build.nokogiri and try again doing bundle install.

Niels Kristian
  • 8,661
  • 11
  • 59
  • 117
0

First install XCode from the app store

then run

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

then run

bundle install

this should work

Ankit
  • 91
  • 2
  • 7