Quick Hit: Library not loaded: libmysqlclient.16.dylib
Posted on April 19, 2011
I haven’t been using mysql much for my rails projects in favor of postgres, but in doing some tutorials on haml and sass I needed mysql in order to follow the tutorials exactly. After adding the mysql2 gem to my Gemfile, I got this error when running rake db:create:
rake aborted! dlopen(/Users/Rebecca/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.16.dylib Referenced from: /Users/Rebecca/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle Reason: image not found - /Users/Rebecca/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle /Users/Rebecca/RubymineProjects/Rails3Tutorial/Rakefile:4:in `<top (required)>'
After reading this post, and futzing around, I got it to work. Here was the fix:
sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql/lib/libmysqlclient.16.dylib ~/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle
What exactly does this do? It basically tells the gem where to find the libmysqlclient.6.dylib.
First, find your gem:
Rebecca$ gem list -d mysql2 *** LOCAL GEMS *** mysql2 (0.2.7) Author: Brian Lopez Homepage: http://github.com/brianmario/mysql2 Installed at: /Users/Rebecca/.rvm/gems/ruby-1.9.2-p180 A simple, fast Mysql library for Ruby, binding to libmysql
Drill down, find the mysql2.bundle file and open it in a text editor. Mostly it’s just jibberish, but you should see the libmysql.16.dylib somewhere. Mine had no absolute path, just the file name. That’s what the libmysql.16.dylib referenced in the above command.
Then you’ll need to find the actual location of the libmyql.16.dylib file. Mine is here: /usr/local/mysql/lib/libmysqlclient.16.dylib
Then you tell the gem bundle to change the reference to libmysql.16.dylib to the correct location and bam! success.