前提
- macOS:13.0(22A380)
- ruby:3.1.4p223 (2023-03-30 revision 957bb7cb81) [arm64-darwin22]
- rails:7.0.4
- bundler:2.3.26
- mysql2:0.5.5
サマリー
- mysql2インストール時に、opensslの読み込み先が間違っているためエラーになる
- なので、読み込み先を指定してあげる必要がある
- 自分の場合は、opensslのリンク先の設定を行い、mysql2のビルドオプションの指定で解決した
作業ログ
mysql2をbundle installすると、
Fetching mysql2 0.5.5
Installing mysql2 0.5.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /Users/tommy_1592/Desktop/work/dev/chile/vendor/bundle/ruby/3.1.0/gems/mysql2-0.5.5/ext/mysql2
/Users/tommy_1592/.rbenv/versions/3.1.4/bin/ruby -I /Users/tommy_1592/.rbenv/versions/3.1.4/lib/ruby/3.1.0 extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_gc_mark_movable()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_enc_interned_str() in ruby.h... yes
-----
Using --with-openssl-dir=/Users/tommy_1592/.rbenv/versions/3.1.4/openssl
-----
*** 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/tommy_1592/.rbenv/versions/3.1.4/bin/$(RUBY_BASE_NAME)
--with-openssl-dir
--with-openssl-dir
--with-openssl-include
--without-openssl-include=${openssl-dir}/include
--with-openssl-lib
--without-openssl-lib=${openssl-dir}/lib
--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-mysqlclient-dir
--without-mysqlclient-dir
--with-mysqlclient-include
--without-mysqlclient-include=${mysqlclient-dir}/include
--with-mysqlclient-lib
--without-mysqlclient-lib=${mysqlclient-dir}/lib
--with-mysqlclientlib
--without-mysqlclientlib
/Users/tommy_1592/.rbenv/versions/3.1.4/lib/ruby/3.1.0/mkmf.rb:1083:in `block in find_library': undefined method `split' for nil:NilClass (NoMethodError)
paths = paths.flat_map {|path| path.split(File::PATH_SEPARATOR)}
^^^^^^
from /Users/tommy_1592/.rbenv/versions/3.1.4/lib/ruby/3.1.0/mkmf.rb:1083:in `each'
from /Users/tommy_1592/.rbenv/versions/3.1.4/lib/ruby/3.1.0/mkmf.rb:1083:in `flat_map'
from /Users/tommy_1592/.rbenv/versions/3.1.4/lib/ruby/3.1.0/mkmf.rb:1083:in `find_library'
from extconf.rb:131:in `<main>'
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Users/tommy_1592/Desktop/work/dev/chile/vendor/bundle/ruby/3.1.0/extensions/arm64-darwin-22/3.1.0/mysql2-0.5.5/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Users/tommy_1592/Desktop/work/dev/chile/vendor/bundle/ruby/3.1.0/gems/mysql2-0.5.5 for inspection.
Results logged to /Users/tommy_1592/Desktop/work/dev/chile/vendor/bundle/ruby/3.1.0/extensions/arm64-darwin-22/3.1.0/mysql2-0.5.5/gem_make.out
An error occurred while installing mysql2 (0.5.5), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.5' --source 'https://rubygems.org/'` succeeds before bundling.
In Gemfile:
mysql2
こちらを参考にして、mysqlが入っていないことが原因と推測
brew install mysql
もう一度試してみる
Fetching mysql2 0.5.5
Installing mysql2 0.5.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /Users/tommy_1592/Desktop/work/dev/chile/vendor/bundle/ruby/3.1.0/gems/mysql2-0.5.5/ext/mysql2
/Users/tommy_1592/.rbenv/versions/3.1.4/bin/ruby -I /Users/tommy_1592/.rbenv/versions/3.1.4/lib/ruby/3.1.0 extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_gc_mark_movable()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_enc_interned_str() in ruby.h... yes
-----
Using --with-openssl-dir=/Users/tommy_1592/.rbenv/versions/3.1.4/openssl
-----
-----
Using mysql_config at /opt/homebrew/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_DEFAULT_AUTH in mysql.h... yes
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 /opt/homebrew/Cellar/mysql/8.2.0_1/lib
-----
creating Makefile
current directory: /Users/tommy_1592/Desktop/work/dev/chile/vendor/bundle/ruby/3.1.0/gems/mysql2-0.5.5/ext/mysql2
make DESTDIR\= sitearchdir\=./.gem.20231229-56670-gi8fgb sitelibdir\=./.gem.20231229-56670-gi8fgb clean
current directory: /Users/tommy_1592/Desktop/work/dev/chile/vendor/bundle/ruby/3.1.0/gems/mysql2-0.5.5/ext/mysql2
make DESTDIR\= sitearchdir\=./.gem.20231229-56670-gi8fgb sitelibdir\=./.gem.20231229-56670-gi8fgb
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib/'
ld: library not found for -lzstd
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/tommy_1592/Desktop/work/dev/chile/vendor/bundle/ruby/3.1.0/gems/mysql2-0.5.5 for inspection.
Results logged to /Users/tommy_1592/Desktop/work/dev/chile/vendor/bundle/ruby/3.1.0/extensions/arm64-darwin-22/3.1.0/mysql2-0.5.5/gem_make.out
An error occurred while installing mysql2 (0.5.5), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.5' --source 'https://rubygems.org/'` succeeds before bundling.
In Gemfile:
mysql2
エラー内容が変わった
ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib/'
ld: library not found for -lzstd
opensslの読み込みが上手くいっていないよう
% ls /usr/local/opt/openssl/lib/
ls: /usr/local/opt/openssl/lib/: No such file or directory
こちらを参考に、opensslのリンク先の設定を行う
$ openssl version
LibreSSL ***
$ brew install openssl@3
$ brew link --force openssl@3
$ echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc
$ source ~/.zshrc
% openssl version
OpenSSL 3.2.0 23 Nov 2023 (Library: OpenSSL 3.2.0 23 Nov 2023)
次にこちらを参考に、mysql2のビルドオプションを設定する
% brew --prefix openssl
/opt/homebrew/opt/openssl@3
% brew --prefix zstd
/opt/homebrew/opt/zstd
% bundle config --local build.mysql2 "--with-ldflags=-L/opt/homebrew/opt/openssl@3/lib --with-opt-dir=/opt/homebrew/opt/zstd"
% bundle config
Settings are listed in order of priority. The top value will be used.
build.mysql2
Set for your local app (/Users/tommy_1592/Desktop/work/dev/chile/.bundle/config): "--with-ldflags=-L/opt/homebrew/opt/openssl@3/lib --with-opt-dir=/opt/homebrew/opt/zstd"
path
Set for your local app (/Users/tommy_1592/Desktop/work/dev/chile/.bundle/config): "vendor/bundle"
再リトライ
...
Installing mysql2 0.5.5 with native extensions
Bundle complete! 34 Gemfile dependencies, 130 gems now installed.
Bundled gems are installed into `./vendor/bundle`
入ったー 🎉