Edited at

nokogiriのインストールエラーはXcode command line tools更新後に規約同意をしていないのが原因かも

More than 3 years have passed since last update.

ある時、Mac OS X Yosemite (10.10.5)でnokogiriのインストールに失敗しました。

ターミナル出力(後述)にはYou have to install development tools first.とあります。

つい最近までインストールできていたから開発環境が無いわけがないのに・・・と思っていたら、直前にXcode command line toolsの更新をしていたのが原因でした。規約同意していないのでxcodebuildが使えなかったんですね。

$ sudo xcodebuild -license

By typing 'agree' you are agreeing to the terms of the software license agreements.
Type 'print' to print them or anything else to cancel, [agree, print, cancel] agree



$ bundle

Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies........
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.8.3
Using thread_safe 0.3.5
Using tzinfo 1.2.2
Using activesupport 4.2.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.0.0
Installing nokogiri with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

/Users/<user>/.rbenv/versions/2.2.3/bin/ruby -r ./siteconf20151226-4433-1t2wmhu.rb extconf.rb --use-system-libraries
checking if the C compiler accepts ... *** 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:
/Users/<user>/.rbenv/versions/2.2.3/lib/ruby/2.2.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/<user>/.rbenv/versions/2.2.3/lib/ruby/2.2.0/mkmf.rb:571:in `block in try_compile'
from /Users/<user>/.rbenv/versions/2.2.3/lib/ruby/2.2.0/mkmf.rb:522:in `with_werror'
from /Users/<user>/.rbenv/versions/2.2.3/lib/ruby/2.2.0/mkmf.rb:571:in `try_compile'
from extconf.rb:80:in `nokogiri_try_compile'
from extconf.rb:87:in `block in add_cflags'
from /Users/<user>/.rbenv/versions/2.2.3/lib/ruby/2.2.0/mkmf.rb:619:in `with_cflags'
from extconf.rb:86:in `add_cflags'
from extconf.rb:336:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /Users/<user>/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/nokogiri- for inspection.
Results logged to /Users/<user>/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-14/2.2.0-static/nokogiri-
An error occurred while installing nokogiri (, and Bundler cannot
Make sure that `gem install nokogiri -v ''` succeeds before bundling.