xurokawa-lab

雑多な雑記。お仕事ではRailsとかAndroidとかKotlinとか。

sierra移行後、bundle installでmysql2がコケた時の対処法

概要

OSがSierraになり、Railsアプリ作成時にbundle installでコケた。 答えに行き着くまでのちょっとした流れと、解決策の備忘録です。

アプリケーション作成時にエラー

今回rails new myapp -d mysqlmysqlを指定してアプリケーション作成時に、以下の様なエラーが発生しました。

-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql/5.7.13/lib
-----
creating Makefile

To see why this extension failed to compile, please check the mkmf.log which can be found here:

/Users/shiki/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-15/2.4.0-static/mysql2-0.4.9/mkmf.log

current directory: /Users/shiki/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.9/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/shiki/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.9/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
result.c:326:40: warning: incompatible pointer types assigning to 'my_bool *' (aka 'char *') from 'bool *'
[-Wincompatible-pointer-types]
    wrapper->result_buffers[i].is_null = &wrapper->is_null[i];
                                       ^ ~~~~~~~~~~~~~~~~~~~~
result.c:328:40: warning: incompatible pointer types assigning to 'my_bool *' (aka 'char *') from 'bool *'
[-Wincompatible-pointer-types]
    wrapper->result_buffers[i].error   = &wrapper->error[i];
                                       ^ ~~~~~~~~~~~~~~~~~~
2 warnings generated.
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/shiki/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.9 for
inspection.
Results logged to
/Users/shiki/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-15/2.4.0-static/mysql2-0.4.9/gem_make.out

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

In Gemfile:
  mysql2
         run  bundle exec spring binstub --all
Could not find gem 'mysql2 (< 0.5, >= 0.3.18)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

原因は何か探る

今回rbenvでrubyのバージョン指定を変更したからか?と思ったが、そのアプローチで調べてみたが答えにたどり着かず。 その後、Sierraに移行したために同じような問題が発生していたという記事を何件か発見。

解決法

直し方は数種類あるようでしたが、一番手っ取り早く直すならXcodeコマンドライン開発ツールを更新するのが良さそうでした。

xcode-select --install

その後、gem installで正常にインストールされたことを確認☺

rbenv exec gem install mysql2 -v '0.4.9'
Building native extensions.  This could take a while...
Successfully installed mysql2-0.4.9
Parsing documentation for mysql2-0.4.9
Installing ri documentation for mysql2-0.4.9
Done installing documentation for mysql2 after 0 seconds
1 gem installed

その後bundle installも正常に終了して問題解決。

終わりに

OS更新したことっていつの間にか頭から抜け落ちてて、rubyのバージョンの問題と目星を付けて調べ始めたので、正解に行き着くのに少し時間が掛かってしまった・・・ 移行ツールで持ってきた既存プロジェクトがずっと問題なく動いていたから尚更。


参考にさせていただきました。

nekonenene.hatenablog.com