albertyw/albertyw.com

View on GitHub
app/notes/20200526-0255.md

Summary

Maintainability
Test Coverage
Upgrading LibMySQLClient in Python MySQLDB/MySQLClient

upgrading-libmysqlclient

1590461728

After upgrading from Ubuntu 18.04 to Ubuntu 20.04, I ran into this error when
trying to import python's `mysqlclient`:

```text
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](https://github.com/PyMySQL/mysqlclient-python) from
    github (`git clone git@github.com: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:

```bash
#!/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 git@github.com: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
```