Fixing “ld: library not found for -lmysqlclient_r” during “gem install mysql2″ on OSX w/ MAMP

Fixing “ld: library not found for -lmysqlclient_r” during “gem install mysql2″ on OSX w/ MAMP

I have been trying to install mysql2 on ruby gems and running into an issue where the following error below would be thrown. I already have MAMP installed, so I know mysql is there, but for some reason ruby didn’t like that.

The error I was receiving:

jgilmour@mb-pro ~> sudo gem install mysql2 — –with-mysql-configs=/Applications/MAMP/Library/bin/mysql_configBuilding native extensions.  This could take a while…
ERROR:  Error installing mysql2:
ERROR: Failed to build gem native extension.
/opt/local/bin/ruby extconf.rb –with-mysql-configs=/Applications/MAMP/Library/bin/mysql_config
checking for rb_thread_blocking_region()… yes
checking for mysql.h… yes
checking for errmsg.h… yes
checking for mysqld_error.h… yes
creating Makefile
make

/usr/bin/gcc-4.2 -I. -I/opt/local/include/ruby-1.9.1/x86_64-darwin10 -I/opt/local/include/ruby-1.9.1/ruby/backward -I/opt/local/include/ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -I/opt/local/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -I/Applications/MAMP/Library/include/mysql  -fno-omit-frame-pointer   -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT  -DDONT_DECLARE_CXA_PURE_VIRTUAL -fno-common -pipe -O2 -arch x86_64 -fno-common -Wall -funroll-loops  -o client.o -c client.c

/usr/bin/gcc-4.2 -I. -I/opt/local/include/ruby-1.9.1/x86_64-darwin10 -I/opt/local/include/ruby-1.9.1/ruby/backward -I/opt/local/include/ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -I/opt/local/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -I/Applications/MAMP/Library/include/mysql  -fno-omit-frame-pointer   -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT  -DDONT_DECLARE_CXA_PURE_VIRTUAL -fno-common -pipe -O2 -arch x86_64 -fno-common -Wall -funroll-loops  -o mysql2_ext.o -c mysql2_ext.c

/usr/bin/gcc-4.2 -I. -I/opt/local/include/ruby-1.9.1/x86_64-darwin10 -I/opt/local/include/ruby-1.9.1/ruby/backward -I/opt/local/include/ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -I/opt/local/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -I/Applications/MAMP/Library/include/mysql  -fno-omit-frame-pointer   -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT  -DDONT_DECLARE_CXA_PURE_VIRTUAL -fno-common -pipe -O2 -arch x86_64 -fno-common -Wall -funroll-loops  -o result.o -c result.c

/usr/bin/gcc-4.2 -dynamic -bundle -o mysql2.bundle client.o mysql2_ext.o result.o -L. -L/opt/local/lib -L/opt/local/lib -L. -L/opt/local/lib -arch x86_64 -L/usr/local/lib -Wl,-rpath,/Applications/MAMP/Library/lib/mysql -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress -Wl,-flat_namespace  -lruby.1.9.1 -L/Applications/MAMP/Library/lib/mysql -lmysqlclient_r -lz -lm  -lpthread -ldl -lobjc

ld: library not found for -lmysqlclient_r
collect2: ld returned 1 exit status
make: *** [mysql2.bundle] Error 1
Gem files will remain installed in /opt/local/lib/ruby1.9/gems/1.9.1/gems/mysql2-0.3.2 for inspection.
Results logged to /opt/local/lib/ruby1.9/gems/1.9.1/gems/mysql2-0.3.2/ext/mysql2/gem_make.out

After much frusteration, I started to follow the guide here: MAMP 1.9.5 / MySQL 5.5.9 and Ruby mysql2 but it still wasn’t working!

What I did to fix it:

cd /Applications/MAMP/Library/lib/mysql/
ln -s  ibmysqlclient.la ibmysqlclient.a
ln -s libmysqlclient_r.la libmysqlclient_r.a
ln -s libndbclient.la libndbclient.a

And it worked!

jgilmour@mb-pro /Applications/MAMP/Library/lib/mysql> sudo gem install mysql2 — –with-mysql-configs=/Applications/MAMP/Library/bin/mysql_config
Building native extensions.  This could take a while…
Successfully installed mysql2-0.3.2
1 gem installed
Installing ri documentation for mysql2-0.3.2…
Enclosing class/module ‘mMysql2′ for class Client not known
Installing RDoc documentation for mysql2-0.3.2…
Enclosing class/module ‘mMysql2′ for class Client not known

9 comments

  1. working!!! thanks.
    in case someone wants to use bundler instead of gem install, i did this:

    bundle config build.mysql2 –with-mysql-config=/Applications/MAMP/Library/bin/mysql_config

    and then

    sudo bundle install

  2. This is amazing. THANK YOU.

  3. Thanks for posting this.

    I’m curious, how did you end up with that solution?

  4. didn’t work for me
    MacOS 10.7.2
    Mysql 5
    Ruby 1.9
    RVM

  5. Thanks so much!

  6. Thanks, you save my life!

Leave a Reply

Scroll To Top
Descargar musica