hoge-hogeoのひきこもごも

インフラエンジニアだけど形を持ったインフラを触ったことがない人の徒然

bundle installで'ld: library not found for -lssl'が出てつらい話

ローカルにアプリケーションcloneしてきてrubyのデプロイツールを検証したい。

bundle installが通らない

$ bundle install --path vendor/bundle
Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
〜中略〜
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

Gem files will remain installed in /Users/hogehogeo/Documents/hogeo-service/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.2 for inspection.
Results logged to /Users/hogehogeo/Documents/hogeo-service/vendor/bundle/ruby/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.

gem installしてみる

gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/

"ld: library not found for -lssl"で落ちる

$ gem install mysql2 
Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.
  
〜中略〜
current directory: /Users/hogehogeo/.rbenv/versions/2.6.3/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 -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

Gem files will remain installed in /Users/hogehogeo/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2 for inspection.
Results logged to /Users/hogehogeo/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-18/2.6.0-static/mysql2-0.5.2/gem_make.out

参考:【Rails】MySQL2がbundle installできない時の対応方法 - Qiita

openssl自体は入ってるらしいので、upgradeしてみる

Error: openssl 1.0.2r is already installed
To upgrade to 1.0.2t, run `brew upgrade openssl`
$ brew upgrade openssl
==> Upgrading 1 outdated package:
openssl 1.0.2r -> 1.0.2t
==> Upgrading openssl 
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2t.mojave.bottle.tar.gz

中身見てみる

$ brew info openssl
openssl: stable 1.0.2t (bottled) [keg-only]
SSL/TLS cryptography library
https://openssl.org/
/usr/local/Cellar/openssl/1.0.2t (1,795 files, 12.0MB)
  Poured from bottle on 2019-09-25 at 12:18:20
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/openssl.rb
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

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

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

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"

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

==> Analytics
install: 268,610 (30 days), 1,333,944 (90 days), 6,296,651 (365 days)
install_on_request: 51,876 (30 days), 187,062 (90 days), 858,928 (365 days)
build_error: 0 (30 days)

上記を参考にしながらパスを指定

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

bundle install再実行

$ bundle install --path vendor/bundle
〜中略〜
Using mini_magick 4.9.5
Using mustermann 1.0.3
Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions

通った。

もうやりたくない。