LoginSignup
2
1

More than 1 year has passed since last update.

Railsでmysql2 gemのbundle installに失敗した時の対処法。LibreSSL→OpenSSLで解決

Last updated at Posted at 2021-12-26

経緯

ポートフォリオ用Railsアプリの作成のため、rubyとRailsおよびMySQLのインストールを終えてbundle installしようとしたところ下記のエラーが出て失敗。

(中略)

Gem::Ext::BuildError: ERROR: Failed to build gem native extension. mysql2

(中略)

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.27_1/lib

(中略)

1 warning generated.
linking shared-object mysql2/mysql2.bundle
ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib'
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

(中略)

An error occurred while installing mysql2 (0.5.3), and Bundler cannot
continue.

In Gemfile:
  mysql2

実行環境

・macOS Monterey v12.1
・ruby v3.1.0
・Rails v7.0.0

結論

HomebrewでOpenSSLをインストールしてパスを通したところ、無事にmysql2のbundle installに成功。

解決への道のり

まず1番目のエラーでググってみたところ、どうやらMacにはSSL/TLSライブラリとしてLibreSSLが入っているいて、これがが悪さをしているらしい。

(参考記事)

現状把握

自分のmacのSSL/TLSライブラリを調べてみる。

$ brew info openssl
openssl@3: stable 3.0.1 (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
Not installed                          <=OpenSSLはインストールされていない?
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@3.rb
License: Apache-2.0
(中略)

HomebrewとしてはOpenSSLの3.0.1が存在することを認知しているけど、インストールはされていないよ、ということだと解釈。

続いて、現在インストールされているSSL/TLSライブラリを調べる。

$ openssl version
LibreSSL 2.8.3

LibreSSL 2.8.3らしい。

そこで、
1, OpenSSLをインストール
2, OpenSSLにPATHを通してデフォルトに設定
3, bundle install
という手順を踏むことにする。

$ brew update
$ brew reinstall openssl
$ brew link --force openssl
Warning: Refusing to link macOS provided/shadowed software: openssl@3
If you need to have openssl@3 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openssl@3/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@3 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@3/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@3/include"

For pkg-config to find openssl@3 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@3/lib/pkgconfig"

なにやら色々指示をされているみたいなのでその通りにしたいところですが、ここは後述の参考記事の通り手を加えた方が良いようです。

$ echo 'export PATH=$(brew --prefix openssl)/bin:$PATH' >> ~/.zshrc
$ source ~/.zshrc

(参考記事)

結果

インストールが完了。

% brew info openssl
openssl@3: stable 3.0.1 (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/usr/local/Cellar/openssl@3/3.0.1 (6,420 files, 28.1MB)   <=記述が変わった
  Poured from bottle on 2021-12-26 at 17:19:16
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@3.rb
License: Apache-2.0
(中略)

OpenSSLのデフォルト化に成功。

% openssl version
OpenSSL 3.0.1 14 Dec 2021 (Library: OpenSSL 3.0.1 14 Dec 2021)

この状態でbundle installを行ったところ、無事にmysql2 gemのインストールに成功しました。

(中略)
Using reline 0.3.0
Fetching mysql2 0.5.3
Using websocket-extensions 0.1.5
(中略)
Installing mysql2 0.5.3 with native extensions
Bundle complete! 15 Gemfile dependencies, 67 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1