Tech Hotoke Blog

IT観音とは私のことです。

【Ruby】mysql2 gemインストール時のトラブルシュート

f:id:TechHotoke:20220404184502p:plain

目的

トラブルシューティングの備忘録として

環境

エラー内容

current directory: /Users/yudai/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/mysql2-0.5.3/ext/mysql2
/Users/yudai/.rbenv/versions/3.0.2/bin/ruby -I /Users/yudai/.rbenv/versions/3.0.2/lib/ruby/3.0.0 -r ./siteconf20220404-12423-h3cixo.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.7/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
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.7/lib
-----
creating Makefile

current directory: /Users/yudai/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/mysql2-0.5.3/ext/mysql2
make DESTDIR\= clean

current directory: /Users/yudai/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/mysql2-0.5.3/ext/mysql2
make DESTDIR\=
compiling client.c
client.c:787:14: warning: incompatible pointer types passing 'VALUE (void *)' (aka 'unsigned long (void *)') to parameter of type 'VALUE (*)(VALUE)' (aka 'unsigned long (*)(unsigned long)') [-Wincompatible-pointer-types]
  rb_rescue2(do_send_query, (VALUE)&args, disconnect_and_raise, self, rb_eException, (VALUE)0);
             ^~~~~~~~~~~~~
/Users/yudai/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/iterator.h:51:25: note: passing argument to parameter here
VALUE rb_rescue2(VALUE(*)(VALUE),VALUE,VALUE(*)(VALUE,VALUE),VALUE,...);
                        ^
client.c:795:16: warning: incompatible pointer types passing 'VALUE (void *)' (aka 'unsigned long (void *)') to parameter of type 'VALUE (*)(VALUE)' (aka 'unsigned long (*)(unsigned long)') [-Wincompatible-pointer-types]
    rb_rescue2(do_query, (VALUE)&async_args, disconnect_and_raise, self, rb_eException, (VALUE)0);
               ^~~~~~~~
/Users/yudai/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/iterator.h:51:25: note: passing argument to parameter here
VALUE rb_rescue2(VALUE(*)(VALUE),VALUE,VALUE(*)(VALUE,VALUE),VALUE,...);
                        ^
2 warnings generated.
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
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

ld: library not found for -lssl

ということでライブラリが見つかりませんというエラーのよう

そもそもldって?

まず、ldGNUリンカーと呼ばれるコマンドです。

lsslってなに?

-lsslとは、mysql2 gemインストール時にldコマンドに与えられたオプションの1つ。

  • lオプション

-lx This option tells the linker to search for libx.dylib or libx.a in the library search path. If string x is of the form y.o, then that file is searched for in the same places, but without prepending lib' or appending.a' or `.dylib' to the file- name.

libx.dylibまたはlibx.aを検索するようにリンカに指示するとあるように、mysql2 gemはlibssl.dylibをライブラリ検索範囲から探してねと指示しているということになるらしいです

Setting libpath to /usr/local/opt/mysql@5.7/lib

実際に覗いてみると、お目当てのlibssl.dylibはありません。

libmysqlclient.20.dylib  libmysqlclient.a    libmysqlclient.dylib    libmysqld.a     libmysqlservices.a  pkgconfig       plugin

brew info openssl を実行します

openssl@3: stable 3.0.2 (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/usr/local/Cellar/openssl@3/3.0.2 (6,429 files, 28.3MB)
  Poured from bottle on 2022-04-04 at 16:36:04
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@3.rb
License: Apache-2.0
==> Dependencies
Required: ca-certificates ✔
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@3/certs

and run
  /usr/local/opt/openssl@3/bin/c_rehash

openssl@3 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have openssl@3 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openssl@3/bin:$PATH"' >> ~/.zshrc

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

For pkg-config to find openssl@3 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@3/lib/pkgconfig"

==> Analytics
install: 153,065 (30 days), 365,265 (90 days), 676,991 (365 days)
install-on-request: 121,259 (30 days), 284,267 (90 days), 531,231 (365 days)
build-error: 5,474 (30 days)
  export LDFLAGS="-L/usr/local/opt/openssl@3/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@3/include"

ここをオプションに指定するらしい(詳しくは後日調べます)

下記を実行

sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/' -- --with-cppflags=-I/usr/local/opt/openssl@3/include --with-ldflags=-L/usr/local/opt/openssl@3/lib

行けました!

Building native extensions with: '--with-cppflags=-I/usr/local/opt/openssl@3/include --with-ldflags=-L/usr/local/opt/openssl@3/lib'
This could take a while...
Successfully installed mysql2-0.5.2
Parsing documentation for mysql2-0.5.2
Installing ri documentation for mysql2-0.5.2
Done installing documentation for mysql2 after 0 seconds
1 gem installed

参考