【rails】mysql2をインストール時に遭遇したエラー
はじめに
railsでgemmysql2
をインストール時に遭遇したエラーの解決法。
エラー遭遇
railsでbundle install
を実行したときに下記エラーに遭遇。
Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /Users/name/workspace/search_for_bugs_at_home/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/ext/mysql2 /Users/name/.rbenv/versions/2.6.6/bin/ruby -I /Users/name/.rbenv/versions/2.6.6/lib/ruby/site_ruby/2.6.0 -r ./siteconf20210601-32665-2hsbr1.rb extconf.rb checking for rb_absint_size()... yes checking for rb_absint_singlebit_p()... yes checking for rb_wait_for_single_fd()... yes checking for -lmysqlclient... no ----- mysql client is missing. You may need to 'brew install mysql' or 'port install mysql', and try again. ----- *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=/Users/name/.rbenv/versions/2.6.6/bin/$(RUBY_BASE_NAME) --with-mysql-dir --without-mysql-dir --with-mysql-include --without-mysql-include=${mysql-dir}/include --with-mysql-lib --without-mysql-lib=${mysql-dir}/lib --with-mysql-config --without-mysql-config --with-mysql-dir --without-mysql-dir --with-mysql-include --without-mysql-include=${mysql-dir}/include --with-mysql-lib --without-mysql-lib=${mysql-dir}/lib --with-mysqlclientlib --without-mysqlclientlib To see why this extension failed to compile, please check the mkmf.log which can be found here: /Users/name/workspace/search_for_bugs_at_home/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-19/2.6.0/mysql2-0.5.3/mkmf.log extconf failed, exit code 1 Gem files will remain installed in /Users/name/workspace/search_for_bugs_at_home/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3 for inspection. Results logged to /Users/name/workspace/search_for_bugs_at_home/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-19/2.6.0/mysql2-0.5.3/gem_make.out An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue. Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling. In Gemfile: mysql2
そもそもmacにmysqlが入っていなかったのでエラーの指示通り、下記コマンドでインストール
$ brew install mysql
もう一度bundle install
Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /Users/name/workspace/search_for_bugs_at_home/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/ext/mysql2 /Users/name/.rbenv/versions/2.6.6/bin/ruby -I /Users/name/.rbenv/versions/2.6.6/lib/ruby/site_ruby/2.6.0 -r ./siteconf20210601-37880-18rlp5j.rb extconf.rb checking for rb_absint_size()... yes checking for rb_absint_singlebit_p()... yes checking for rb_wait_for_single_fd()... 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 checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes checking for my_bool in mysql.h... no ----- 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.25_1/lib ----- creating Makefile current directory: /Users/name/workspace/search_for_bugs_at_home/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/ext/mysql2 make DESTDIR\= clean current directory: /Users/name/workspace/search_for_bugs_at_home/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/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/name/workspace/search_for_bugs_at_home/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3 for inspection. Results logged to /Users/name/workspace/search_for_bugs_at_home/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-19/2.6.0/mysql2-0.5.3/gem_make.out An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue. Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling. In Gemfile: mysql2
すると今度は別のエラーが発生。
ld: library not found for -lssl
sslライブラリが見つけられないみたい。
解決法
ライブラリの場所を指定する。
下記コマンドで指定場所を確認。
$ brew info openssl openssl@1.1: stable 1.1.1k (bottled) [keg-only] Cryptography and SSL/TLS Toolkit https://openssl.org/ /usr/local/Cellar/openssl@1.1/1.1.1k (8,071 files, 18.5MB) Poured from bottle on 2021-03-30 at 21:39:47 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. If you need to have openssl@1.1 first in your PATH, run: echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc For compilers to find openssl@1.1 you may need to set: export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib" export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include" For pkg-config to find openssl@1.1 you may need to set: export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig" ==> Analytics install: 674,157 (30 days), 2,468,696 (90 days), 8,763,653 (365 days) install-on-request: 49,412 (30 days), 194,997 (90 days), 1,107,297 (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
完了!