Ruby: mysql2-Gem not working (Mac OS X Snow Leopard, Ruby 1.9.2)
I just compiled ruby and installes rubygems, mysql2 and rails, which worked quite well. But I get the following error message, whenever I try to start the rails server:
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2.rb:7:in `require': dlopen(/usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError) Referenced from: /usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle Reason: image not found - /usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2.rb:7:in `' from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:68:in `require' from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:68:in `block (2 levels) in require' from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:66:in `each' from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:66:in `block in require' from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:55:in `each' from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:55:in `require' from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler.rb:120:in `require' from /Users/filzo/Documents/rails/blog/config/application.rb:7:in `' from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:28:in `require' from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:28:in `block in ' from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:27:in `tap' from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:27:in `' from script/rails:6:in `require' from script/rails:6:in `'
I tried several possible solutions (e.g. this: http://railsforum.com/viewtopic.php?pid=23125#23125 with libmysqlclient.18.dylib); but nothing worked for me. I hope you can help me.
EDIT: It seems like this fixed the problem for me:
sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
Anyway; is it a bug in the MySQL or the ruby-code?
One of the simple and best way is to create a soft link by command -
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
This appears to be a known (and unfixed!) bug in the MySQL binary distribution.
Whilst running install_name_tool on your mysql2.bundle files will work, if you remove and rebuild them you're back to square one. Also, you'll have to apply this fix to anything else that links against it. A better solution is to fix the problem in the library itself:
$ sudo install_name_tool -id \ /usr/local/mysql/lib/libmysqlclient.18.dylib \ /usr/local/mysql-5.5.12-osx10.6-x86_64/lib/libmysqlclient.18.dylib
Removing and rebuilding the mysql2 gem:
$ gem uninstall mysql2 $ gem install mysql2
...will pick up the change to the library, and everything should work correctly, without having to tinker with any environment variables.
If you are using RVM (an excellent choice) on Mac OS X - here is the magic command that finally worked for me.
sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Users/*USERNAME*/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
Replace USERNAME in the syntax appropriately as per your current setup. And if your version of mysql gem is different change that as well.
A better alternative to install_name tool is to add a path the the mysql lib to your DYLD_LIBRARY_PATH environment variable. In /etc/profile:
Or, as I have the Oracle Instant Client installed as well, do:
export ORACLE_HOME="/usr/local/oracle/instantclient_10_2" export DYLD_LIBRARY_PATH=$ORACLE_HOME export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql/lib
Try to make this symbolic link, so rake can find the correct MySQL client lib.
ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
I'm not sure if this will help or not, but I'm using OS X 10.7.1 Lion. In my case, I opened a terminal window, went to /Library/Ruby/Gems/1.8/gems/mysql2-0.3.7 and as soon as I did RVM barked saying it encountered a new or modified .rvmrc file. The contents of that file was:
rvm use 1.9.2@mysql2 --create
It asked if I wanted to trust this .rvmc file. I said yes, and it returned the following:
Using /Users/dan/.rvm/gems/ruby-1.9.2-p290 with gemset mysql2
Once it did, I ran the following command:
sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle
Once I did, I was set to go. Hope this helps!