Friday, July 29, 2011

Getting error "Library not loaded: libmysqlclient.18.dylib (LoadError)" on Mac

On Mac, successfully installed RVM, ruby 1.9.2, mysql2 gem, Rails 3.1.0.rc5.
On starting rails server or rails console, getting following errors:

:restaurant ror$ rails c
/Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:236:in `require': dlopen(/Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/mysql2-0.3.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)
  Referenced from: /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/mysql2-0.3.6/lib/mysql2/mysql2.bundle
  Reason: image not found - /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/mysql2-0.3.6/lib/mysql2/mysql2.bundle
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:236:in `block in require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:222:in `block in load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:616:in `new_constants_in'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:222:in `load_dependency'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/activesupport/lib/active_support/dependencies.rb:236:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/mysql2-0.3.6/lib/mysql2.rb:9:in `<top (required)>'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:68:in `require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:66:in `each'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:66:in `block in require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:55:in `each'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler/runtime.rb:55:in `require'
    from /Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.15/lib/bundler.rb:120:in `require'
    from /Users/ror/projects/restaurant/config/application.rb:7:in `<top (required)>'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands.rb:38:in `require'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/bundler/gems/rails-30dae273c85a/railties/lib/rails/commands.rb:38:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

  Solution to fix the problem:
Type following command and then run Rails server or console:

-:restaurant ror$ export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

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

9 comments:

  1. Thanks! The 'export' bit worked for me on rvm ruby-1.9.2-p290.

    ReplyDelete
  2. Made my day, Export works fine for me on ruby 1.9.2

    ReplyDelete
  3. OMG THANK YOU SO MUCH. I'm a Ruby noob and I've spent HOUR on end trying to fix this. THANK YOU THANK YOU THANK YOU!

    ReplyDelete
  4. Thank you! Pasting the soft link straight in solved the problem. I don't have much of an idea as to what I am doing so having something simple point out my error and solve the issue all at once was fantastic

    ReplyDelete
  5. unreal thank you so much, spent 3 hours posting in various forums about this error.

    ReplyDelete
  6. Yep, worked for me too! Something seems to have changed in how Rails 3.1.x is identifying mysql libs (via DYLD_LIBRARY_PATH). Oh well, it works!

    ReplyDelete
  7. Another solution is add this

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

    to your ~/.bash_profile, don't forget to source it after:
    $ source ~/.bash_profile

    ReplyDelete
  8. Hello, my problem is

    $ rails server
    dyld: lazy symbol binding failed: Symbol not found: _mysql_get_client_info
    Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.3.13/lib/mysql2/mysql2.bundle
    Expected in: flat namespace

    dyld: Symbol not found: _mysql_get_client_info
    Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.3.13/lib/mysql2/mysql2.bundle
    Expected in: flat namespace

    After trying your soft link, still doesnt work, can you please help me?

    $ sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
    dyld: DYLD_ environment variables being ignored because main executable (/usr/bin/sudo) is setuid or setgid
    ln: /usr/lib/libmysqlclient.18.dylib: File exists

    ReplyDelete