Notes

Upgrading LibMySQLClient in Python MySQLDB/MySQLClient

May 25, 2020

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:

  1. Ensuring libmysqlclient is installed (sudo apt install libmysqlclient-dev)
  2. Cloning the mysqlclient from github (git clone [email protected]:PyMySQL/mysqlclient-python.git)
  3. Manually building mysqlclient (make build)
  4. 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