UPDATE 11/3/12: If you’re now on Mountain Lion, life has gotten much easier. Read my new post on it
UPDATE 8/4/11: The instructions here are a little dated and difficult, probably from my own ignorance. With Lion out, too, things have changed enough that you should probably use the instructions here instead.
I recently needed to port a Django app off of our Linux server onto my Macbook Pro, which was slightly more involved than I thought it would be. I don’t remember having nearly so much difficulty with it when I initially set it up on my old Leopard partition, but times change. In any case, I figured it would be a common enough task that I would offer instructions on how to make it work. If you’re looking to just install Django fresh, you’ll notice there are a few steps specific to importing parts from another Django app, but it’ll mostly apply. So let’s go.
Here’s exactly what we’ll be putting together:
- MySQLdb – a python package to communicate with MySQL
- libjpeg – necessary for PIL
- Python Imaging Library – a pretty useful library for drawing and more
If you’re more slick from Terminal than I am, there are a lot of steps here that you can do out of that. If that’s you, then you can translate those operations yourself. If you’re like me and still like GUIs for some tasks, maybe this will work better for you. I’ll also leave a trail of links to the references I used to figure it out myself so you can read the real reference
1) Installing and setting up MySQL
Download MySQL. Particularly, I ended up getting the 32-bit version for reasons that are not clear to me, though the 64-bit version might have been the correct call. I at least know it works with the 32-bit version. There’s a .dmg version that apparently fast-tracks the installation, but I ended up not using that either because it didn’t download properly. So here’s what you do to do it manually from the tarball.
and unzip it by just double-clicking on it in Finder. From Terminal, run the following
sudo mv (copy path) ./
sudo ln -s full-path-to-mysql-VERSION-OS mysql
Here’s a nice trick I learned recently. You’ll notice the code above has (copy path) in it for the path to the mysql folder you just unzipped. Instead of typing it in, you can click and drag the folder from Finder to Terminal, and it’ll fill in the whole path for you.
So if you want to do things properly, you’ll create a separate mysql user, apparently, and let everything run through that. In my case, I’m not planning on ever using MySQL locally for actual production, so I lazily just have everything running from my user account. To complete setup
mysqladmin -u root password NEWPASSWORD
Where you can fill in the root password. Next, for convenience, let’s add the mysql commands to our path so we don’t have to type them every time. Open up your bash profile and add this line (if it doesn’t already exist, just create it at ~/.bash_profile)
Of course, if you already have a path there, you only need to append the bit about mysql from it. For convenience in the rest of this setup, copy the same line into normal Terminal and run it to update your PATH variable in session.
Right now, the mysql database is running because of “bin/mysqld_safe &” above. In general, from now on, the command to start it is
and the command to shut it down is
mysqladmin -u root -p shutdown
where you’ll need to type in the root password you defined above. Go ahead and start the MySQL database again. The next little bit is for porting the contents of a web server database to your local machine.
mysql -u root -p
create database DBNAME;
mysqldump -u root -p DBNAME > ~/DBNAME.out
scp firstname.lastname@example.org:~/DBNAME.out LOCALPATH
mysql -u root -p DBNAME < LOCALPATH/db-name.out
2) Installing Django
This part is thankfully very easy. Download Django from here and unzip it. I myself ended up using the 1.0 to avoid compatibility issues with our web server, but I’m guessing the latest version is the greatest. cd to the folder you just extracted and execute
sudo python setup.py install
Boom, done. Thanks Django!
Okay, so this is going to let us tie the 2 pieces we’ve put down together. Download MySQLdb from here, unzip, and navigate to that folder. In Terminal, execute the following:
ARCHFLAGS='-arch x86_64' python setup.py build
ARCHFLAGS='-arch x86_64' python setup.py install
defaults write com.apple.versioner.python Prefer-32-Bit -bool yes
To check to see if it worked, execute this:
So if you didn’t get icky output, then that worked. If it didn’t, I’m not the person to ask.
If that worked for you and you’re not using PIL, then you’re done. Head on over to the Django tutorial if you want to try it. If you’re doing drawing stuff, then you’re not out of the woods yet.
4) Installing libjpeg and Python Imaging Library
First, you’re going to need to have Xcode installed so you can compile lipjpeg. If that’s not already installed, you can find it on your Snow Leopard installation dvd under “Optional Installs” or from the Apple website.
We’ll start with libjpeg. Download it from here. You’ll probably want one that is of the form jpegsrc.v7.tar.gz, where the version might be different. Again, extract, navigate in, and execute this
cp /usr/share/libtool/config/config.sub .
cp /usr/share/libtool/config/config.guess .
./configure --enable-shared --enable-static
sudo mkdir -p /usr/local/include
sudo mkdir -p /usr/local/lib
sudo mkdir -p /usr/local/man/man1
sudo make install
sudo ranlib /usr/local/lib/libjpeg.a
Don’t ask me what a lot of that means. I just copy what people tell me to do.
Finally, we can install the PIL. Download it from here, extract, and navigate it. The last Terminal commands are
python setup.py clean
python setup.py build
sudo python setup.py install
And that should be it.
So there you go. To get your apache server running to make your Django app go, go to System Preferences->Sharing->check “Web Sharing”. Your Django app can live where it is, but if you need to drop media files somewhere, the www root for Mac OS is /Library/WebServer/Documents/