Skip to content

mySQL Native Bindings in OS X El Capitan

Posted on:December 1, 2015

Booting up a Django project, I ran into the following problem:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/siawyoung/.pyenv/versions/test-venv/lib/python2.7/site-p
ackages/_mysql.so, 2): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
Referenced from: /Users/siawyoung/.pyenv/versions/test-venv/lib/python2.7/site-packages/_mysql.so

Googling around for solutions was really confusing because there seemed to be a mix of old and recent problems and solutions, like this StackOverflow question which was asked 4 years ago regarding OS X 10.6, but with answers from this year addressing El Capitan (yes, I know, wtf). Another one exactly like it.

I installed mySQL with brew, but wherever it is, you can find the file:

$ find /usr -name "libmysqlclient.18.dylib"
/usr/local/Cellar/mysql/5.6.25/lib/libmysqlclient.18.dylib
/usr/local/Cellar/mysql/5.6.27/lib/libmysqlclient.18.dylib

It seemed the cleanest way was to use install_name_tool, so I tried it:

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/Cellar/mysql/5.6.27/lib/libmysqlclient.18.dylib /Users/siawyoung/.pyenv/versions/test-venv/lib/python2.7/site-packages/_mysql.so

But that didn’t work.

After a quick google and wading through tons of non-starters, I finally found a fix: all you have to do (inside the virtualenv or pyenv) is set the environment variable DYLD_LIBRARY_PATH like so:

$ export DYLD_LIBRARY_PATH=/usr/local/Cellar/mysql/5.6.27/lib

It’s not the most elegant solution, but it’s the simplest and least intrusive one.