Install mysql2 gem on macOS Sierra

The issue is that you're missing a library as the error message indicates

ld: library not found for -l-lpthread

EDIT: There seem to be other errors related that may be fixed with the instructions below, namely:

ld: library not found for -lssl

My guess is that you did not install xcode yet which happens to install a few more libraries. Please make sure to install xcode through the official app-store.

It might be necessary to re-install the command-line tools again as well (even if you had xcode installed and just updated it at some point).

xcode-select --install

Let me know if this helped!


Fix:

Edit /usr/local/Cellar/mysql-connector-c/6.1.10/bin/mysql_config, find this line:

libs="$libs -l "

and change it to

libs="$libs -l mysqlclient "

Explanation:

-l-lpthread

The linker does not understand the option -l-lpthread. Two -l-l linker options are jammed up against each other. It is because the library name mysqlclientis missing from the generated make file.

I ran into this issue when trying to build the native extensions for the mysql2 gem on Ruby 2.4.1 using the mysql-connector-c from Home Brew

This was on MacOS 10.12.5.

The generated LIBS variable should look something this:

LIBS = $(LIBRUBYARG_SHARED) -L/usr/local/Cellar/mysql-connector-c/6.1.10/lib -l mysqlclient -lpthread -ldl -lobjc

It appears the variable is expanded from the file /usr/local/Cellar/mysql-connector-c/6.1.10/bin/mysql_config

The libs var in the file mysql_config should contain:

libs="$libs -l mysqlclient "

instead of

libs="$libs -l "

The var embedded_libs may be wrong too?

The mysql-connector-c lib installs and build fine via Home Brew it just appears the file mysql_config is incorrect or generated incorrectly.

Not sure the cause of the issue. Possibly Home Brew, mysql-connector-c, mysql2 gem build process, user environment?