Installing MySQLdb module for Python 2.7 for Ubuntu 12.04

After spending a good 30 minutes trying to find a solution on why I couldn’t install the MySQLdb Python module, I finally found the answer. Click the Read More to see the 4 simple steps.

Previously, I already had MySQL installed on my machine. When I tried running:

sudo pip install MySQL-python

I was getting this error message:

Traceback (most recent call last):

File "<string>", line 14, in <module>

...

File "setup_posix.py", line 25, in mysql_config

   raise EnvironmentError("%s not found" % (mysql_config.path,))

EnvironmentError: mysql_config not found

 

I found the solution on this blog post here but I’ll list the steps here anyway.

  1. Be sure you have pip installed on your machine using this command:
    • sudo easy_install pip
  2. If you already have pip installed, it’d be a good idea to upgrade it now:
    • sudo pip install pip --upgrade
  3. Build the dependencies for python-mysqldb libraries:
    • sudo apt-get build-dep python-mysqldb
  4. Install the Python MySQL libraries:
    • sudo pip install MySQL-python

To make sure it is installed, run python:

$ python
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

>> import MySQLdb
>>

A successful install does not produce any error message after the import statement