gem install mysql2
Andrew Mason

Andrew Mason @andrewmcodes

About: Andrew Mason is a full stack Ruby on Rails engineer and content creator in the Ruby community.

Location:
Phoenix, AZ
Joined:
Jan 24, 2019

gem install mysql2

Publish Date: Dec 11 '20
13 0

I've come across this error several times throughout my development career so I figured it was finally time to write it down.

Scenario

Whenever I try to install certain versions of the mysql2 gem in a Ruby on Rails application, I get the following error:

Gem::Ext::BuildError: ERROR: Failed to build gem native extension
...
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

An error occurred while installing mysql2 (0.5.2), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.2'` succeeds before bundling.
Enter fullscreen mode Exit fullscreen mode

Solution

In order to fix this issue on macOS, first make sure that you have cmake installed.

brew install cmake
Enter fullscreen mode Exit fullscreen mode

Then you can install the gem via the following command:

gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
Enter fullscreen mode Exit fullscreen mode

Hope this helps save someone some time!

Comments 0 total

    Add comment