Setting Up A Macintosh (Snow Leopard) For Development


In trying to set up my Mac for Python / Django development, I ran into several annoying issues. I'm not sure if these are particular to my machine (it was set up by the university before I could get my hands on it) or if they're generic problems that everyone may face. Here are the steps (as I recall them) I needed to take to get my Mac working.

Note that I am not a server guy (yet), so this was essentially my first real attempt at setting things up. I'm sure there's plenty of magical thinking and unnecessary gibberish. Please feel free to correct me so this is the best it can be.

--

First off, you should make sure you have both XCode 4 and X11 installed on your machine.

XCode 4 can be found in the App Store here: http://itunes.apple.com/us/app/xcode/id422352214?mt=12 . From what I understand some of the tools used to compile your installations are part of XCode. It's a $5 purchase.

X11 is used in the creation of the GDAL libraries. It can be found on your Mac system installation disk in the "optional installs" folder. Run the package in that directory, and when the installer allows you to choose optional packages you'll want to expand "Applications" and check "X11" to install it.

--

.bash_profile, .bash_rc

At this point you may want to set up your bash profile files.

Here's how I did it:

Open your Terminal application (it's in Applications/Utilities).

At the prompt type
$ vi .bash_profile
(some instructions on how to use Vi if you're not familiar: http://www.infobound.com/vi.html -- all you really need to know is to hit the "i" key and start putting in the code you want. When done hit the escape key, and type ":wq" to save and quit. or ":q" to quit if you didn't change anything. or ":q!" to quit and not save your changes).

I found a sample .bash_profile after some searching, here's what mine looks like (again, not sure if this is sane or necessary, but here you go):

#include .bashrc if it exists
if [ -f ~/.bashrc ] ; then
     source ~/.bashrc
fi
 
#set PATH so it includes private bin if it exists
if [ -d ~/bin ] ; then
    PATH=~/bin:"${PATH}"
fi
 
#and the same with MANPATH
if [ -d ~/man ] ; then
     MANPATH=~/man:"${MANPATH}"
     export MANPATH
fi
 
 
 
#This fixes some pip / easy_install errors as things can't compile
export ARCHFLAGS="-arch i386 -arch x86_64"

The last couple of lines I found I had to add after trying numerous times to get psycopg2 (required for Django) to work properly. An explanation can be found here: http://www.chrisbaltzer.com/post/6754911523/building-python-libs-with-xcode-4. I followed his instructions to also add a line to the /etc/sudoers file as well.

I also edited the .bashrc file to play around with the colors of my terminal and change the way the prompt looks. Much of it isn't necessary, but if you do decide to change the colors also be sure to change the terminal preferences to have a black background with white text and something bright for your bold text.

$ vi .bashrc

and this is what I had at this point

#paths
PATH="/bin:/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
export PATH
 
 
export PYTHONPATH="/usr/local/lib/python"
 
# History Handling
#
# Erase Duplicates
export HISTCONTROL=erasedups
# Resize History Size
export HISTSIZE=5000
# Append to bash_history if quits
shopt -s histappend
 
 
 
#How Prompt Looks
#export PS1="\u@\h\w$ "
export PS1="\n<\[\033[0;34m\]\h\[\033[0m\]:\[\033[0;36m\]\u\[\033[0m\]> \j [\$(date +%m-%d\" \"%H:%M)] \[\033[0;31m\]\w\[\033[0m\] \n! "
 
# Cli Colors
export CLICOLOR=1
# Colors
export LSCOLORS=BxFxCxDxGxegedabagHxEx
 
# Aliases
#
alias ls='ls -la'
 
 

The important lines are the paths at top. They should make sure that when you run commands in the terminal it looks in the right folders to find them.

--

Python 2.7

I decided to update the Python version to 2.7. I'm not sure if this was wise or necessary, but I did it anyway. If you choose this, be sure to update your version of easy_install as well -- otherwise things will go weird in future steps.

Python 2.7: http://www.python.org/getit/ -- at the time I'm writing the latest version is 2.7.2, and you'll want the link that says Python 2.7.2 Mac OS X 64-bit/32-bit x86-64/i386 Installer
Easy Install Update: http://pypi.python.org/pypi/setuptools -- again, for the version of Python above you'll want this installer: setuptools-0.6c11-py2.7.egg
If you forget to update your setup tools, easy_install will install to the 2.6 version of Python that comes with Snow Leopard and you'll be in trouble. I'm not sure if Lion will have 2.7.

Python 2.7 should update your .bash_profile with these lines:

# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

My final .bash_profile looks like this: http://pastebin.com/GdSv7uHG

--

Development Stack


At this point, I think I was set up to properly follow the Chicago Tribune's tutorial (one of many excellent pieces they put together) on setting things up for Django development: http://blog.apps.chicagotribune.com/2010/02/17/quick-install-pythonpostgis-geo-stack-on-snow-leopard/

Here are the commands

$ easy_install virtualenv
$ easy_install virtualenvwrapper
$ easy_install ipython
 

This will install VirtualEnv, VirtualEnvWrapper, and an interactive python interpreter (the last of which I haven't really used yet -- you can tell I'm a novice). VirtualEnv is great for segregating your python libraries so you can run multiple versions of libraries against your various projects. Keeps things from conflicting.

I also installed pip, yolk, and fabric. These may be better when installed directly into a virtual environment, I'm fuzzy on that. Regardless:

$ sudo easy_install pip
$ sudo pip install fabric yolk
Fabric is used for deplyoment, yolk is used to see what libraries you have installed into your python environment.

Next, you install brew, postgis, postgres, gdal, and git. Brew helps tremendously in installing packages.

$ sudo mkdir /usr/local
$ sudo chown -R 'whoami' /usr/local
$ curl -L http://github.com/mxcl/homebrew/tarball/master | tar xz --strip 1 -C /usr/local
 
installs brew

$ brew install postgis
$ brew install gdal  --with-postgres
$ brew install git
 
installs the rest.

Note: Here is where I ran into endless pain with GDAL. It kept asking for X11, which wasn't installed on my machine by default. Hopefully the earlier step in this guide will have fixed that problem.
Note 2: These take a very long time to install, so have patience.
Note 3: If you get errors, the "brew doctor" command can give you some guidance on what's broken. Some things appear to always be broken for me, but things worked fine as far as I can tell. Here are my errors:

We couldn't detect gcc 4.0.x. Some formulae require this compiler.
 
Some "config" scripts were found in your path, but not in system or Homebrew folders.
 
`./configure` scripts often look for *-config scripts to determine if software packages
are installed, and what additional flags to use when compiling and linking.
 
Having additional scripts in your path can confuse software installed via Homebrew if
the config script overrides a system or Homebrew provided script of the same name.
 
/Library/Frameworks/Python.framework/Versions/2.7/bin
    python-config python2.7-config
 
I believe that the gcc issue is caused by XCode 4 not having the older compiler (hence me adding the ARCHFLAGS line in my .bash_profile). Not sure about the python thing, I'm sure I did something wrong there.


The first line that installs postgis also installs postgres. A nice helper message appears and then goes away telling you what to do to your machine if this is your first install of postgres. Here it is:

If builds of PostgreSQL 9 are failing and you have version 8.x installed,
you may need to remove the previous version first. See:
  https://github.com/mxcl/homebrew/issues/issue/2510
 
To build plpython against a specific Python, set PYTHON prior to brewing:
  PYTHON=/usr/local/bin/python  brew install postgresql
See:
  http://www.postgresql.org/docs/9.0/static/install-procedure.html
 
 
If this is your first install, create a database with:
  initdb /usr/local/var/postgres
 
If this is your first install, automatically load on login with:
  mkdir -p ~/Library/LaunchAgents
  cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/
  launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
 
If this is an upgrade and you already have the org.postgresql.postgres.plist loaded:
  launchctl unload -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
  cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/
  launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
 
Or start manually with:
  pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
 
And stop with:
  pg_ctl -D /usr/local/var/postgres stop -s -m fast
 
 
Some machines may require provisioning of shared memory:
  http://www.postgresql.org/docs/current/static/kernel-resources.html#SYSVIPC
 
If you want to install the postgres gem, including ARCHFLAGS is recommended:
    env ARCHFLAGS="-arch x86_64" gem install pg
 
To install gems without sudo, see the Homebrew wiki.
 
I followed the initdb step. I also enabled the auto-load step (the "If this is your first install..." bit).

I also found that in another Chicago Tribune tutorial (the "Hello Newsroom" application) a table is expected in your Postgres DB that isn't there by default, so I downloaded this code and ran it: https://gist.github.com/666218#file_mk_postgis_template.sh

You should also set up git at this point. I created an account at github.com and followed their easy tutorial to get everything set up: http://help.github.com/mac-set-up-git/

I also had to add some more lines to my .bashrc to make the virtual environment stuff work properly:

#Virtualenvs
export WORKON_HOME=$HOME/.virtualenvs
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true
source /usr/local/bin/virtualenvwrapper.sh
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
Which leads to this final .bashrc file: http://pastebin.com/f2cKXmJX

The Trib tutorial also mentions installing QGIS from a stand-alone installer, but for the life of me I can't get it to run. It crashes on load looking for an sqlite3.framework folder that doesn't exist. I couldn't figure out how to install that.
--

Trying out the Hello, Newsroom application


Here's the tutorial: http://blog.apps.chicagotribune.com/2010/02/17/hello-newsroom-a-simple-geodjango-application/

If everything worked correctly, you should be able to do the tutorial (at least the local testing part). Where I ran into countless issues was in installing the pip requirements.txt file. All of them boiled down to the ARCHFLAGS command, so hopefully everything will work fine.