rails + MySQL on OSX: Library not loaded: libmysqlclient.18.dylib

I'm just starting out with Ruby (and rails). I did the setup according to http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:ruby gems, using rvm. I have everything working well with sqlite.

Now I'd like to try converting things over to MySQL, since that's what I do most of my development with. In my Gemfile I've replaced sqlite with mysql2:

group :development, :test do
#  gem 'sqlite3', '1.3.5'
  gem 'mysql2'
  gem 'rspec-rails', '2.9.0'
end

But when I try to create the DB for rails in MySQL I get:

$ rake db:create --trace
rake aborted!
dlopen(/Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
  Reason: image not found - /Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle

I've seen other postings recommending re-installing MySQL via homebrew (mine was installed via a downloadable DMG), but I'd prefer not to do that as I have several other databases in there already for other non-ruby projects.

I do in fact have the file that Rails is looking for; it's installed in /usr/local/mysql/lib/libmysqlclient.18.dylib. What's the best way to tell Rails how to locate it?


Solution 1:

The solution is pretty easy; Add the library path in your ~/.bash_profile or ~/.profile file:

MYSQL=/usr/local/mysql/bin
export PATH=$PATH:$MYSQL
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

If it is still not working (this work for me):

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

There are many blogs with install_name_tool, which won't work for me because I'm on OSX Lion:

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/bin/indexer
sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/bin/search

Solution 2:

In El Capitan I got ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted

In El Capitan /usr/lib/ now has a restricted flag and can't be written to without disabling security so I just put the link in /usr/local/lib instead.

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

Rails server is running fine again.

Solution 3:

While the title of this question describes precisely the problem I encountered, the circumstances are different from those described in the previous answers, and so was the solution.

In my case (El Capitan, mysql installed via homebrew), a brew update && brew upgrade caused the mysql package to be upgraded to 5.7.10 (from 5.6.x).

The upgrade caused libmysqlclient.18.dylib to be replaced with libmysqlclient.20.dylib, but the mysql2 gem was still relying on the former.

To fix the problem I did: gem uninstall mysql2 && gem install mysql2

Please note that similar problems can occur with different homebrew-managed libraries (see my own answer to this, for example)

Solution 4:

sudo ln -s /usr/local/mysql-5.5.25-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

That worked for me. I installed MySQL from a dmg file.