rails-mysql rake db :create - Library not loaded libmysqlclient.20.dylib image not found

16,915

Solution 1

The fix was to

gem uninstall mysql2

then reinstall it again with

bundle  # gem install mysql2 would also work

This recompiles and resolves the reference that had been left over from having mysql 5.7.9 installed.

Solution 2

OS X is not able to find the libmysqlclient.18.dylib library. To make it accessible, We can do the following :

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

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

$ source ~/.bash_profile

Then:

 sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
Share:
16,915
Michael Durrant
Author by

Michael Durrant

rails ruby rspec rock

Updated on June 05, 2022

Comments

  • Michael Durrant
    Michael Durrant almost 2 years

    Reason: image not found - /Users/mdurrant/.rvm/gems/ruby-2.1.5/extensions/x86_64-darwin-14/2.1.0-static/mysql2-0.3.20/mysql2/mysql2.bundle

    Our application requires mysql version 5.6 which seems to be working ok -

    $ mysql -uroot
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 1
    Server version: 5.6.27 MySQL Community Server (GPL)
    Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    mysql> 
    

    However when I try to create the database from rails I get:

    $ rake db:create
    rake aborted!
    LoadError: dlopen(/Users/mdurrant/.rvm/gems/ruby-2.1.5/extensions/x86_64-darwin-14/2.1.0-static/mysql2-0.3.20/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib
      Referenced from: /Users/mdurrant/.rvm/gems/ruby-2.1.5/extensions/x86_64-darwin-14/2.1.0-static/mysql2-0.3.20/mysql2/mysql2.bundle
      Reason: image not found - /Users/mdurrant/.rvm/gems/ruby-2.1.5/extensions/x86_64-darwin-14/2.1.0-static/mysql2-0.3.20/mysql2/mysql2.bundle
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/mysql2-0.3.20/lib/mysql2.rb:31:in `<top (required)>'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:in `require'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:in `each'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:in `block in require'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:in `each'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:in `require'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/bundler-1.10.6/lib/bundler.rb:134:in `require'
    /Users/mdurrant/eq/lynx/config/application.rb:10:in `<top (required)>'
    /Users/mdurrant/eq/lynx/Rakefile:10:in `require'
    /Users/mdurrant/eq/lynx/Rakefile:10:in `<top (required)>'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `eval'
    /Users/mdurrant/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `<main>'
    (See full trace by running task with --trace)
    14:18:13 mdurrant EQ-267 /Users/mdurrant/eq/lynx master
    $ 
    
  • Takarii
    Takarii over 7 years
    Add more detail to your answer and format it to make it more readable.
  • jbarr
    jbarr over 5 years
    For those wondering what the cause of this may be—My loose understanding is that it is related to the fact that when mysql2 is installed, it creates native extensions for the current Ruby _version (see gems/ruby-2.1.5/extensions in the stacktrace?). If you then change Ruby versions without re-installing mysql2, the existing natives extensions won't be able to find the image since it is still under the old Ruby version. So you should reinstall mysql2 after changing ruby versions.
  • Mik
    Mik over 2 years
    works on Apple M1 2021 after having fixed issues with multiple installations of mysql