Scroll to top

Running MySQL 5.5 and Rails 3 on Mac OS X

Ali - March 22, 2011 - 17 comments

A recent opportunity made me dust off my development skills and I decided to use Ruby on Rails as the framework of choice.

I’m happy with Ruby 1.8.7 for now (comes with Mac OS X Snow Leopard), but I had to update Rails to version 3, which was a painless process. After installing MySQL 5.5.10 (dumping MAMP for now), I installed the ruby gem for MySQL:

$ sudo gem install mysql2

You may want to run this too, although the first one should do the trick:

$ sudo gem install mysql

A quick test got me this piece of error:

LoadError: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
Reason: image not found – /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
from /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require’
from /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2.rb:7
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require’
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require’
from (irb):1

You can never have a perfect installation experience. Can you?

A thread on Stack Overflow guided me to this solution:

$ sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.10-osx10.6-x86_64/lib/libmysqlclient.18.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

Note: As it happened with me, you might have the same problem that MySQL failed to create a link (alias) in the /usr/bin folder. You can fix it with a quick ln command:

$ sudo ln /usr/local/mysql-5.5.10-osx10.6-x86_64/bin/mysql /usr/bin/mysql

PS: This solution is based on the latest versions of the libraries available as of March 2011. You might need to change the versions in the commands or paths according to your machine settings.

Related posts


  1. Richard

    Thank you so much for this. It was the exact answer that I needed. I appreciate you taking the time to write this.

  2. Aditya

    The recent fix being sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle and also one has to update to mysql2 gem version 0.27 with ‘sudo gem install mysql2’ before trying it out. Cheers!

  3. Rick D

    That one with the recent fix worked. Thanks Aditya.

  4. Dani Carvalho

    The command worked fine for me too. Thank you Aditya.

  5. Josh

    Thanks so much for this page!! I have spent more hours then I won’t to admit on this. Thank you Aditya

  6. leonel


    When are you going make it work with MAMP ‘MAMP Pro’ am interest on it.

    Thank you

  7. Ali

    Thanks for the updated version Aditya.

  8. Ali

    I haven’t tried it myself, but I believe if you just replace the path to MySQL library file (libmysqlclient) with the one installed with MAMP, it should work (of course, while MAMP is running).
    There is also a guide I just found that I hope fixes your issue: (haven’t tested it myself)

  9. Martin

    better fix for the load error is to set the systems path for finding dynamic libraries. i.e. put:
    export DYLD_LIBRARY_PATH=”/usr/local/mysql/lib:$DYLD_LIBRARY_PATH”
    in your ~/.bash_profile
    this works for all references to libmysqlclient.18.dylib, not just the one in (one) ruby if you use rvm and several rubies

  10. Hays

    OMG that was annoying… here is the latest:

    sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.12-osx10.6-x86_64/lib/libmysqlclient.18.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle

  11. This worked great for me too for mysql2 version 0.2.7

    thanks for the post!

  12. Gideon Jadlovker

    Thanks so much. not sure why rails is looking for mysql 0.2.11 (while 0.3.6 is out)

    here my version (including the RVM directories)

    sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.14-osx10.6-x86_64/lib/libmysqlclient.18.dylib /Users/GidApple/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.2.11/lib/mysql2/mysql2.bundle

    sudo ln /usr/local/mysql-5.5.14-osx10.6-x86_64/bin/mysql /usr/bin/mysql

  13. Thanks, I was using command “export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH” when I found soft link command “sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib” which solved my problem.

    Ritesh Kumar

  14. Thanks for posting the fix.

  15. Dr. Spock

    Another solution to the error is:

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

  16. Dave Peters

    In the world of Mountain Lion, and particularly in the context of setuid/sudo execution where DYLD_LIBRARY_PATH is ignored, the install_name_tool invocation is particularly useful. Many thanks.

Post a Comment

Your email address will not be published. Required fields are marked *