32
23

More than 5 years have passed since last update.

mysql2 のgemがない?トラブルシューティング

Posted at

経緯

コントローラの作成を行おうとしたところ、mysql2のgemがないとのエラーが起こった。

$ rails g controller tweets
#=>よくあるエラー
Could not find gem 'mysql2 (= 0.5.2)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

Gemfileにリストされているどのgem sourcesにもmysql2(= 0.5.2)が見つかりません。
足りないgemをインストールするために bundle installを実行してください。
とのこと。言われたままにやってみる

$ bundle install

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

An error occurred while installing mysql2 (0.5.2), and Bundler cannot
continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'`
succeeds before bundling.

・・・だめだ

解決方法

この場合、以下のようにすることでmysql2をインストールできます。

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

正しくインストールされました!

$ bundle show | grep mysql2
  * mysql2 (0.4.5)
32
23
1

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
32
23