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
Posted by Sinoob on October 7, 2010 at 1:21 AM
Awesome … Thanks dude … you saved me some pain!
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.
Posted by escueladesuperheroes.com on June 25, 2011 at 9:37 AM
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
Posted by Seb James on December 17, 2010 at 3:35 AM
Thanks for posting this, I was pulling my hair out.
Posted by Alexey Medvedev on December 24, 2010 at 10:05 AM
Man, you save me! Thanks!
Posted by Martin Clifford on December 26, 2010 at 8:22 PM
Thanks- this “just” worked!!
Posted by Tobias on December 28, 2010 at 6:32 AM
Works like charme. That tip saved me a lot of time. Thank you.
Posted by Giorgio Previtera on December 28, 2010 at 5:12 PM
Man, you are THE man!
Posted by Adi Luhung on January 2, 2011 at 9:55 AM
finally!! works! 🙂 million thanks dude!
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
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
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.
Posted by John on January 23, 2011 at 12:33 PM
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
Posted by Al on February 15, 2011 at 12:31 AM
Yeppers. I picked up on this one. Thanks.
Posted by Nadav Gur on January 26, 2011 at 5:59 PM
Man, if we ever meet, please wear clean boots cause I’m gonna kiss them.
Posted by Noel Geren on February 11, 2011 at 9:38 AM
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/ .
Posted by Junior on February 14, 2011 at 4:19 PM
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.
Posted by Al on February 15, 2011 at 12:30 AM
Thanks a million big homie. You rock! Passing this along to some friends.
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.
Posted by Kevin Menzie on February 17, 2011 at 4:05 PM
Thank you! You’ve made what could have been an otherwise unfriendly entry into Ruby on Rails painless.
Posted by marklai.com » MySQL, Ruby on Rails, and Mac OS X on February 19, 2011 at 6:42 PM
[…] out rails & gem still don’t work well with MySQL 5.5. (note: updating mysql2.bundle with the libmysqlclient library path might […]
Posted by DarkHcK on February 27, 2011 at 9:43 AM
Thank you very-very much
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
Posted by Elvis Fernandes on March 17, 2011 at 3:15 AM
Thank you veeery much!
Your post saved me not only or twice, but lots of times!
Posted by Ali on March 18, 2011 at 11:15 AM
Thanks a ton, it just worked 🙂
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.
Posted by Sébastien Grosjean on March 31, 2011 at 10:55 PM
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/
Posted by mysql2 ruby gem and Mac OS X: image not found | Samuel Kadolph on April 1, 2011 at 7:03 AM
[…] far the only solution I have found online is to use install_name_tool to update the (extremely) relative libmysqlclient.16.dylib reference to […]
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.
Posted by Vlad on April 13, 2011 at 1:29 AM
Honestly not sure what i would have done without this – thank you!
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
Posted by victorjtorres on June 13, 2011 at 6:27 PM
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
Posted by Steve Batcup on June 24, 2011 at 4:48 AM
Invaluable – thanks very much
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.
Posted by Bob on July 1, 2011 at 1:41 PM
Was bloody losing it. Now up and running. Cheers!
Posted by Ritesh Kumar on August 2, 2011 at 11:03 AM
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
Posted by Simon on November 22, 2011 at 3:41 PM
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.
Posted by Timothy Mensing on February 7, 2012 at 12:00 AM
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 🙂
Posted by Timothy Mensing on February 7, 2012 at 12:08 AM
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
Posted by Anthony To on June 20, 2012 at 11:46 AM
You are a god among us mortal men. Many thanks.