Fixing the "Library not loaded" error with RVM and the MySQL2 Gem

If you are running RVM and have the MySQL Community edition database on your Mac, you will need the MySQL2 gem to access it from Rails. If you install the Gem (gem install mysql2), and then try to start WEBrick, you get this scary error:
/Users/cosjef/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.7/lib/mysql2.rb:9:in `require’: dlopen(/Users/cosjef/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)[/bash]
To fix this error, run the following command, ensuring that the ~/.rvm path to the MySQL 0.3.7 bundle file listed below is accurate for your system:
[bash]sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib ~/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle[/bash]
Note that this command must be run with the “sudo” prefix (which will require a password) in order to write to the required files. You should then be able to start WEBrick successfully.