How to Fix: Library not loaded: libmysqlclient.18.dylib

9th July, 2011 Patrick

Just installed OS X Lion and after installing RVM, Rails, and MySQL I went to got start one of my rails projects and received this great message.

/Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.3/lib/mysql2.rb:7:in `require': dlopen(/Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.3/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)
  Referenced from: /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.3/lib/mysql2/mysql2.bundle
  Reason: image not found - /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.3/lib/mysql2/mysql2.bundle
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.3/lib/mysql2.rb:7:in `<top (required)>'
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.15/lib/bundler/runtime.rb:68:in `require'
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.15/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.15/lib/bundler/runtime.rb:66:in `each'
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.15/lib/bundler/runtime.rb:66:in `block in require'
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.15/lib/bundler/runtime.rb:55:in `each'
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.15/lib/bundler/runtime.rb:55:in `require'
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.15/lib/bundler.rb:120:in `require'
    from /Users/patrick/Workspace/MonkeyOnCode/config/application.rb:7:in `<top (required)>'
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.0/lib/rails/commands.rb:28:in `require'
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.0/lib/rails/commands.rb:28:in `block in <top (required)>'
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.0/lib/rails/commands.rb:27:in `tap'
    from /Users/patrick/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.0/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

This is simply an issue with mysql2 not being able to find the MySQL provided dynamic library. The easy way to fix this is simple add the below value to your ~/.bash_profile and you will need to source the file again or start a new terminal.

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