LoginSignup
12
6

More than 3 years have passed since last update.

【Rails環境構築】MySQL2が原因で「bundle install」失敗した時の対処法

Posted at

はじめに

Railsの環境構築をローカルでしようとrails newからはじめたところ、bundle installができずに引っかかったため備忘録として残しておきます。

環境

OS : Mac OS Catalina 10.15.1
Ruby : 2.6.3p62
Rails : 6.0.1
Homebrew : 2.1.16
Bundle : 1.17.2

エラー

$ bundle install

省略(gemのインストール)

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 0.5.2のインストール時にnative extention(CやC++で書かれるMySQLの拡張のライブラリ)のビルドに失敗したので、bundleの前にgem install mysql2 -v '0.5.2'で確認してください」とのことですが、私の場合root権限でこのコマンドを打っても解決せず...

解決法

結論から言うと、「ビルド時にOpenSSL公開鍵のPATH指定が必要」と「bundle configでPATHオプションをLDFLAGS、CPPFLAGSで別々に指定」で解決しました。

1.公開鍵のPATH確認

brew info openssl

省略

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"      ← ここ使用
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"   ← ここ使用

省略

2.それぞれPATHを指定

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

それぞれ、「build.mysql2の現在のローカル値を置き換えています」と返答がきます。

3. bundle install (ここで終了)

$ bundle install

私の場合はこれで解決し、「rails new」でも問題なく作成できました。

失敗or解決に直接繋がらなかったコマンド

1. PATHの両指定

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

2.mysqlの再起動

$ sudo mysql.server restart

その他

公開鍵、LDFLAGS、CPPFLAGSなど知識やAppleの独自TLS背景などまだまだ勉強不足でした💦
表記や解釈ミスなどあればご指摘頂けますと幸いです。

参考

こちらの記事がなければ解決できませんでした(本当に助かりました)。
https://qiita.com/fukudakumi/items/463a39406ce713396403
https://qiita.com/akito19/items/e1dc54f907987e688cc0
http://woshidan.hatenadiary.jp/entry/2017/01/21/150948

12
6
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
12
6