bundle installしたらMySQLのエラーでハマった時の対処法

bundle installしたらMySQLのところでエラーが発生したので、その解決方法をまとめました。

$ bundle install
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory: /Users/asakura1041/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10/ext/mysql2
/Users/asakura1041/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20211009-88199-1k3s2zg.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/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/Cellar/mysql/8.0.26/lib
creating Makefile

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

current directory: /Users/asakura1041/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10/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/asakura1041/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10 for inspection.
Results logged to /Users/asakura1041/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-19/2.5.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' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:



$ brew install openssl


$ brew info openssl

openssl@3: stable 3.0.0 (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
/usr/local/Cellar/openssl@3/3.0.0 (6,415 files, 28.2MB) Poured from bottle on 2021-10-09 at 00:37:45 From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@3.rb License: Apache-2.0 ==> 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@3/certs and run /usr/local/opt/openssl@3/bin/c_rehash openssl@3 is keg-only, which means it was not symlinked into /usr/local, because macOS provides LibreSSL. If you need to have openssl@3 first in your PATH, run: echo 'export PATH="/usr/local/opt/openssl@3/bin:$PATH"' >> ~/.zshrc For compilers to find openssl@3 you may need to set: export LDFLAGS="-L/usr/local/opt/openssl@3/lib" export CPPFLAGS="-I/usr/local/opt/openssl@3/include" For pkg-config to find openssl@3 you may need to set: export PKG_CONFIG_PATH="/usr/local/opt/openssl@3/lib/pkgconfig" ==> Analytics install: 46,902 (30 days), 46,914 (90 days), 46,920 (365 days) install-on-request: 37,771 (30 days), 37,783 (90 days), 37,789 (365 days) build-error: 0 (30 days)


export LDFLAGS="-L/usr/local/opt/openssl@3/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@3/include"


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

再度bundle installすると成功しました。

$ bundle install
Using rubocop-performance 1.3.0
Using rubocop-rspec 1.33.0
Using rubyzip 2.3.0
Using shoulda-matchers 4.0.1
Using simple_form 4.1.0
Using slack-ruby-client 0.14.2
Using smarter_csv 1.2.6
Using spring 2.0.2
Using spring-watcher-listen 2.0.1
Using therubyracer 0.12.3
Using timecop 0.9.1
Using uglifier 4.1.20
Using web-console 3.7.0
Using whenever 0.10.0
Using wicked_pdf 2.0.2
Using wkhtmltopdf-binary
Bundle complete! 83 Gemfile dependencies, 463 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.