简体   繁体   中英

Install mysql2 gem on macOS Sierra

I'm trying to install mysql2 gem (0.4.5) on macOS Sierra (10.12.1) and getting the error. I don't have local mysql server, it's remote.

Here is error log:

checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes 
checking for rb_intern3()... yes 
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not    in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql-connector-c/6.1.9/lib
-----
creating Makefile

To see why this extension failed to compile, please check the mkmf.log   which can be found here:
    /Users/user/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-16/2.3.0-static/mysql2-0.4.4/mkmf.log

current directory: /Users/user/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/mysql2-0.4.4/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/user/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/mysql2-0.4.4/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 -l-lpthread
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Did anyone meet similar error?

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 -ll linker options are jammed up against each other. It is because the library name mysqlclient is 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?

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!

I'm not sure if the issue is that the mysql-connector-c formula is broken in Homebrew, or if the issues lies in the mysql2 gem, but you can work around the issue by installing the mysql formula.

Even if you don't need a local MySQL server for development, the version of mysql_config from the mysql formula correctly returns the list of libraries required to link the gem's native extension.

If you already have mysql-connector-c installed:

brew unlink mysql-connector-c
# OR
brew uninstall mysql-connector-c

Install the mysql formula:

brew install mysql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM