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/binfolder. You can fix it with a quick
$ 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.
Thank you so much for this. It was the exact answer that I needed. I appreciate you taking the time to write this.
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!
That one with the recent fix worked. Thanks Aditya.
The command worked fine for me too. Thank you Aditya.
Thanks so much for this page!! I have spent more hours then I won’t to admit on this. Thank you Aditya
When are you going make it work with MAMP ‘MAMP Pro’ am interest on it.
Thanks for the updated version Aditya.
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: http://ow.ly/4C8RY (haven’t tested it myself)
better fix for the load error is to set the systems path for finding dynamic libraries. i.e. put:
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
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
This worked great for me too for mysql2 version 0.2.7
thanks for the post!
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
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.
Thanks for posting the fix.
Another solution to the error is:
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.