“Library not loaded: libmysqlclient.16.dylib” fix on Mac OS X

After hours of tweaking and a series of “installing and uninstalling” ruby on rails, I finally got it working using RVM. I am using Rails 2.3.4, for some weird reason, I couldn’t get Rails 3.0 installed.

Anyways, I was following a tutorial on the web and whenever I run rake db:migrate, I get this following error:

kimber:depot huang$ rake db:migrate
(in /Users/huang/depot)
!!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql.
rake aborted!
dlopen(/Users/huang/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/i686-darwin10.6.0/mysql.bundle, 9): Library not loaded: libmysqlclient.16.dylib
Referenced from: /Users/huang/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/i686-darwin10.6.0/mysql.bundle
Reason: image not found - /Users/huang/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/i686-darwin10.6.0/mysql.bundle

(See full trace by running task with --trace)

As you know, unlike Linux, there is no environment variable called LD_LIBRARY_PATH to set. Instead, you would set the DYLD_LIBRARY_PATH to point to your mysql libraries like so:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

Add this line into your .profile file and source it. This should fix this error.