Mysql 5.5, Snow leopard and rails

I had some issues getting this to work with this error message:

LoadError: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.2.4/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.16.dylib
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.2.4/lib/mysql2/mysql2.bundle
Reason: image not found – /Library/Ruby/Gems/1.8/gems/mysql2-0.2.4/lib/mysql2/mysql2.bundle
from /Library/Ruby/Gems/1.8/gems/mysql2-0.2.4/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.4/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

After a few (hundred) tries I found that you need to change the mysql2.bundle to get this to work, and here is how…

First installing mysql 5.5 from dev.myql.com/downloads using the dmg package… easy as pie….

Second ruby/rails/gems have other issues that I don’t want to go into BUT have rails running then install mysql2 gem using the normal command sudo gem install mysql2

Now is the “tricky” part:

sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql/lib/libmysqlclient.16.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.2.4/lib/mysql2/mysql2.bundle

This makes a change in the mysql2.bundle file that is not using the full path for libmysqlclient library…

then to test fire up irb

>> require ‘mysql2’
=> true

40 responses to this post.

  1. Posted by Sinoob on October 7, 2010 at 1:21 AM

    Awesome … Thanks dude … you saved me some pain!

    Reply

  2. Posted by steve on October 9, 2010 at 1:10 AM

    Thank you!

    For rvm, the second parameter changes, of course. I used this…

    sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql/lib/libmysqlclient.16.dylib ~/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.4/lib/mysql2/mysql2.bundle

    and it worked like a charm!

    Too bad the gem needs to be altered by hand, but at least it’s working now. Thanks again.

    Reply

    • I just went to my directories and check all my files, then I change ‘mysql2-0.2.4’ for ‘mysql2-0.2.11’ and the in the ruby directory I create with RVM and work sweeeeeeeeeeeeeeeeeeeeeeeeeeeet, I my god I really loss some hair with this, buy I’m glad is finally working,
      Thanks man….
      Ima

      Reply

  3. Posted by Seb James on December 17, 2010 at 3:35 AM

    Thanks for posting this, I was pulling my hair out.

    Reply

  4. Posted by Alexey Medvedev on December 24, 2010 at 10:05 AM

    Man, you save me! Thanks!

    Reply

  5. Posted by Martin Clifford on December 26, 2010 at 8:22 PM

    Thanks- this “just” worked!!

    Reply

  6. Works like charme. That tip saved me a lot of time. Thank you.

    Reply

  7. Posted by Giorgio Previtera on December 28, 2010 at 5:12 PM

    Man, you are THE man!

    Reply

  8. finally!! works! 🙂 million thanks dude!

    Reply

  9. Posted by ainstushar on January 2, 2011 at 12:49 PM

    Thanks!! After spending hours on this problem, I found a link to this page on stackoverflow.

    For the 0.2.6 mysql2, and ruby 1.9.2, the path needs to be changed to

    sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql/lib/libmysqlclient.16.dylib /usr/local/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

    Reply

  10. Posted by Oleg on January 3, 2011 at 9:18 AM

    Thanks, this worked for me:
    sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql/lib/libmysqlclient.16.dylib ~/.rvm/gems/ruby-1.9.2-p136/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

    Reply

  11. Posted by Jay Cincotti on January 21, 2011 at 1:16 PM

    Thank you. Like everyone else, I’ve been banging my head against this wall for a couple days now.
    I had to downgrade to the mySQL gem 2.7 (from 2.8.1) to even get to this error. Prior I was getting an error with hundreds of “ARCHFLAG = …” solutions that wouldn’t work for me.

    Reply

  12. Just a note: change ‘mysql2-0.2.4’ part of the path in the command to the reflect the version of the mysql2 gem you are using. For example, I installed the latest mysql gem, version 0.2.6, so I replace the original mysql2 directory to ‘mysql2-0.2.6/’:

    sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql/lib/libmysqlclient.16.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

    Reply

  13. Man, if we ever meet, please wear clean boots cause I’m gonna kiss them.

    Reply

  14. Thanks for the post! This definitely helped in my quest for getting MySQL 5.5, Ruby 1.8.7, Rails 2.3.11 and the mysql gem 2.8.1 to play nice together! I’ve posted steps on my blog @ http://pushandpop.blogspot.com/ .

    Reply

  15. You saved my life. I waste a couple of hours trying to figure out what was going on and then I found this blog. Thanks a lot man.

    Reply

  16. Thanks a million big homie. You rock! Passing this along to some friends.

    Reply

  17. Posted by Kurt Bieg on February 17, 2011 at 12:37 PM

    Dude, seriously … thank you sooooo very much. Been struggling with this for half a day. If you’re in nyc … dinner’s on me.

    Reply

  18. Thank you! You’ve made what could have been an otherwise unfriendly entry into Ruby on Rails painless.

    Reply

  19. […] out rails & gem still don’t work well with MySQL 5.5. (note: updating mysql2.bundle with the libmysqlclient library path might […]

    Reply

  20. Posted by DarkHcK on February 27, 2011 at 9:43 AM

    Thank you very-very much

    Reply

  21. Posted by Daniel Olawale on March 6, 2011 at 10:46 AM

    Thank you ever so much for this. It works very well. God bless you

    Reply

  22. Thank you veeery much!

    Your post saved me not only or twice, but lots of times!

    Reply

  23. Thanks a ton, it just worked 🙂

    Reply

  24. Posted by Andy Gray on March 21, 2011 at 3:48 PM

    THANK YOU!!!!!!
    I’ve been pulling what little hair I had left out over this issue until I tried your solution.

    Reply

  25. Hi,

    You might want to consider an other option. The one you describe work but won’t persist over time neither solve the real issue.

    The problem comes from the mysql2 gem missing the dynamic library from MySQL.

    A cleaner solution would need to update your DYLD_LIBRARY_PATH to add MySQL libs to it. To do so, update your ~/.bash_profile to add the MySQL library folder :

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

    Note: You might want to update the mysql location based on your install

    This should keep things clean but also ensure that any gem or code requiring MySQL dynamic libraries will find them.

    Reference : http://lightyearsoftware.com/2011/02/mysql-5-5-on-mac-os-x/

    Reply

  26. […] far the only solution I have found online is to use install_name_tool to update the (extremely) relative libmysqlclient.16.dylib reference to […]

    Reply

  27. Posted by RSturim on April 2, 2011 at 3:55 AM

    Just a heads — blink copy and pasting (like I did) will cause much frustration. Turns out I have “libmysqlclient.18” installed — not libmysqlclient.16. If I have more closely read my own error messages I could have saved a lot of time. Thanks all.

    Reply

  28. Posted by Vlad on April 13, 2011 at 1:29 AM

    Honestly not sure what i would have done without this – thank you!

    Reply

  29. Posted by SolDarko on May 25, 2011 at 1:20 PM

    Thank You so much for this fix. I changed my lib to 18 and version of mysql with the magic command and added the export path in .profile.

    sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle

    Reply

  30. Even with these instructions it doesn’t seem to work for me. I try and copy and paste directly and it won’t take it. It just copies my input and adds (no such file or directory)” – Can someone please advise on how to proceed?

    Thanks

    Reply

  31. Posted by Steve Batcup on June 24, 2011 at 4:48 AM

    Invaluable – thanks very much

    Reply

  32. Posted by Christopher on June 29, 2011 at 12:38 AM

    Thank you!!! I tried for days to fix this issue untill I found this site.

    Reply

  33. Posted by Bob on July 1, 2011 at 1:41 PM

    Was bloody losing it. Now up and running. Cheers!

    Reply

  34. Another way is to create a soft link by command –
    sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

    http://rorguide.blogspot.com/2011/07/getting-error-library-not-loaded.html

    Reply

  35. Try this if you have problems:

    sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.3.10/lib/mysql2/mysql2.bundle

    Hint: To start up IRB, type irb in the terminal.

    Reply

  36. Current modification to original post (I’m a noob, just so you know):

    —–
    I have mysql2 3.11 installed, which I found by looking at my image not found error message (which had 3.11 next to mysql2-3.11) and changed my gem lib to 18 (don’t know why yet, I used SoulDarko’s reply advice above).

    After entering sudo gem install mysql2 in my terminal and pressing enter (I had to do this first), I then entered the following, pressed enter and it worked.

    sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle
    ——

    Thanks for the help everyone, its much appreciated.

    – T 🙂

    Reply

  37. Correction from above, I meant to say 0.3.11 not just 3.11 , I used the code below and it worked (after typing into the terminal and hitting enter: sudo gem install myql2):

    sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle

    Reply

  38. Posted by Anthony To on June 20, 2012 at 11:46 AM

    You are a god among us mortal men. Many thanks.

    Reply

Leave a reply to Sébastien Grosjean Cancel reply