1
1

More than 1 year has passed since last update.

[Rails]初のbundle installでmysql2のエラーが出た時の解決策をまとめてみた

Last updated at Posted at 2021-09-21

はじめに

開発環境を整えているとき、初のbundle installにて以下のようなエラーが出ました。

Installing mysql2 0.5.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

(中略)

Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

(中略)

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:

このエラーで詰まされている人が多く、関連記事が既にたくさんあるのですが、以下で登場するOpenSSLやbundle configといった周辺知識を調べたかったので自分で解決策をまとめてみました。

1.mysql2をbundle installする時のオプションを指定

だいたいの皆さんは以下のコマンドでうまくいってます。

$ brew install openssl  //opensslがinstall済みであれば不要
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
$ bundle install

補足

bundle config 〜のコマンドは一体何をしているのか

  • bundle configはbundlerの設定に関するコマンド
  • --localオプションをつけることで、プロジェクトごとの設定ファイルに設定を書き込む
    • そのファイルはapp/.bundle/configに格納されている
  • build.mysql2でmysql2をビルド(bundle install)する時のオプションを指定
    • mysql2をbundle installする時に、OpenSSLを参照するようにパスを追加している。

"--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"の部分はbrew info opensslで確認できます。

参考
https://dog-and-rabbit.com/rails-mysql2-gem-error
https://qiita.com/thunders/items/101c6b329830fb1fb27d

OpenSSLとは何か

SSLの機能を提供するライブラリです。HTTPS通信などで使われます。


しかし私はこれでもこれでうまくいかなかったのでさらに調査を進めました。すると以下の記事に出会いました。
https://310nae.com/error-mysql2/

1度に2つのライブラリのパスを同時に指定するとエラーになるということで、コマンドを2回に分けます。

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

しかしこれでもうまくいかず、、

2.そもそもmysqlがインストールされていない

私はこのパターンでした、、汗

$ brew list  #このコマンドで表示されるリストの中にmysqlがなければ以下のコマンドを打つ
$ brew install mysql
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
$ bundle install

mysql2を使うときはMySQLクライアントを先にローカルにインストールする必要があるみたいです。

補足

MySQLクライアントとは?

  • MySQLサーバーと通信してデータベース内の情報を操作するためのプログラム
  • mysqlはコマンドラインを提供するクライアント

参考
https://gihyo.jp/dev/serial/01/mysql-road-construction-news/0051
https://qiita.com/unsoluble_sugar/items/1403ddf0ac9709b1aae6

余談

私は他にも

  • ruby, gem, opensslのパスがおかしくないかチェック
$ which ruby
$ which openssl
  • gemのアップデート
$ gem update --system    #gem自体のアップデート
$ bundle update    #プロジェクト内のgemのアップデート

参考 https://qiita.com/tatama/items/4e9d6bb8bfa2d9556db7

  • xcodeインストール
xcode-select --install

参考 https://qiita.com/gengogo5/items/815869416eaee3bb8daa

などを試しましたがうまくいきませんでした。しかし、今後またエラーになった時に、原因の1つになることも否定できないので一応書き記しておきます。

まとめ

OpenSSLに関して、もっと勉強していく必要がありそうです。
何か間違いがあれば気兼ねなくコメントください。

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