This post is mostly about how I install development environment for Python and Django on OS X and my experience. If you want just a quick installation guide, follow this link, there is a really great tutorial about OSX Developer System installation.
I wrote a simple how-to about installing python virtual environment on Ubuntu, almost a year ago. Later it was extended with few posts about popular pitfalls and general problems. Through this year, I was bootstrapping python virtual environment for Django development on different ubuntu/debian/gentoo boxes plenty of times and everything was always going fine. So this how-to is really helpful for me and (I hope) for you.
Since I became a mac user, the first urgent thing to do was installing and configuring development environment for Python and Django to get ready for a work. Prior to buying a mac, I was following all the related topics about Django development on OS X. I saw there some problems and tedious moments, but I was sure that won’t be hard and everything will work fine. Eventually, it was much easier than I excpected.
I am really new to the mac and os x world, so I can be deeply mistaken. I have followed the steps in this tutorial to setup development environment for Django and everything works like a charm, though. So practical part of this post seems to be working. I am always opened for your feedbacks and fixes.
OS X has UNIX shell in the core, almost the same as the one I used to on Linux (but with some not that often valuable differences, e.g. built-in commands are BSD-like), so there is no reason to have any problem to run python scripts and other console-related-stuff. The weak point is handling system-wide dependencies, since OS X has no native package manager or so. You can go dirty old school and compile missing dependencies manually. But it is XXI century outdoors, we have a lot of smart guys, who are ready to fill the gaps of the world’s most advanced operating system. So there are some crafty third-party sort-of-package-managers for OS X, like Macports, Fink, Homebrew and others. I chose Homebrew, as the most spoken-about in communities I follow, with positive feedbacks. I can admit it’s a mainstream. It is pretty easy and weird (as for a person who was living in a Linux world and used to tools like apt and emerge) to install. I was doing this install on both Snow Leopard and Lion distributions (with only one difference in installing graphicsmagick using Homebrew on Lion).
So the first thing we do is checking for requirements, we need to have Xcode 4 installed (it is free of charge for Lion, you can get it from the App Store) and X11 as Homebrew installation guide says (it is installed by default in Snow Leopard). Actually the main requirement of Xcode is that it provides gcc and many people complain about they have to install a huge package only to have gcc installed. There is a gcc implementation without installing Xcode osx-gcc-installer but I did not try it though (I was noticed about this one on twitter other day, so I suppose it is immature, but probably a really good way to go in the nearest future). If requirements are satisfied, we can go further with installation:
###1. Installing Homebrew. Looks weird as I have mentioned above. You can explore a script on the given url if you need explanations.
$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
This is retrieving a script source from gist and like “piping” it to the ruby interpereter. It will prompt you for your password (since it requires a write access to /usr/local/). When it’s done, you can run brew command to install packages.
###2. Configuring system paths. Homebrew installs everything to /usr/local, so we need it to be in the beginning of our $PATH same as python software installed using brew (to override system wide). So we add the next line to our ~/.bashrc (either ~/.bash_profile or ~/.zshrc):
###3. Installing and configuring django development environment. I can’t imagine my daily workflow with python and django without using python virtual environment and I guess so are you.
The first thing I do is updating Python to the latest 2.x version:
$ brew install python
Restart terminal afterwards, to make sure fresh python is used by default.
Then I install pip using easy_install, but firstly we check that the correct easy_installed will be used:
$ which easy_install /usr/local/share/python/easy_install # This is correct $ easy_install pip
UPDATE: As of a version 2.7.5, python package currently has pip pre-installed, so the above snippet is obscure.
When pip is installed, you can follow my simple tutorial and install python virtual environment in the same manner, with only few difference.
We use pip without sudo command:
$ pip install virtualenv virtualenvwrapper
We source from the other path file, so the lines we put to ~/.bashrc (either ~/.bash_profile or ~/.zshrc) are:
export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
If you need lazy loading of virtualenvwrapper (makes shell start faster, though tab completion for wrapper is only available after the first command is ran):
export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh source /usr/local/bin/virtualenvwrapper_lazy.sh
And then we restart terminal, so virtualenv can be initialized and we can use virtualenv and virtualenvwrapper as usual.
When I was moving to OSX, I heard a lot about problems installing PIL using pip on macs. I was wondered when I didn’t face any problem installing it using brew:
$ brew install libjpeg $ brew install lcms # If needed $ brew install libtiff # If needed $ pip install PIL
###4. Installing PostgreSQL. I use PostgreSQL, so the following is about how to install and setup PostgreSQL on OSX using brew for my needs. This is totally covered at the OSX Developer System installation, if you are MySQL user, you can find necessary documentation there as well.
$ PYTHON=/usr/local/bin/python brew install postgresql $ initdb /usr/local/var/postgres $ cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/ $ launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
This will install posgres, initialize default data, run postgres and make it always running when system starts. You can now check installation by running:
$ createdb mytestdb $ dropdb mytestdb
And it has to be flown smoothly.
Then you can install python postgresql adaptor within your virutalenvironment:
$ pip install psycopg2
If your setup requires some additional tools or libraries, you can search them using brew search