Categories
software engineering

Installing Django on Mountain Lion (+ MySQL and PIL)

UPDATE 1/14/13: A few small corrections. See Jonathan’s comment below if you’re having issues installing mysqldb

A few years ago, I posted instructions on installing Django on Snow Leopard. Since then, things happen gotten a lot easier. At that time, there was a weird shift going on with 64-bit computing that made it difficult to know what versions of various software to install. Well, it’s a few years past that, and package installers have become much easier to use as well. So, if you’re looking to do a fresh Django install on your computer, take a look:

1. Install MySQL

Download MySQL from http://dev.mysql.com/downloads/mysql/. Specifically, you want the “Mac OS X ver. 10.6 (x86, 64-bit), DMG Archive” version. Install mysql*.pkg as given. If you would like, you can also install the startup item (if you want MySQL to start automatically) and the preference pane so that MySQL can be started from System Preferences.

For some convenience, you can also add mysql to your path to invoke it in Terminal directly. To do this, open ~/.bash_profile with your favorite editor, and add

export PATH="/usr/local/mysql/bin:$PATH"

to it. You should also set your root password for MySQL using

/usr/local/mysql/bin/mysqladmin -u root password YOUR_PASSWORD

References:

2. Install libjpeg

Feel free to skip this if you don’t need the Python Imaging Library, but if you do, homebrew is the easiest way to get it and many other packages that you deserve as a UNIX user. Long ago, I used fink, and it was an awful experience. I hope they’ve improved it since, but homebrew was so painless.

ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
brew install libjpeg

References:

3. Install virtualenv (recommended)

I hadn’t used virtualenv before doing this setup, but it’s great. In short, it creates a virtual environment to setup python packages. Even though it’s easy to install packages otherwise, it does become a mess if different applications have different dependencies or if you need to swap python versions. This does away with all of that.

sudo easy_install virtualenv
sudo easy_install virtualenvwrapper

Then start from line 2 of https://gist.github.com/1852087 to get your virtualenv setup.

References

4. Install the python packages

If you didn’t install virtualenv, you’ll need to install pip (sudo easy_install pip) since pip otherwise is packaged with virtualenv. You will also need to sudo the next steps.

I recommend that you use a requirements file to setup your packages. It probably isn’t a big deal if you’re working on your own project, but it’s immensely handy as soon as someone else needs to setup the same environment as you. To do that, create a requirements.txt with contents similar to

django>=1.4
MySQL-python
pillow>=1.7.8
South>=0.7.6

pillow is optional depending on if you want PIL, and so is South. If you have your own favorite way of doing schema migrations, feel free to substitute. If not (or if you don’t know what South is for*), I recommend South. After you have that setup, just run

pip install -r /PATH/TO/requirements.txt
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

I don’t exactly know what the circumstances are for requiring the second line, but I needed it, so I’m going to assume you will too.

References:

And that’s it! Now, you can fire off your Django project from within your virtual environment for local development. If you mine through some of the other links above, you will find a lot of other really good instructions for setting things up. Presumably, you can also google things as necessary.

Let me know in the comments if you run into any problems, if I missed anything, or if you have any proposed enhancements to this process. Also, let me know if I can do any other advocacy on the part of Django. I have had largely good experiences with it for building websites.

 

* one thing that Django doesn’t address well is database migrations. When you setup your models initially, Django will create db tables for the fields you specify. As fields change, however, Django doesn’t deal with changing the db tables. A tool like South will analyze your models and create migrations for you to run and modify the db schema

3 replies on “Installing Django on Mountain Lion (+ MySQL and PIL)”

This guide was very helpful, thanks! Two things that differed for me on Mountain Lion that I thought I’d point out:

– when installing MySQL-python I ran into an error that mysql_config could not be found. You can either modify the site.cfg in the mysql-python package to match the path for mysql_config on your system, or you can use ln -s to create a symbolic link (I did the latter)

see: http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found

– the line you have posted: sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylibsource

did not work for me. According to the reference link you posted (http://stackoverflow.com/questions/6383310/python-mysqldb-library-not-loaded-libmysqlclient-18-dylib) the line should instead be: sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Glad this was helpful to you, and thanks for the comments! I’m not sure what was up with mysql_config, but I’m glad you got that straightened out.

The second one was definitely a typo on my part. Thanks for catching that mistake and checking the reference.

Good luck with your Django pursuits!

Thank you. This tutorial worked great for me:

1) Install MySQL
2) Install pip
3) Install django
4) Install MySQL-python

Done!

Leave a Reply

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