eventmachine gemのインストールができなかったのでXcodeを入れなおしたときの記録

エンジニアのにのみやです。

bundle installでeventmachineのインストールがコケていろいろ大変だったので、同様のエラーでお困りの人の役に立てることを願いつつ記録として残しておきます。

概要

eventmachine gemのインストールでエラー発生

Xcodeを再インストール

⇒ Command Line Tools for Xcodeを再インストール

⇒ 解決

OSはMac OS X 10.9.1 (Mavericks), Rubyのバージョンは2.0.0p247です。

エラー内容

bundle installの途中で発生したエラーは以下です。

Installing eventmachine (1.0.3) 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /Users/ninomiya/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb 
*** 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/ninomiya/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
    --with-openssl-config
    --without-openssl-config
    --with-pkg-config
    --without-pkg-config
/Users/ninomiya/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:430:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /Users/ninomiya/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:515:in `try_link0'
    from /Users/ninomiya/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:530:in `try_link'
    from /Users/ninomiya/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:616:in `block in try_ldflags'
    from /Users/ninomiya/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:609:in `with_ldflags'
    from /Users/ninomiya/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:615:in `try_ldflags'
    from /Users/ninomiya/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:1712:in `pkg_config'
    from extconf.rb:61:in `'

Gem files will remain installed in /Users/ninomiya/.rvm/gems/ruby-2.0.0-p247@buyma/gems/eventmachine-1.0.3 for inspection.
Results logged to /Users/ninomiya/.rvm/gems/ruby-2.0.0-p247@buyma/gems/eventmachine-1.0.3/ext/gem_make.out

An error occurred while installing eventmachine (1.0.3), and Bundler cannot
continue.
Make sure that `gem install eventmachine -v '1.0.3'` succeeds before bundling.

gemインストール時のエラーとしてよくあるextconf.rb failedというやつです。

gem_make.outにログがあるよと書いてありますが、コンソール以上に有用な情報は見当たりません。

extconf.rbのオプションがいろいろと提示されていますが、ここらへんのオプション指定の問題というよりも、You have to install development tools first.とあるように開発環境の問題と思われました。

錯誤(1) gcc最新版をインストール

実は前機のSnow Leopardから現機に乗換えたときもiconvやら何やらでトラブってMacPortsをアンインストールしたり等開発環境をいじりまくったことがありました。そこで開発環境の要であるgccを最新版にしてみました。

brew search gccとやるとgcc49が最新です。インストール方法も表示されるのでそれに従います。リンクは自分で作成しないといけないようです。

brew tap homebrew/versions
brew install gcc49
ln -s /usr/local/bin/gcc-4.9 /usr/local/bin/gcc
ln -s /usr/local/bin/g++-4.9 /usr/local/bin/g++

そして再度bundle installしましたが同じエラーが発生しました。

錯誤(2) brew doctor

Homebrew周りがいけないのかと思いbrew doctorで出てきた警告をいくつかつぶしてみました。

まず、configスクリプトが実行パスに入っているという警告

Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and what additional flags to use when
compiling and linking.

とXQuartzが古いという警告

Warning: Your XQuartz (2.7.4) is outdated
Please install XQuartz 2.7.5:
  https://xquartz.macosforge.org

は関係なさそうだったので無視しました。

Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected dylibs:
    /usr/local/lib/libtcl8.6.dylib
    /usr/local/lib/libtk8.6.dylib

といった余計なファイルの存在に対する警告がいくつか出ていたのでひととおり削除してみました。 随時bundle installしますが結果は変わりません。

/usr/local以下の所有者や権限も使っていくうちになぜか狂っていくので、このタイミングで修正しましたが効果ありませんでした。

Xcode再インストール

Xcodeをアップデートしようとしましたができなかったので再インストールしました。

Xcodeに紐付いたApple IDを変更したかったのでキーチェーンアクセス内のdaw2.apple.comという項目を削除したりしましたが変更できません。App Storeに紐付いているアカウントは変えてあったのですが。

Xcodeの削除にはAppCleanerというツールを使いました。そしてXcode 5.0.2をApp Storeからダウンロードし直します。Apple IDを変更しての再インストールは問題なくできたものの、やはりeventmachineは入りません。

Command Line Tools for Xcode再インストール

次にhttps://developer.apple.com/downloads/index.actionでCommand Line Tools for Xcodeを探してインストールしました。

Command Line Tools

再度bundle installしたところeventmachineを含め全てのgemをインストールすることができました。

Macの開発環境整備について

XcodeとCommand Line Toolsを入れないと開発環境・ツールが整わないのは不便だと感じました。Homebrewでgcc, autoconf, ...をひたすら入れて行くのは大変だしどこまで不備なくできるかも不明です。GUIでインストールするのも疑問です。余計なものも入ってしまいますが、yumだったらyum groupinstall "Development Tools"で済むので楽ですね。