5
2

More than 3 years have passed since last update.

bundle installでmysql2 gemが `ld: library not found for -limported_openssl` で失敗する問題を強引に解決する

Last updated at Posted at 2019-10-18

概要

  • HomebrewでインストールしたMySQLを 5.7 から 5.6 にダウングレードしたところ、お題のエラーが発生するようになったっす
  • とても急いでいたので、類稀なバッドノウハウを以って解消しました

環境

  • macOS Catalina 10.15
  • ruby 2.4.2
  • Rails 4.2.11
  • MySQL 5.6.46
  • rbenv 1.1.2
  • Homebrew 2.1.15

いきなり対処法

  1. brew コマンドで MySQL 5.6 をアンインストール
  2. rbenv コマンドで ruby 2.4.2 を再インストール
  3. brew コマンドで MySQL 5.6 をインストール
  4. mysql_config を修正(後述)
  5. bundle config --delete build.mysql2 不要だったけど気分で(環境によっては必要かも)
  6. bundle install
  7. :end: :tada:
修正内容
/usr/local/Cellar/mysql@5.6/5.6.46/bin/mysql_config
123行目
- libs=" $ldflags -L$pkglibdir  -lmysqlclient  -limported_openssl -limported_crypto "
+ libs=" $ldflags -L$pkglibdir  -lmysqlclient  "

126行目
- libs_r=" $ldflags -L$pkglibdir  -lmysqlclient  -limported_openssl -limported_crypto   "
+ libs_r=" $ldflags -L$pkglibdir  -lmysqlclient  "

ここから詳細

bundle install のログはこんな感じ。

Installing mysql2 0.3.20 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
(...略)
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... 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 mysqld_error.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
(...略)
ld: warning: directory not found for option '-L/usr/local/opt/mysql@5.7/lib'
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

問題は2つあった

1. 根絶したつもりだった MySQL 5.7 さん問題
ld: warning: directory not found for option '-L/usr/local/opt/mysql@5.7/lib'

ひょっこり登場してきた。これは以下の 5.7 情報が書かれていたことが原因でした。

~/.rbenv/versions/2.4.2/lib/pkgconfig/ruby-2.4.pc
50行目
DLDFLAGS=-L/Users/choripon/.rbenv/versions/2.4.2/lib -L/usr/local/opt/mysql@5.7/lib -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress

恐らく ruby をインストールした際の引数が影響してる気がするけど今となっては謎です。ruby を入れ直すことで解決しました。

2. 自分誰やねん問題
ld: library not found for -limported_openssl

かなり謎だったため、最終手段 grep -ir 'imported_openssl' / 2>/dev/null の発動承認に至った次第でして、ヒットした mysql_config を直すことで解決しました。(だいぶ後で Using mysql_config at /usr/local/opt/mysql@5.6/bin/mysql_config のログ出力に気づいたのはナイショ)

以上です。

おしまい

今回弄った mysql_config は、後で元に戻しました。
あと全く関係ないですが、mkmf.log を心の中で「 むくむふろぐ 」と言ってます(誰にも共有したことない)。

5
2
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
5
2