bundle installでmysql2を入れるときにハマったのでメモ
TL;DR
bundle config --local build.mysql2 --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
エラー内容
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
...
ld: library not found for -lssl
要はlinkerがlocal上にあるはずのライブラリを見つけることができなかったらしい
gemはlocal上にあるライブラリに依存しているので、ライブラリの場所を指定することで対処できる
依存ライブラリ指定の仕方
gcc等の通常のコンパイラであれば以下のようにライブラリを指定することができる
LDFLAGS="-L/usr/local/opt/openssl/lib" CPPFLAGS="-I/usr/local/opt/openssl/include" PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig"
しかしbundleコマンドやgemコマンドではこの方法が通用しない
それぞれの指定方法を紹介する
gem installの場合
gemコマンドの場合以下のようにライブラリを指定することができる
gem install ジェムの名前 -- --with-環境変数=ライブラリのパス
今回の場合は
gem install mysql2 -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
でインストールすることができた。
bundle installの場合
local上にgemをインストールせず、bundle installしたい場合、以下のように特定のgemをインストールする際のライブラリを指定することができる
bundle config --local build.ジェムの名前 --with-環境変数=ライブラリのパス
今回の場合は
bundle config --local build.mysql2 --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
でインストールすることができた
参考
https://qiita.com/ts-3156/items/d53915307c77674b50ae
https://stackoverflow.com/questions/23559027/gem-install-with-additional-include-and-lib-paths
https://guides.rubygems.org/command-reference/#extension-install-failures
https://bundler.io/v1.16/bundle_config.html#BUILD-OPTIONS