[mysql] rails + MySQL on OSX: Library not loaded: libmysqlclient.18.dylib

I'm just starting out with Ruby (and rails). I did the setup according to http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:ruby gems, using rvm. I have everything working well with sqlite.

Now I'd like to try converting things over to MySQL, since that's what I do most of my development with. In my Gemfile I've replaced sqlite with mysql2:

group :development, :test do
#  gem 'sqlite3', '1.3.5'
  gem 'mysql2'
  gem 'rspec-rails', '2.9.0'
end

But when I try to create the DB for rails in MySQL I get:

$ rake db:create --trace
rake aborted!
dlopen(/Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
  Reason: image not found - /Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle

I've seen other postings recommending re-installing MySQL via homebrew (mine was installed via a downloadable DMG), but I'd prefer not to do that as I have several other databases in there already for other non-ruby projects.

I do in fact have the file that Rails is looking for; it's installed in /usr/local/mysql/lib/libmysqlclient.18.dylib. What's the best way to tell Rails how to locate it?

This question is related to mysql ruby-on-rails ruby macos

The answer is


If you are using MySQL installed from HomeBrew in El Capitan, then you should link it as follows:

sudo ln -sf /usr/local/Cellar/mysql/5.6.27/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

For MySql 5.6 installed from DMG on Mavericks

sudo ln -s /usr/local/mysql-5.6.14-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

I ran into this problem after a complete removal and then fresh install of MySQL. Specifically:

Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib

I had not even touched my Rails app.

Reinstalling the mysql2 gem solved this problem.

$ gem uninstall mysql2
$ gem install mysql2 -v 0.3.18 # (specifying the version found in my Gemfile.lock)

[MySQL 5.7.10, Rails 4.0.0, Ruby 2.0.0, Mac OS X Yosemite 10.10]


My issue with the loading of that bundle file was a bad symlink. So check the link, and replace it with a fresh one if needed. Everything fell into place at that point. Not sure how that happened, but it did. First time that a syntax error happened like that.


gem uninstall -aIx

and

bundle install

worked for me.


I am using Rails REE (2.3.4) for a legacy system we have. After upgrading to El Capitan, running script/console enerated an error and my app would no longer start (using pow):

$ script/console
Loading development environment (Rails 2.3.4)
/blah-blah/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract/connection_specification.rb:76:in establish_connection:RuntimeError: Please install the mysql2 adapter: gem install activerecord-mysql2-adapter (dlopen(/blah-blah/gems/mysql2-0.2.19b4/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /blah-blah/gems/mysql2-0.2.19b4/lib/mysql2/mysql2.bundle
  Reason: image not found - /blah-blah/gems/mysql2-0.2.19b4/lib/mysql2/mysql2.bundle)


From this very thread, above, I determined that I needed to issue this command in terminal:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
This command produced an error: "ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted". I have never seen that error before.

After quite a bit of digging, I found this article: http://www.macworld.com/article/2986118/security/how-to-modify-system-integrity-protection-in-el-capitan.html and followed the instructions to turn SIP (El Capitan's new System Integrity Protection) off. After turning SIP off, and after rebooting, the ln command worked fine. Then I turned SIP off. Now all is fine. My app runs again using pow and no error running script/console. I hope this helps you.


I was working with the rails g model command and I got this error:

Library not loaded: libmysqlclient.18.dylib

I have tried this and it functioned for me. I was using Mavericks 10.9.5

sudo ln -s /usr/local/mysql-5.6.19-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Thanks!

Now I'm using Yosemite 10.10.5 and I got the same error, so I just ran this command on the terminal an it was successfully fixed up.

$ sudo ln -s /usr/local/mysql-5.6.26-osx10.8-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

also you can try:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Both of them work fine for me. Hope it could be useful!


While the title of this question describes precisely the problem I encountered, the circumstances are different from those described in the previous answers, and so was the solution.

In my case (El Capitan, mysql installed via homebrew), a brew update && brew upgrade caused the mysql package to be upgraded to 5.7.10 (from 5.6.x).

The upgrade caused libmysqlclient.18.dylib to be replaced with libmysqlclient.20.dylib, but the mysql2 gem was still relying on the former.

To fix the problem I did: gem uninstall mysql2 && gem install mysql2

Please note that similar problems can occur with different homebrew-managed libraries (see my own answer to this, for example)


After a lot of googling and trying all above...the only thing that solved my problem was this command:

$install_name_tool -id /usr/local/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

I am using a macbook pro, OSX 10 El Capitan. Darwin xxxx-MacBook-Pro.local 15.6.0 Darwin Kernel Version 15.6.0: Thu Jun 23 18:25:34 PDT 2016; XXX:xnu-3248.60.10~1/RELEASE_X86_64 x86_64 Perl:v5.18.2 Mysql:5.6.19


Just for the records: $ gem pristine mysql2 solves this for me.


There are already a lot of answer to this question, especially this one https://stackoverflow.com/a/10847618/5515861. I only want to add couple of notes. If you guys using Mac, I don't know how you install the MySQL, but the first thing to investigate is where is your MySQL installation is located. For me, MySQL is installed using brew for version 5.7, and the location is /usr/local/opt/[email protected]/, so add the following to my ~/.zshrc.

MYSQL=/usr/local/opt/[email protected]/bin/
MYSQL_LIB=/usr/local/opt/[email protected]/lib/
export PATH=$PATH:$MYSQL
export DYLD_LIBRARY_PATH=$MYSQL_LIB:$DYLD_LIBRARY_PATH

Hope you fix your issues


I got this issue "Library not loaded: libmysqlclient.18.dylib" when importing MySQLdb from MySQL For python3:

    Traceback (most recent call last):
  File "test.py", line 3, in <module>
    import MySQLdb
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQL_python-1.2.4-py3.5-macosx-10.11-x86_64.egg/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQL_python-1.2.4-py3.5-macosx-10.11-x86_64.egg/_mysql.cpython-35m-darwin.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQL_python-1.2.4-py3.5-macosx-10.11-x86_64.egg/_mysql.cpython-35m-darwin.so
  Reason: image not found

Solution works for me: Mac OS X 10.11.1 Python3.5

Edit ~/.bash_profile:
export PATH="/opt/local/Library/Frameworks/Python.framework/Versions/3.5/bin:$PATH"
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
export PATH="/usr/local/mysql/bin:$PATH"
export PATH="/usr/local/mysql/lib:$PATH"
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

To be sure what symlink is needed (depend on mysql version and os version) :

$ locate libmysqlclient.18.dylib
/usr/local/mysql-5.6.24-osx10.8-x86_64/lib/libmysqlclient.18.dylib

and so :

ln -s /usr/local/mysql-5.6.24-osx10.8-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

If you're using Bitnami RubyStack and ran across the similar problem. Try this one

sudo ln -s /Applications/rubystack-2.0.0-17/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

On Mac Sierra if using Homebrew then do:

sudo ln -s /usr/local/Cellar/[email protected]/5.6.34/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

This worked for me. All I had to do is uninstall mysql2 gem and install it again using the below commands

gem uninstall mysql2
gem install mysql2 -v '0.3.18' -- --with-mysql-config=/usr/local/Cellar/[email protected]/5.7.28/bin/mysql_config

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Worked for me. All the similar ones didn't.


This works for me:

ln -s /usr/local/Cellar/mysql/5.6.22/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

use this from your command line:

sudo install_name_tool -id /usr/local/mysql-connector-c-6.1.3-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/local/mysql-connector-c-6.1.3-osx10.7-x86_64/lib/libmysqlclient.18.dylib

tried on fews computers with maverick always works


For those who are using brew. Just link you mysql version with "--force" option.

brew link mysql56 --force

I am using Mac OS, and I was stuck with this bug even after uninstalling / removing all mysql and MAMP. Earlier, I installed brew install mysql and also used MAMP. addling softlink didn't work for me.

It was only resolved by removing all existing mysql. and then install mysql through MySQL from here.


The only thing that worked for me is:

sudo install_name_tool -change libmysqlclient.18.dylib \
/usr/local/mysql-5.6.23-osx10.8-x86_64/lib/libmysqlclient.18.dylib \
/Library/Ruby/Gems/2.0.0/gems/mysql2-0.4.3/lib/mysql2/mysql2.bundle

Replace the paths of mysql and gems to fit your system.


I confirm patch from Abhishek does work.

it work for Yosemite, too.

note: instead of linking to a particular version of mysql, use the fact mysql already built symlink:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

this solution does work for Xcode and C API.


Thanks. A Homebrew upgrade made my Rails apps have issues on my Mac. I reinstalled MySQL (5.7) from source, then I had to do this

sudo ln -s /usr/local/mysql-5.7.28-macos10.14-x86_64/lib/libmysqlclient.20.dylib /usr/lib/libmysqlclient.20bdylib

based on what I read above, and in my Gemfile

gem 'mysql2', '0.5.3'

and in database.yml

adapter: mysql2

sudo ln -s /usr/local/mysql-5.5.25-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

That worked for me. I installed MySQL from a dmg file.


In El Capitan I got ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted

In El Capitan /usr/lib/ now has a restricted flag and can't be written to without disabling security so I just put the link in /usr/local/lib instead.

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

Rails server is running fine again.


Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to ruby-on-rails

Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Where do I put a single filter that filters methods in two controllers in Rails Empty brackets '[]' appearing when using .where How to integrate Dart into a Rails app Rails 2.3.4 Persisting Model on Validation Failure How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? Rails: Can't verify CSRF token authenticity when making a POST request Uncaught ReferenceError: React is not defined

Examples related to ruby

Uninitialized Constant MessagesController Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Ruby - ignore "exit" in code Empty brackets '[]' appearing when using .where find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException) How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite? How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? How to update Ruby with Homebrew?

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"