Notes
Upgrading LibMySQLClient in Python MySQLDB/MySQLClient
After upgrading from Ubuntu 18.04 to Ubuntu 20.04, I ran into this error when
trying to import python’s mysqlclient
:
ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or directory
After spending a while debugging it, I found the python mysqlclient
package
is a fork of MySQLdb
which compiles a _mysql.*.so
file which in turn references
libmysqlclient.so.*
. However, libmysqlclient.so
gets updated every so
often (with the most recent update from 20
to 21
) which makes mysqlclient
lose track of the libmysqlclient.so
version when installed with pip. After
trying various ways of clearing build caches, I was able to find a workaround
by:
- Ensuring
libmysqlclient
is installed (sudo apt install libmysqlclient-dev
) - Cloning the mysqlclient from
github (
git clone [email protected]:PyMySQL/mysqlclient-python.git
) - Manually building mysqlclient (
make build
) - Copying the generated
_mysql.*.so
to my virtualenv
I’m still trying to find a better way of doing this so I can get a working
mysqlclient
after running pip install mysqlclient
.
Edit 2020-06-19:
Since I had to do this again recently, here’s a script to automate the above workaround:
#!/bin/bash
# After installing mysqlclient on ubuntu 20.04, run this script to manually
# downgrade libmysqlclient from v20 to v19
# This assumes that you have an active python virtualenvironment
set -exuo pipefail
IFS=$'\n\t'
# sudo apt install libmysqlclient
git clone [email protected]:PyMySQL/mysqlclient-python.git
cd mysqlclient-python
make build
cp MySQLdb/_mysql.*.so $VIRTUAL_ENV/lib/python3*/site-packages/MySQLdb/
cd ..
rm -rf mysqlclient-python