Installing mysqlclient on macOS can be a bit tricky for beginners, but with the right instructions, it’s a straightforward process. This Python package is a thin wrapper around the MySQL C client library and is often used for interacting with MySQL databases in Django or other frameworks. If you’re developing Python applications on macOS and need to connect to a MySQL database, mysqlclient is a fast and reliable option.
Before diving into the steps, it’s important to ensure your system has the necessary dependencies. Here’s a step-by-step guide to get mysqlclient up and running on your Mac.
Step 1: Install Homebrew
Homebrew is a package manager for macOS that simplifies installing software. If you haven’t already installed Homebrew, open Terminal and paste the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
After the installation is complete, you may need to add Homebrew to your PATH. Follow the instructions presented by the installer.
Step 2: Install MySQL
Once Homebrew is installed, use it to install MySQL:
brew install mysql
This command installs the MySQL server, client, and development libraries—essential for building mysqlclient.

Step 3: Install MySQL Client Development Headers
In some cases, you may need explicit access to the MySQL client development files. These are normally included, but if you encounter issues, you can also install the mysql-client package separately:
brew install mysql-client
After installation, add the mysql-client path to your shell’s environment:
echo 'export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"' >> ~/.zprofile
source ~/.zprofile
This step ensures that the compiler can find the necessary files during the build process.
Step 4: Set Compiler Flags
Sometimes you’ll need to specify the path to MySQL headers when installing mysqlclient. This can be done by setting environment variables:
export LDFLAGS="-L/opt/homebrew/opt/mysql-client/lib"
export CPPFLAGS="-I/opt/homebrew/opt/mysql-client/include"
These flags tell your compiler where to look for the MySQL libraries and header files.
Step 5: Create and Use a Virtual Environment (Optional)
Though not mandatory, using a virtual environment is a good practice in Python development. It isolates dependencies and minimizes conflicts:
python3 -m venv venv
source venv/bin/activate
Step 6: Install mysqlclient via pip
With everything in place, you can now install mysqlclient:
pip install mysqlclient
If the compiler has all the necessary paths and libraries, the package should install smoothly. Upon success, you can verify your installation:
python -c "import MySQLdb; print('mysqlclient installed successfully')"
Common Errors and Troubleshooting
If you encounter errors, double-check the installation paths of MySQL or mysql-client. Also, make sure your Python and pip versions are up-to-date:
python3 --version
pip3 --version

FAQ
-
Q: Can I install mysqlclient using pip without Homebrew?
A: Technically yes, but it may result in missing dependencies on macOS. Using Homebrew ensures the required headers and libraries are properly installed. -
Q: What version of MySQL is compatible with mysqlclient?
A: Most modern MySQL versions (5.7, 8.0) are supported. Ensure your client libraries and server match in version wherever possible. -
Q: What if I’m using an M1 or M2 Mac?
A: For Apple Silicon Macs, ensure you’re using the right architecture version of Python and that Homebrew is installed under/opt/homebrew
. The rest of the steps are the same. -
Q: Is mysqlclient the only MySQL Python library?
A: No, alternatives includePyMySQL
andmysql-connector-python
, but mysqlclient offers better performance as it’s compiled. -
Q: How do I uninstall mysqlclient?
A: Runpip uninstall mysqlclient
from your environment. If you used a virtual environment, ensure it is activated before running the command.
Following these steps will help you install and configure mysqlclient successfully on macOS. Whether you’re developing a Django project or working on a custom database tool, this setup gives you a powerful and reliable way to interact with MySQL databases.