fv17の日記

Webエンジニアの備忘用ブログです。主にWeb界隈の技術に関して書いています。

bundle install時に"An error occurred while installing mysql2 (0.4.9), and Bundler cannot continue."でコケる場合の対処法

エラー発生手順

MySQLをインストール

brew install mysql@5.7
brew tap homebrew/services
brew services start mysql@5.7

アプリケーションのディレクトリでbundle install

git clone <クローン対象のURL>
cd <対象ディレクトリ>
bundle install

発生したエラー

Installing mysql2 0.4.9 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
.
.
.
An error occurred while installing mysql2 (0.4.9), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.9' --source 'https://rubygems.org/'` succeeds before bundling.

原因と対処方法

その1 - MySQLが正しく認識されていない

下記コマンドで正しく認識させる

brew link mysql@<your MySQL version> --force

例えば、

brew link mysql@5.7 --force