pip install mysql-python fails with EnvironmentError mysql_config not found
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Understanding the Error
When you attempt to run pip install mysql-python, especially on UNIX-like operating systems such as Linux or macOS, you might encounter the following error:
This error generally indicates that the mysql-config script, which is part of a MySQL development package, is not installed on your system. The script provides necessary compile and linking options for building MySQL-related software. The mysql-python package, also known as MySQLdb, relies on this script for installation.
Why Does This Happen?
The error arises mainly because:
- MySQL Development Libraries Missing: The
mysql-pythonpackage requires MySQL client library headers and the requisite binary,mysql_config, during installation. - Incompatible MySQL Versions: If you have a MySQL server installed, it doesn't inherently include the client development files required by
mysql-python. - macOS and Homebrew: On macOS systems, if you have installed MySQL using package managers like Homebrew, the development files might not be symlinked to a location in your system's PATH by default.
Prerequisites to Resolve the Issue
Before resolving the error, ensure you have the following prerequisites:
- Python and Pip Installed: Python should be properly installed, and
pipis the tool needed to install Python packages. - Administrator/Sudo Access: Certain operations such as installing packages may require elevated permissions.
Steps to Resolve the Error
The solution involves installing missing packages depending on your operating system.
Linux Systems (e.g., Ubuntu)
On Ubuntu or Debian-based systems, you can resolve this by installing mysql-client and its development headers with:
Red Hat/CentOS Systems
For Red Hat-based distributions, use:
macOS
For macOS users, MySQL development files can be installed using Homebrew:
- Install MySQL using Homebrew if it's not already installed:
- Ensure the Homebrew path is correct, and
mysql_configis available:
Verify mysql_config
After installation, verify that mysql_config is accessible:
This command should return the path to mysql_config, indicating it's available in your system PATH.
Reinstall mysql-python
Once you have the development environment set up, reinstall mysql-python:
Alternative Solutions
Consider MySQL Connector/Python
As an alternative to mysql-python, consider using the official MySQL Connector for Python. It is pure Python, thus requiring no additional dependencies. Install it via:
Use PyMySQL
PyMySQL is another pure Python MySQL client that can serve as a replacement for mysql-python:
Comparing Solutions
| Package | Advantages | Limitations |
mysql-python | C API for MySQL (better performance in some scenarios) Widely used in legacy systems | Requires mysql_config |
mysql-connector-python | Official MySQL package Pure Python (no external dependencies) | Slower compared to mysql-python in some cases |
pymysql | Pure Python Easier installation and maintenance | Performance can be slower; not suitable for all legacy systems |
Conclusion
Encountering the mysql_config not found error is often a roadblock when installing mysql-python, but with the right setup, it can be resolved with relative ease. It is worthwhile to explore alternatives such as MySQL Connector/Python or PyMySQL, especially when setting up new projects, due to their ease of installation and modern features.
Ultimately, understanding why this error occurs and having the ability to rectify it empowers developers to work more efficiently with MySQL databases within their Python applications.
Related reading
- Pivot or equivalent in clickhouse
- PlayFramework with Morphia?
- Please explain about insertablefalse and updatablefalse in reference to the JPA Column annotation
- Please use 'MongoMappingContextsetAutoIndexCreationboolean' or override 'MongoConfigurationSupportautoIndexCreation' to be explicit
- pip install tensorflow cannot find file called client_load_reporting_filter.h
- pip install unroll python setup.py egg_info failed with error code 1
- Populate a database with TestContainers in a SpringBoot integration test
- Populate data table from data reader

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.