MySQLclient Installation Errors in Python while running pip install mysqlclient

The Problem: mysql_config not found

When you try to install mysqlclient using pip, the installation process might fail with an error that looks something like this:

Collecting mysqlclient==2.0.3 (from -r requirements.txt (line 44))
  Using cached mysqlclient-2.0.3.tar.gz (88 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [31 lines of output]
      /bin/sh: 1: mysql_config: not found
      ...
      OSError: mysql_config not found
      [end of output]

This error typically occurs because mysqlclient requires certain MySQL development libraries to be present on your system. These libraries provide the necessary tools for compiling and linking the mysqlclient package, particularly the mysql_config command, which is used to retrieve information about the MySQL installation.

The Solution: Installing MySQL Development Libraries

To resolve this issue, you need to install the MySQL development libraries on your system. The process is straightforward but varies depending on your operating system.

For Ubuntu/Debian

On Ubuntu or Debian-based systems, you can easily install the required libraries using apt. The package you’re looking for is libmysqlclient-dev. Here’s the command you need to run:

sudo apt install libmysqlclient-dev

For Fedora sudo dnf install mysql-devel

For CentOS/RHEL sudo yum install mysql-devel

For macOS brew install mysql-client

After installing, you’ll also need to make sure that the mysql-client binaries are in your PATH:

echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

For MariaDB Users

If you’re using MariaDB instead of MySQL, the package you need is slightly different. On Ubuntu, for instance, you would install libmariadb-dev instead of libmysqlclient-dev:

sudo apt install libmariadb-dev

Run pip install -r requirements.txt (or just pip install mysqlclient if you were running solo on that) and you should be good to go. Now you can get back to building your application with one less thing to worry about… happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *