bundle installができないエラー

変なエラーに引っかかったので備忘録として記載。

 

rails newを行ったところ、こんなエラーが発生。

どうやらMY SQLがbundleをインストールできなかったらしい。

 

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.

 

とりあえず、エラー文の言う通りに

gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'

をターミナルで実行(ちなみにrails newで作ったディレクトリに移動してから実行)

 

ld: library not found for -lssl

clang: error: linker command failed with exit code 1 (use -v to see invocation)

make: *** [mysql2.bundle] Error 1

 

make failed, exit code 2

 

と返された。

調べてみるとどうやらopensslというところの設定がうまくいってないらしい。

 

さらに調査してみると同じ内容で行き詰まってる記事を発見。

https://qiita.com/fukudakumi/items/463a39406ce713396403

 

記事にも書いてあるけど、

  • LDFLAGSとかCPPFLAGSとやらをオプション指定した上でbundle installすればいい。
  • 設定するべき値はbrew info opensslで確認できる。

ということでまずはbrew info opensslを実行。

色々出てくるけど重要なところは、この二文。

  export LDFLAGS="-L/usr/local/opt/openssl/lib"

  export CPPFLAGS="-I/usr/local/opt/openssl/include"

 

pathを間違えないように、ターミナルで下記を実行。

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で解決しました。

 

うーむたまたま全く同じ症例のエラーが出てたからいいものの、

ちゃんと理解してエラーログ対処していかないと今後が危ういぞ・・・。