Explain Codes LogoExplain Codes Logo

Pip install mysql-python fails with EnvironmentError: mysql_config not found

python
pip-install
mysql-python
environment-error
Anton ShumikhinbyAnton Shumikhin·Mar 2, 2025
TLDR

Resolve EnvironmentError: mysql_config not found. First, install MySQL development headers. Then, attempt pip install mysql-python.

# Debian/Ubuntu sudo apt-get install libmysqlclient-dev # Red Hat sudo yum install mysql-devel # macOS brew install mysql # Finally, in all cases: pip install mysql-python

Behind the scenes of the error

Understanding the ruckus

The mysql_config is required to compile the native C extensions for MySQL-python. If mia, the necessary MySQL dependencies remain unreachable.

Where's Waldo (mysql_config)

Locate mysql_config using:

# If this doesn't output anything, congratulations! You've found a black hole, not mysql_config. which mysql_config

Linux-based chameleons:

  • Ubuntu/Debian: Libraries get hip with age. Use default-libmysqlclient-dev. Don't forget to sudo apt update, keeps 'em fresh.
  • CentOS: Embrace both python-devel and mysql-devel with a yum hug.

macOS adventurers:

Adapt to alterations:

# Permanently add to your path echo 'export PATH="$PATH:/usr/local/mysql/bin"' >> ~/.bash_profile source ~/.bash_profile # The path strikes back!

And/or take the universal plunge into /etc/paths.

Securing the lot for installation

Bagging the essentials for MySQL-python

  • Python headers: Get with python-dev or its cool cousin python3-dev.
  • MySQL client libraries: Gift-wrapped in libmysqlclient-dev.

Dealing in virtual environments

Keep the virtual veil activated before embracing mysql-python.

source your-venv/bin/activate pip install mysql-python # Ensuring your python isn't exposed...Environmentally safe!

Swapping to the MySQLdb support

Consider python-MySQLdb, pre-baked and ready, no mysql_config hunts.

Extra points if you can:

  • Battling with OSX Mountain Lion, add its specific paths
  • Exploring with pip3 for Python 3 installations

Smoothing out the chinks

Checking out the environment setup

mysql_config being available is a thumbs-up sign for Python MySQL adventures.

Updating the System's memory

Keep the system in the know with sudo apt update or yum update before installing the latest versions of dependencies.

Wrangling macOS paths

For macOS diaspora, paths might need reconfiguring after updates to OS or MySQL.

Befriending Docker Containers

Dockerfiles should specify MySQL dependencies installation to mind the gap:

# Docker's compiled wisdom RUN apt-get install -y libmysqlclient-dev