Ruby on Rails – Library not loaded: libmysqlclient.18.dylib (LoadError)


This is a typical mysql error on Mac when you try to start Rails server, libmysqlclient.18.dylib can be something else like that libmysqlclient.16.dylib, .. etc

The root reason is : libmysqlclient.18.dylib is not under /usr/lib – this is place where Ruby (or Rails ?) will look for this dynamic library

Solution : well, we just need to create a symbolic link of libmysqlclient.18.dylib under /usr/lib . you can find for libmysqlclient.* (this mean libmysqlclient and whatever characters behind it) in your mysql installation folder. to look for it, execute this command

which mysql
#we have /usr/local/mysql/bin/mysql - it means installation folder is /usr/local/mysql


cd /usr/local/mysql
sudo find . -name 'libmysqlclient.*'
# in this case, i have ./lib/libmysqlclient.18.dylib returned, it my mysql lib path is /usr/local/mysql/lib/libmysqlclient.18.dylib

Finally, we create the symbolic link, this is the command that I used to make it work for me. This way you don’t need to use install_name_tool every time you update your mysql

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