Rails5 gem mysql2 installエラー

# rails5 gem mysql2でのエラー


久々にローカル（Mac）でrubyのバージョンを上げて、アプリ作ろうと思った時に立ちはだかったエラーがあったので、どうやって解決したかを書いていきます。
手っ取り早く解決策を知りたい方はこのコマンドだけ実行してください

```
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
```

自分がエラーを解決するときにどのような思考を辿ったのかを書いておきます.

## 環境
```shell
Mac OS Mojave 10.14.2
Rails 5.0.7.1
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]
```

Railsのバージョンを指定せずに入れたら、中途半端なものが入ってしまった。


## エラー文
```shell
Fetching mysql2 0.3.20 (was 0.5.2)
Installing mysql2 0.3.20 (was 0.5.2) with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/Komatsu/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.3.20/ext/mysql2
/Users/Komatsu/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181227-62571-17mksz0.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.22/lib
-----
creating Makefile

current directory: /Users/Komatsu/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/Komatsu/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.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/Komatsu/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.3.20 for inspection.
Results logged to /Users/Komatsu/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0-static/mysql2-0.3.20/gem_make.out

An error occurred while installing mysql2 (0.3.20), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.20' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  mysql2
```


## ログの確認
色々エラーが出ているので、とりあえずログを見る。

```vim
current directory: /Users/Komatsu/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.3.20/ext/mysql2
/Users/Komatsu/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181227-61320-322pky.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.22/lib
-----
creating Makefile

current directory: /Users/Komatsu/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/Komatsu/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.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
```

まぁ同じことしか書いてない。
とりあえずmkmf.logも探してみる。
まずは場所の特定

```shell
$ find ~/.rbenv | grep mysql | grep mkmf.log
/Users/Komatsu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-17/2.3.0-static/mysql2-0.4.10/mkmf.log
/Users/Komatsu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-17/2.3.0-static/mysql2-0.5.1/mkmf.log
/Users/Komatsu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-17/2.3.0-static/mysql2-0.5.2/mkmf.log
/Users/Komatsu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-17/2.3.0-static/mysql2-0.3.18/mkmf.log
/Users/Komatsu/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0-static/mysql2-0.5.2/mkmf.log
/Users/Komatsu/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0-static/mysql2-0.3.21/mkmf.log
/Users/Komatsu/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0-static/mysql2-0.3.20/mkmf.log
```

バージョンを変えてトライした履歴が残っている。
とりあえずそのうちのひとつを見てみると…何もわからない…


## 原因仮説
最初のログを見ていると
`checking for rb_thread_blocking_region()... no`
`Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load`

とのこと、パスが通っていない？
opensslの問題？


## 状況の確認をする
opensslが消えてしまっている可能性を検討。

```shell
MacBook-Pro-lion:profy Komatsu$ openssl version
LibreSSL 2.6.5
MacBook-Pro-lion:profy Komatsu$  which openssl
/usr/bin/openssl
MacBook-Pro-lion:profy Komatsu$ brew info openssl
openssl: stable 1.0.2p (bottled) [keg-only]
SSL/TLS cryptography library
https://openssl.org/
/usr/local/Cellar/openssl/1.0.2o_1 (1,791 files, 12.3MB)
  Poured from bottle on 2018-06-05 at 18:55:48
/usr/local/Cellar/openssl/1.0.2o_2 (1,792 files, 12.3MB)
  Poured from bottle on 2018-07-18 at 18:42:36
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/openssl.rb
==> Dependencies
Build: makedepend ✘
==> Options
--without-test
	Skip build-time tests (not recommended)
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.
```

これではなさそう。


## 解決策
`xcode-select --install` を試し、最新の状態にするも解決せず.

以下のコマンドで解決しました。
他のアプリではうまくいっていたので、`--local `のオプションをつけて実行。

```shell
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
You are replacing the current local value of build.mysql2, which is currently nil
```

ちなみにこれをやると
`app/.bundle/config`
にファイルができます。

```
---
BUNDLE_BUILD__MYSQL2: "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

```

## 最後に
エラーの解決法はいたることろに書いてあるので、どのような思考手順でエラーを解決していったかを書いていこうかな、と考えております。