gem install mysql2

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.

Solution

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

brew install cmake

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

Hope this helps save someone some time!

Andrew Mason's profile image

Hey, I'm Andrew 👋

I'm a senior product engineer at Podia, co-host of Remote Ruby and Ruby for All, and co-editor of Ruby Radar. You can explore my writing, learn more about me, or subscribe to my RSS feed.

Page Info

Published:
11 December 2020
Updated:
29 January 2022
Reading Time:
1 min read

Tags