Install MySQLclient using pip

Posted under » MySQL » Python » Django on 06 April 2021

Unlike LAMP which automatically setup MySQL connection to PHP, Python connection to MySQL can be tricky.

To install, follow this instruction or continue reading.

$ apt-get install python3-dev default-libmysqlclient-dev build-essential pkg-config 

Note that if you have a specifc version of Python, eg. 3.8 you have to specify it.

$ sudo apt-get install python3.8-dev default-libmysqlclient-dev build-essential pkg-config 

Once installed, then run Pip in your environment. Pip is a package-management system written in Python used to install and manage software packages. It connects to an online repository of public and paid-for private packages, called the Python Package Index. Most distributions of Python come with pip pre-installed. Check by

(bri) ubuntu@hornet:~$ pip --version 

But if not, then

$ sudo apt install python3-pip
$ apt install python3.8-pip # or
$ pip install --upgrade pip # for good measure

Don't install mysqlclient first until you install wheel which is like a python zip packager or else you will get error when in venv.

(bri) ubuntu@hornet:~$ pip install -U wheel

If you want to use a specific Python version

(bri) ubuntu@hornet:~$ pip3.8 install -U wheel

After installing PIP do not use sudo pip or it will break apps.

Afterwhich you can proceed installing mysqlclient

(bri) ubuntu@hornet:~$ pip install mysqlclient

If somehow you have a `failed building wheel' error, then you have to specify the version that works for your OS, eg. Ubuntu 24.04 like so

(bri) ubuntu@hornet:~$ pip install mysqlclient==2.2.5

If you want to update.

(bri) ubuntu@hornet:~$ pip install mysqlclient --upgrade
Normally it will uninstall the old one and then install the updated version

If you want to uninstall.

(bri) ubuntu@hornet:~$ pip uninstall mysqlclient

If you want to see the list of installed libraries

(bri) ubuntu@hornet:~$ pip list

Find using Python Package Index.

However, when I have 2 different venvs somehow it will only accept the latest version. ie. 3.12 because the installation is default while 3.8 doesn't seem to work. So it is not that straightforward.

See also Connect MySQL with pymysql.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data