Ruby mysql2 gem 설치 시 libssl 오류 해결법

Problem

mysql2 gem 설치 과정에서 libssl library가 없다는 오류가 발생했다.

❯❯❯ gem install mysql2
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
        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

[1] 을 참고해 openssl 을 다시 설치해보도록 한다. libssl 이 없는데 왜 openssl을 설치해야하는 지 이상했는데, [2]에 따르면 openssllibssl을 포함한다.

OpenSSL consists of two separate libraries: libcrypto and libssl. libcrypto is a general-purpose cryptography library which can be used alone. libssl is a TLS library which depends on libcrypto.

❯❯❯ brew reinstall openssl && brew link openssl --force
(중략)
==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2q: 1,794 files, 12.1MB
Warning: Refusing to link macOS-provided software: openssl
If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc

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"

Solution

[1]의 다른 답변과 [3]을 참고해 bundler 에 openssl의 경로를 찾을 수 있도록 옵션을 추가하고 다시 설치한다.

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

Reference

  1. mysql - Error when trying to install app with mysql2 gem - Stack Overflow
  2. OpenSSL - Wikibooks, open books for an open world
  3. Bundler: bundle config