mysql2のインストールではまる

Rails4.0も正式リリースされたとういうことで、早速アプリを作ろうとしたところ、
mysql2のインストールで予想にはまってしまったので、後々のためにメモしときます。


ちなみに環境は


この状態でDBにMySQLを利用するRailsのプロジェクトを作成して

$ bundle install

を実行すると、mysql2のインストールの部分で

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /usr/local/opt/rbenv/versions/2.0.0-p195/bin/ruby extconf.rb
    checking for rb_thread_blocking_region()... yes
    checking for rb_wait_for_single_fd()... yes
    checking for mysql.h... no
    checking for mysql/mysql.h... no
    -----
    mysql.h is missing.  please check your installation of mysql and try again.
    -----
    *** 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
            --without-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=/usr/local/opt/rbenv/versions/2.0.0-p195/bin/ruby
            --with-mysql-config
            --without-mysql-config


Gem files will remain installed in /Users/shizm/work/sample/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.11 for inspection.
Results logged to /Users/shizm/work/sample/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.11/ext/mysql2/gem_make.out

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

というようなエラーが発生しました。


エラーメッセージが

 「とりあえず gem install mysql2 が成功するか確認しろ」

って言ってるので言われるままにやってみるも、やっぱり失敗。


ということで、まずは

 --without-mysql-config

オプションのせいで MySQL の各種ファイルが参照できなくなってるんじゃ?
ってことで
MySQL関係のファイルは /usr/local/lib /usr/local/include とかに入ってるのは確認済みだから、これが原因じゃないのは明らかなんだけどね・・・)

$ which mysql_config
/usr/local/bin/mysql_config

でパスを確認して

gem install mysql2 -- --with-mysql-config=/usr/local/bin/mysql_config

とやってみるが、まだ同じエラーがでる。


こうなったらGoogle先生の力を借りるしかないってことで検索したら、ズバリな記事がありました。

Random Acts of Sentience: gem install mysql2 missing mysql.h on OS X


上の記事に書かれている通り /usr/local/bin/mysql_config の120行目辺りに定義されているパラメータを編集して

bundle install

を実行したら、無事に mysql2 のインストールに成功しました。