bundle installでmysql2のインストールができない

Category :

bundle installでmysql2をインストールしようとするとエラーがでました。

Fetching mysql2 0.5.3
Installing mysql2 0.5.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
make: *** [mysql2.bundle] Error 1
make failed, exit code 2
Gem 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.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

対処法

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 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"

フラグを設定するように指示されるので、こちらを実行します。

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を再度実行すると、インストールができるはずです。

TOP
© 2021 uichi