
Railsのプロジェクトでbundle installしたところ、mysql2のインストールでエラーになってしまったのでメモ。
- Ruby...2.4.3
- Rails...5.1.5
- mysql2
発生したエラー
Fetching mysql2 0.4.10
Installing mysql2 0.4.10 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /Users/hako/Project/sample_app/vendor/bundle/ruby/2.4.0/gems/mysql2-0.4.10/ext/mysql2
/Users/hako/.rbenv/versions/2.4.3/bin/ruby -r ./siteconf20210305-81784-1w9z9iw.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
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
checking for rb_big_cmp()... yes
-----
Using mysql_config at /usr/local/opt/mysql@5.7/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.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/opt/mysql@5.7/lib
-----
creating Makefile
current directory: /Users/hako/Project/sample_app/vendor/bundle/ruby/2.4.0/gems/mysql2-0.4.10/ext/mysql2
make "DESTDIR=" clean
current directory: /Users/hako/Project/sample_app/vendor/bundle/ruby/2.4.0/gems/mysql2-0.4.10/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/hako/Project/sample_app/vendor/bundle/ruby/2.4.0/gems/mysql2-0.4.10 for inspection.
Results logged to /Users/hako/Project/sample_app/vendor/bundle/ruby/2.4.0/extensions/x86_64-darwin-18/2.4.0/mysql2-0.4.10/gem_make.out
An error occurred while installing mysql2 (0.4.10), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.10'` succeeds before bundling.
In Gemfile:
mysql2
mysql2を使うときはmysqlクライアントを先にローカルにインストールしておかないといけない。
あらかじめHomebrewでインストールしてあり、versionは@5.7を使用している。
エラーの途中に
Using mysql_config at /usr/local/opt/mysql@5.7/bin/mysql_config
と出ているように、PATHは/usr/local/opt/mysql@5.7/binに通っている。
$ echo $PATH /usr/local/opt/imagemagick@6/bin:/usr/local/opt/mysql@5.7/bin:.....
mysql自体は問題なさそうだなー。
opensslがおかしい
MacでRailsにgem mysql2をインストールする時のトラブルシューティング - Qiita
こちらの記事によると、gemのビルド時に必要なopensslライブラリを見つけられないために発生しているらしい。
たしかにエラーの中に
ld: library not found for -lssl
こんなのがあった。
opensslのバージョンを確認
Homebrew経由でインストールされているopensslのバージョンを確認してみると
$ brew info openssl openssl@1.1: stable 1.1.1j (bottled) [keg-only] Cryptography and SSL/TLS Toolkit https://openssl.org/ /usr/local/Cellar/openssl@1.1/1.1.1h (8,067 files, 18.5MB) Poured from bottle on 2020-12-05 at 12:50:58 From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@1.1.rb License: OpenSSL ==> Caveats A CA file has been bootstrapped using certificates from the system keychain. To add additional certificates, place .pem files in /usr/local/etc/openssl@1.1/certs and run /usr/local/opt/openssl@1.1/bin/c_rehash openssl@1.1 is keg-only, which means it was not symlinked into /usr/local, because macOS provides LibreSSL. ==> Analytics install: 866,703 (30 days), 2,419,116 (90 days), 8,609,687 (365 days) install-on-request: 131,320 (30 days), 381,540 (90 days), 1,255,364 (365 days) build-error: 0 (30 days)
openssl@1.1の模様。
なんかよくわからないけど、「openssl@1.1 is keg-only, which means it was not symlinked into /usr/local」とあってシンボリックリンクが貼られていないような気がした。
調べたところ、brewがopensslにリンクを貼ってくれなくなったらしく手動で貼らないといけないらしい。
Homebrew brew linkしてもリンクしたopensslを使ってくれない。 - Qiita
結局こちらもパスが通っていないことが原因ぽい。
$ which openssl /usr/bin/openssl $ openssl version LibreSSL 2.8.3
たしかにopenssl@1.1をインストールしたはずなのに、LibreSSLというよくわからないものになっている。。
パスを通す
$ vim ~/.bash_profile export PATH="/usr/local/opt/openssl/bin:$PATH"
$ source ~/.bash_profile
$ which openssl /usr/local/opt/openssl/bin/openssl $ openssl version OpenSSL 1.1.1h 22 Sep 2020
ちゃんと1.1.1になった。
bundleにオプションを設定
先ほどと同じこちらの記事によると、
brew infoでインストールされているopensslを確認すると、自分の環境のLDFLAGSとCPPFLAGSのpathが確認できます。
とのことだけど、自分の場合は表示されない。。
$ brew info openssl@1.1 openssl@1.1: stable 1.1.1j (bottled) [keg-only] Cryptography and SSL/TLS Toolkit https://openssl.org/ /usr/local/Cellar/openssl@1.1/1.1.1h (8,067 files, 18.5MB) Poured from bottle on 2020-12-05 at 12:50:58 From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@1.1.rb License: OpenSSL ==> Caveats A CA file has been bootstrapped using certificates from the system keychain. To add additional certificates, place .pem files in /usr/local/etc/openssl@1.1/certs and run /usr/local/opt/openssl@1.1/bin/c_rehash openssl@1.1 is keg-only, which means it was not symlinked into /usr/local, because macOS provides LibreSSL. ==> Analytics install: 866,703 (30 days), 2,419,116 (90 days), 8,609,687 (365 days) install-on-request: 131,320 (30 days), 381,540 (90 days), 1,255,364 (365 days) build-error: 0 (30 days)
でも記事を見た感じ先ほど設定したパスと同じっぽかったのでこれで試してみる
$ bundle config --local build.mysql2 "--with-cppflags=-I/usr/local/opt/openssl@1.1/include --with-ldflags=-L/usr/local/opt/openssl@1.1/lib" $ bundle install
Using migration_comments 0.4.1 Using multi_xml 0.6.0 Fetching mysql2 0.4.10 Installing mysql2 0.4.10 with native extensions Using nested_form 0.3.2 Fetching oauth 0.5.5 Installing oauth 0.5.5 Fetching oauth2 1.4.4 Installing oauth2 1.4.4 Fetching page_title_helper 4.0.0 Installing page_title_helper 4.0.0
成功!!良かった〜。
参考
MacでRailsにgem mysql2をインストールする時のトラブルシューティング - Qiita