Rails のプロジェクトを扱っていて、
gem を bundle install していたところ mysql2 の部分でこんなエラーが…
Installing mysql2 0.4.4 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /Users/user_name/Programs/web/test-repo/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.4/ext/mysql2
/Users/user_name/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20161031-6252-1pfzkzr.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
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.16/lib
-----
creating Makefile
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Users/user_name/Programs/web/test-repo/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/mysql2-0.4.4/mkmf.log
current directory: /Users/user_name/Programs/web/test-repo/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.4/ext/mysql2
make "DESTDIR=" clean
current directory: /Users/user_name/Programs/web/test-repo/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.4/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
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/user_name/Programs/web/test-repo/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.4 for inspection.
Results logged to /Users/user_name/Programs/web/test-repo/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/mysql2-0.4.4/gem_make.out
An error occurred while installing mysql2 (0.4.4), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.4'` succeeds before bundling.
で、 gem install mysql2 -v '0.4.4' を試したらそれは正常に実行できるので困った。
エラーメッセージの途中に library not found for -lssl と書かれていることから
openssl のライブラリ周りで問題が起きているのはわかるのですが、openssl を再インストールしたところで状況は変わらず
なかなか困っていました。
解決法1
ここの記事が解決してくれました : RailsプロジェクトでMySQLがbundle installできなかった - Qiita
この記事に書かれているコマンドと少し異なるのですが、
bundle config build.mysql2 --with-opt-lib=/usr/local/opt/openssl/lib --with-opt-include=-I/usr/local/opt/openssl/include
で解消できました。
なお、bundler のグルーバル設定を変えたくない場合は --local のオプションを上記コマンドに付けるといいです。
解決法2(こっちのが良さそう)
違うやり方として、この記事 : Pry起動時にエラーが出る場合の対処法 - Qiita
を参考に、
CONFIGURE_OPTS="--with-readline-dir=/usr/local/opt --with-openssl-dir=/usr/local/opt" rbenv install 2.3.1
と、openssl のディレクトリを明示した上で Ruby のコンパイルをおこなった上で…という方法でもいけました。
解決法3(追記:2017/1/25)
コメントでいただきました。
現在は xcode-select --install でも対応できるようです。
最新の Xcode Command Line Tools では、この問題に対応しているんでしょうね。
というわけで、App Store で Xcode の最新版をインストールしたのち、
xcode-select --install
を実行してみてください。
(追記ここまで)
ついでに nokogiri も gem install に失敗してたけど、そちらはこの記事で助かりました。 : OSX への nokogiri 1.6.8.rc3 の install でハマったメモ - Qiita
たしかに以前 brew doctor で指摘されてるから unlink した覚えがある。
ってことで、
brew link --force libxml2
したあとに gem install nokogiri -- --use-system-libraries でインストール可能。
bundle config の方は以下のように設定。
bundle config build.nokogiri --use-system-libraries
これで mysql2 や nokogiri が Gemfile に含まれていても bundle install に成功するようになりました。
めでたしめでたし