rails 6.0.0に対応したアプリを作成しようと環境構築していた際に、bundle installができなくなり、さらに既存アプリも(rails 5.2.3で作成)開けなくなりました。既存アプリを再度開けるようにするまでの過程を記録します。
この記事を書いている現在でもrails6での環境構築はできていません。
本トピック関連の環境
rails (6.0.0 / 5.2.3)
macOS Mojave ver.10.14.6
mysql@5.7 / mysql@5.6
エラーが発生した経緯
(エラーの詳細は『既存アプリを開けるようにするためにやったこと』の下に書いています。)
rails 6.0.0にてAction text等の新機能を試してみたいと思い、rails 6.0.0をインストールしました。
rbenvを使って、rubyのバージョンを2.6.4にアップデート → アプリ用のディレクトリ作成 → そのディレクトリの中で、rails 6.0.0インストール。
rails _6.0.0_ new -d mysql .
ここから詰まりに詰まった3日間が始まりました。
bundle install
これが全く通りません。
rails 6.0.0の新規アプリも…
rails 5.2.3の既存アプリも…
既存アプリを開けるようにするためにやったこと
(ここでいう既存アプリはrails 5.2.3で作成)
- rails 6.0.0のアンインストール
- Homebrewのアンインストールと再インストール
- mysql@5.6とmysql@5.7のアンインストール
- mysql@5.6の再インストール
この4つです。
1. rails 6.0.0のアンインストール
$ cd //ホームディレクトリに移動
$ gem list //railsのバージョン確認のため
$ gem uninstall rails --version 6.0.0
$ gem install rails --version 5.2.3 //必要に応じて
インストール先が違えば、そのディレクトリ内でアンインストールしてください。
2. Homebrewのアンインストールと再インストール
必要がなければこの手順はスキップしてください。
mysqlのアンインストールだけでよければ、以下の記事を参考に。
コマンドは随時更新されているので、公式から直接コピーした方が良いと思います。
必要であれば、アンインストールする前に$ brew listで何をHomebrew経由でインストールしているのか確認。
▼アンインストール
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
▼インストール
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
ここで一旦、mysql@5.6をインストールして、既存アプリでrails sできないかな、と試してみましたが、だめでした。
[1]エラー
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) もしくは (38)
ここで/tmp/mysql.sockを作成すると、[2]エラーのときに再度/tmp/mysql.sockを削除することになるらしいので作成しない方がいいようです。
mysqlのプロセスを確認します。(たぶん動いていないから[1]エラーが出る)
$ ps -ax | grep mysql
以下、試してみた。
$ mysql.server start
$ mysql.server restart
しかしエラー解決できず…
[2]エラー
ERROR! The server quit without updating PID file (/usr/local/var/mysql/XXXX.local.pid).
ここで/usr/local/var/mysqlの配下に(ユーザー名).local.pidを作成しても、なにかコマンドを打つと勝手に消えます。
負のループに入ります。
以下の記事を参照して、とりあえずは既存アプリを開けるようにしました。
- ローカルでmysql立ち上げるとERROR! The server quit without updating PID file (/usr/local/var/mysql/**.local.pid)
- 「ERROR! The server quit without updating PID file」となり、MacOSでmysqlにアクセスできない。
3. mysql@5.6とmysql@5.7のアンインストール
前述の手順にも載せていますが、
この記事を参考にしました。
私は前の手順でHomebrewごとアンインストールしていたので、以下の手順から…
▼ローカルのmysqlを削除してmysqlインストール
$ sudo rm -rf /usr/local/mysql
$ sudo rm -rf /Library/StartupItems/MYSQL
$ sudo rm -rf /Library/PreferencePanes/MySQL.prefPane
$ sudo rm -rf /Library/Receipts/mysql-.pkg
$ sudo rm -rf /usr/local/Cellar/mysql*
$ sudo rm -rf /usr/local/bin/mysql*
$ sudo rm -rf /usr/local/var/mysql*
$ sudo rm -rf /usr/local/etc/my.cnf
$ sudo rm -rf /usr/local/share/mysql*
$ sudo rm -rf /usr/local/opt/mysql*
▼mysqlコマンド打てないことを確認
$ mysql -v
# mysql: command not found
4. mysql@5.6の再インストール
$ brew install mysql56
$ brew reinstall https://raw.githubusercontent.com/Homebrew/homebrew-core/f171f1c74/Formula/mysql@5.6.rb
▼ パスを通す & mysqlの自動更新設定
//パスを通す
$ echo 'export PATH="/usr/local/opt/mysql@5.6/bin:$PATH"' >> ~/.bash_profile
//自動更新設定
$ mkdir ~/Library/LaunchAgents
$ ln -sfv /usr/local/opt/mysql\@5.6/*.plist ~/Library/LaunchAgents
// mysql起動
$ /usr/local/opt/mysql@5.6/bin/mysql.server start
上記コマンド打ったあとに再度、既存アプリを開くため$ rails s
mysqlを入れ直しているせいなのか、no database〜みたいなエラーが出ましたが
エラー文に解決策も載せてあったので、そのまま打ちました。
$ rails db:create
$ bin/rake db:migrate RAILS_ENV=development
やっとアプリが開けるようになりました。
あとはHomebrewのアンインストールで消えてしまったものを再度入れ直して、現状rails5でアプリ開発しています。
はやくrails6でアプリ作成できる環境を整えたい…
エラー詳細
▼ bundle installができなかったときのエラーは以下の通りです。
XXXX:sample_app XXXX$ bundle install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
(gemのリスト)
Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/ext/mysql2
/Users/XXXX/.rbenv/versions/2.6.5/bin/ruby -I /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r ./siteconf20191019-30330-sj3h0.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/opt/mysql@5.6/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... no
checking for MYSQL_OPT_SSL_ENFORCE in mysql.h... no
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/opt/mysql@5.6/lib
-----
creating Makefile
current directory: /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR=" clean
current directory: /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -limported_openssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1
make failed, exit code 2
Gem files will remain installed in /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2 for inspection.
Results logged to /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-18/2.6.0-static/mysql2-0.5.2/gem_make.out
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.
In Gemfile:
mysql2
mysql2のお家がないよ、と言われました。
Installing mysql2 0.5.2 with native extensions Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
エラーの最後に指示があったので、その通りにコマンドを打ちました。
$ gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'
しかしながら、エラーは継続します。
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
current directory: /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/ext/mysql2
/Users/XXXX/.rbenv/versions/2.6.5/bin/ruby -I /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r ./siteconf20191019-38722-ow1bgt.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/opt/mysql@5.6/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... no
checking for MYSQL_OPT_SSL_ENFORCE in mysql.h... no
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/opt/mysql@5.6/lib
-----
creating Makefile
current directory: /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR=" clean
current directory: /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -limported_openssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1
make failed, exit code 2
Gem files will remain installed in /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2 for inspection.
Results logged to /Users/XXXX/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-18/2.6.0-static/mysql2-0.5.2/gem_make.out
ネットで検索したところ
ld: library not found for -limported_openssl 類似したエラーが原因で、ここで引っかかっているという記事がありました。
このエラー解決法を実行する前に、opensslの有無を確認するためにも
再度インストールしてみました。
$ brew install openssl
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
futhark
Error: openssl 1.0.2s is already installed
To upgrade to 1.0.2t, run `brew upgrade openssl`
openssl 1.0.2sは、すでにあるよ!と言われてしまいました。
念の為、$ brew upgrade openssl
無事にアップデートできました。
アップデート詳細の最後の方にまた指示があったので、それに従いました。
If you need to have openssl first in your PATH run:
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
For compilers to find openssl you may need to set:
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
指示にある3つのコマンドを打ちました。
$ echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile`
$ bundle config --local build.mysql2 --with-ldflags=-L/usr/local/opt/openssl/lib`
$ bundle config --local build.mysql2 --with-cppflagss=-I/usr/local/opt/openssl/include
2と3は合わせて
$ gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/' -- --with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib
これでも大丈夫みたいです。
解決策として「このコマンド打てば通った!」という事例が多いように思いました。
でも私の場合は通りませんでした。
同様のエラーに遭う方もいるようで…
mysql@5.7を入れることでエラーが
ld: library not found for -lsslに変わったりもしましたが、結局エラー解決できず…
逆にこれが悪かったのかもしれない。
以上です。