bundle installでmysql2のインストールができない
Category : Rails
bundle installでmysql2をインストールしようとするとエラーがでました。
Fetching mysql2 0.5.3Installing mysql2 0.5.3 with native extensionsGem::Ext::BuildError: ERROR: Failed to build gem native extension....make: *** [mysql2.bundle] Error 1make failed, exit code 2Gem files will remain installed in /Users/nakaikensuke/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3 for inspection.Results logged to /Users/nakaikensuke/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-19/2.6.0/mysql2-0.5.3/gem_make.outAn 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
対処法
gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'
を実行するように指示があるので、実行してください。
もう一度bundle install
を実行します。
下記のエラーが出てきたら、openssl
をインストールする必要があります。
Building native extensions. This could take a while...ERROR: Error installing mysql2:ERROR: Failed to build gem native extension....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.22/lib...
brew info openssl
を実行してください。
A CA file has been bootstrapped using certificates from the systemkeychain. To add additional certificates, place .pem files in/usr/local/etc/openssl@1.1/certsand run/usr/local/opt/openssl@1.1/bin/c_rehashopenssl@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"' >> ~/.zshrcFor 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"
フラグを設定するように指示されるので、こちらを実行します。
bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl@1.1/lib --with-cppflags=-I/usr/local/opt/openssl@1.1/include"
bundle install
を再度実行すると、インストールができるはずです。