LoginSignup
2
6

More than 3 years have passed since last update.

`require': dlopen(~ mysql2): Library not loaded: /usr/local/opt/openssl@1.1/lib/libssl.1.1.dylib (LoadError)

Last updated at Posted at 2020-02-18

0. 問題

rails server が立ち上がらず、以下のようなエラーが出る。

$ rails s
/Users/yokoto/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/lib/mysql2.rb:31:in `require': dlopen(/Users/yokoto/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/opt/openssl@1.1/lib/libssl.1.1.dylib (LoadError)
  Referenced from: /Users/yokoto/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/lib/mysql2/mysql2.bundle

どうやら、 openssl@1.1libssl.1.1mysql2-0.4.5 あたりに問題がありそう。

1. 環境

$ openssl version
LibreSSL 2.8.3

MacのHigh Sierraから、デフォルトのOpenSSLがOpenSSLからLibreSSLになっている

らしく、OpenSSL依存のライブラリが問題を起こしていらしい。
ちなみに自分の環境は、

  • macOS Catalina バージョン10.15.1

参考リンク

2. 解決方法

1. mysql2 gemのアンインストール

$ gem uninstall mysql2

2. opensslのインストール

$ brew install openssl

次が重要。上記の出力で、

...
openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because openssl/libressl is provided by macOS so don't link an incompatible version.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile

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"

とあるので、これに従い、

$ echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile
$ export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
$ export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
$ export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

bash_profile の再起動を忘れずに。

$ . ~/.bash_profile

3. mysql2 gemのインストール

OpenSSLを使ってインストールする必要がある。

グローバルにインストール

$ gem install mysql2 -v 0.4.10 -- --with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib

bundlerを使ってインストール

$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
$ bundle install

参考リンク

3. 結果

$ rails s
=> Booting Puma
=> Rails 5.1.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.8.2 (ruby 2.4.1-p111), codename: Sassy Salamander
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000

よかった。

2
6
2

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
6