【エラー解決】rake aborted! LoadError: dlopen(/Users/user/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.5.2/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/opt/[email protected]/lib/libmysqlclient.18.dylib


rake aborted! LoadErrorで怒られて、解決できたので備忘録として残しておきます。

エラー内容

$ bundle exec rake db:create

rake aborted!
LoadError: dlopen(/Users/user/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.5.2/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/opt/[email protected]/lib/libmysqlclient.18.dylib
  Referenced from: /Users/user/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.5.2/lib/mysql2/mysql2.bundle
Reason: image not found - /Users/user/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.5.2/lib/mysql2/mysql2.bundle
/Users/user/Projects/chat-space/config/application.rb:7:in `<top (required)>'
/Users/user/Projects/chat-space/Rakefile:4:in `require_relative'
/Users/user/Projects/chat-space/Rakefile:4:in `<top (required)>'
/Users/user/.rbenv/versions/2.3.1/bin/bundle:23:in `load'
/Users/user/.rbenv/versions/2.3.1/bin/bundle:23:in `<main>'
(See full trace by running task with --trace)

上記コマンドを入力すると、/usr/local/opt/[email protected]/lib/libmysqlclient.18.dylibファイルが、無いとエラーが出ている。

パーミッションの確認

次にパーミッションの確認で、以下のコマンドを入力。
()内の(/Users/user/.rbenvの => 2番目のuser部分は各個人のユーザー名なので適宜、自身のPCを確認してください。

$ ls -l /usr/local/opt/mysql/lib

total 22416
-rw-r--r--   1 USER_NAME  admin  5423052  9 18 18:55 libmysqlclient.21.dylib
-r--r--r--   1 USER_NAME  admin  6041256  6 29 01:42 libmysqlclient.a
lrwxr-xr-x   1 USER_NAME  admin       23  6 29 01:42 libmysqlclient.dylib -> libmysqlclient.21.dylib
-r--r--r--   1 USER_NAME  admin     9624  6 29 01:42 libmysqlservices.a
drwxr-xr-x   3 USER_NAME  admin       96  9 18 18:55 pkgconfig
drwxr-xr-x  91 USER_NAME  admin     2912  6 29 01:42 plugin

内容を確認してみると、/usr/local/opt/[email protected]/lib/libmysqlclient.18.dylibはなく、libmysqlclient.21.dylibがある状態です。

パーミッションについて参考になった記事

パーミッションについては、以下の記事が参考になりました。
-rw-r--r--などの説明についてわかりやすく説明しているの、参考にしてください。

permission deniedで悩まされたので勉強してみた

もうPermission deniedなんて怖くない!〜linuxのパーミッションについてかなり丁寧に説明してみる〜

パーミッションコマンドを入力すると、

find /usr -name コマンドで「libmysqlclient.18.dylib」を探す

$ find /usr -name 'libmysqlclient.18.dylib'

ind: /usr/sbin/authserver: Permission denied
find: /usr/local/var/mysql/sys: Permission denied
find: /usr/local/var/mysql/mysql: Permission denied
find: /usr/local/var/mysql/performance_schema: Permission denied

Permission deniedがたくさん表示される。

bundle doctorコマンドで各情報の表示&トラブルの診断をする。

$ bundle doctor

The following gems are missing OS dependencies:
 * byebug: /usr/local/opt/gmp/lib/libgmp.10.dylib
 * ffi: /usr/local/opt/gmp/lib/libgmp.10.dylib
 * mysql2: /usr/local/opt/gmp/lib/libgmp.10.dylib
 * mysql2: /usr/local/opt/[email protected]/lib/libmysqlclient.18.dylib
 * nokogiri: /usr/local/opt/xz/lib/liblzma.5.dylib

エラー内容を確認すると、The following gems are missing OS dependencies:
と、以下のgemたちは、OSへの依存関係がありません:と出ています。

なので出ているgemsを1つ1つアンインストールしていきます。

bundle exec gem uninstall [gem名]コマンド

bundle exec gem uninstall byebug

emove executables:
        byebug

in addition to the gem? [Yn]  n
Executables and scripts will remain installed.
Successfully uninstalled byebug-10.0.2

最下部にSuccessfully uninstalled byebug-10.0.2とアンインストール完了と表示されます。

再度、bundle doctorを行います。

$ bundle doctor

The following gems are missing
 * byebug (10.0.2)
Install missing gems with `bundle install`

とエラーが出るので、エラー内容が示すようにbundle installをします。

bundle install

$ bundle install

Fetching gem metadata from https://rubygems.org/.........
Using rake 12.3.1
Using concurrent-ruby 1.0.5
Using i18n 1.1.0
 |
省略
 |
Using uglifier 4.1.19
Using web-console 3.7.0
Bundle complete! 18 Gemfile dependencies, 78 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

これで終わりではなく、再度bundle doctorで診断しましょう

$ bundle doctor

The Gemfile's dependencies are satisfied
The following gems are missing OS dependencies:
 * ffi: /usr/local/opt/gmp/lib/libgmp.10.dylib
 * mysql2: /usr/local/opt/gmp/lib/libgmp.10.dylib
 * mysql2: /usr/local/opt/[email protected]/lib/libmysqlclient.18.dylib
 * nokogiri: /usr/local/opt/xz/lib/liblzma.5.dylib

とまた依存関係のないgems兄弟たちが出てくるので、これを再度bundle exec gem uninstall gem名コマンドを行います。

順に表示されているものから行い、Successfully uninstalled gem名が出来たら、bundle installをしましょう。

$ bundle exec gem uninstall ffi

You have requested to uninstall the gem:
        ffi-1.9.25

childprocess-0.9.0 depends on ffi (>= 1.0.11, ~> 1.0)
rb-inotify-0.9.10 depends on ffi (< 2, >= 0.5.0)
If you remove this gem, these dependencies will not be met.
Continue with Uninstall? [yN]  y
ERROR:  While executing gem ... (Gem::DependencyRemovalException)
    Uninstallation aborted due to dependent gem(s)

エラーのgems兄弟を1つ1つbundle exec gem uninstall gem名していくと、bundle doctorで再診断を行いましょう。


Install missing gems with `bundle install`

bundle installをしてくださいと出るので、bundle installを行います。
無事にインストールが出来たら、bundle doctorで診断。

最後にbundle doctor

$ bundle doctor

The Gemfile's dependencies are satisfied
No issues found with the installed bundle

gmes兄弟の依存関係が解消されました。
インストールされたbundleに問題はありません。
と解決できました。

これでrails gでも、私の場合はデータベースを作成したかったのでundle exec rake db:createをしました。

$ bundle exec rake db:create

Created database 'sample_development'
Created database 'sample_test'

でエラー解決できました。

参考記事

Homebrew で MySQL を 5.7.22 から 8.0.11 にバージョンアップしたら Rails が動かなくなったので対処する

MySQLエラー解決法(Mac編)

permission deniedで悩まされたので勉強してみた

もうPermission deniedなんて怖くない!〜linuxのパーミッションについてかなり丁寧に説明してみる〜