mysql2インストール時のエラー対応
AWSについて学習中。Railsアプリをあげようと思ったらエラーが出てややハマったのと、同様のエラー記事がなかったので残しておく。
Apple M1 ruby 3.1.3 Rails 7.0.4.1
gemからmysql2をインストールしようとすると以下のようなエラーが出た。
$gem install mysql2 Building native extensions. This could take a while... ERROR: Error installing mysql2: ERROR: Failed to build gem native extension. current directory: /Users/username/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/mysql2-0.5.5/ext/mysql2 /Users/username/.rbenv/versions/3.1.3/bin/ruby -I /Users/username/.rbenv/versions/3.1.3/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 ----- Cannot find library dir(s) /usr/local/opt/openssl@1.1/lib ----- *** 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/username/.rbenv/versions/3.1.3/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 To see why this extension failed to compile, please check the mkmf.log which can be found here: /Users/username/.rbenv/versions/3.1.3/lib/ruby/gems/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/username/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/mysql2-0.5.5 for inspection. Results logged to /Users/username/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/extensions/arm64-darwin-22/3.1.0/mysql2-0.5.5/gem_make.out
openssl関連で、--with-ldflags
や--with-cppflags
オプションをつければインストールできるという記事が多いのでやってみるが同じエラーが続く。
エラー文読み直しにて、Cannot find library dir(s) /usr/local/opt/openssl@1.1/lib
が怪しい。
そもそも/usr/local/opt/openssl@1.1/lib
というディレクトリがなく、そりゃ見つからないと怒られてしまう。
なんでこんなことになっているのかと思ったら.zshrc
に
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/local/opt/openssl@1.1"
という環境変数が設定されていた。多分、学習初期に訳もわからず設定したものな気がする。
これを存在するパスに。Home · rbenv/ruby-build WikiをみるとRuby 3.1以降ではOpenSSL3が必要とのことで
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@3)"
こうすることで無事にmysql2をインストールできた。