Heroku, Django and Ubuntu Lucid

The short version

The following commands on a previously rubyless Ubuntu Lucid machine will get a Django project called awesome with an app called nonsense running on Heroku. Explanations are at the end.


sudo aptitude install rubygems1.9.1 libopenssl-ruby1.9.1
gem install addressable
gem install heroku
ln -s ~/.gem/ruby/1.9.1/bin/heroku ~/bin/heroku
ssh-keygen -t rsa
heroku keys:add
mkdir -p ~/projects/heroku-django
cd ~/projects/heroku-django
virtualenv --no-site-packages .
source bin/activate
pip install psycopg2
pip install gunicorn
pip install Django
pip freeze > requirements.txt
django-admin.py startproject awesome
cd awesome
python manage.py startapp nonsense
cat >Procfile <<EOF
web: bin/gunicorn_django --workers=4 --bind=0.0.0.0:$PORT awesome/settings.py
EOF
cat >.gitignore <<EOF
bin/
include/
lib/
*.pyc
EOF
git init .
git add .
git commit -m "Initial commit"
cd ~/projects/heroku-django
heroku create --stack cedar
git push heroku master
heroku run bin/python awesome/manage.py syncdb --app app-name-XXXX
firefox http://app-name-XXXX.herokuapp.com/ &

The details

dtt101 has been nagging me about the benefits of Heroku for ages now. It’s fair to say that the fact that the two main frameworks it advocates are Rails and Node rather put me off (I am, after all, Python through and through). I’d seen the odd blog post recently that suggested that you could run Django (and all sorts of other things) on their new Cedar platform, but hadn’t taken it particularly seriously. This morning, however, I came across the excellent Deploying Django on Heroku (Mac OS X) so I thought I’d finally have a look at trying it out for one of my own projects.

The server that hosts House Price Please is due to be decommissioned so it needed a new home. It’s also fair to say that it’s never had that much traffic, so it makes an ideal candidate for trying heroku out.

All these steps were on Ubuntu Lucid (Isotoma‘s standard desktop OS). They also assume that you’ve already got a Heroku account.

The heroku CLI is provided as a Ruby Gem, so the first step was to get rubygems installed. Lucid provides 1.8, 1.9 and 1.9.1 from apt, so I plumped for 1.9.1. In theory I could then use the gem install command to install the heroku gem.


sudo aptitude install rubygems1.9.1
gem install heroku

However, this blew up saying ‘launchy relies on addressable’, so I needed to try again, installing addressable first:


gem install addressable
gem install heroku

There are also some warnings about /var/lib/gems/1.9.1 not being writeable, but I’m not aiming for a system wide install of the heroku CLI, so that’s fine. However, to get the heroku command on my path I had to do the following:


ln -s ~/.gem/ruby/1.9.1/bin/heroku ~/bin/heroku

Next step is to add ssh keys to heroku. If you’ve not already got a key pair you’ll need to generate one using ssh-keygen -t rsa. Then add them to heroku using:


heroku keys:add

On a previously rubyless machine this blows up for with a message saying no such file to load -- net/https. To fix this I had to:


sudo aptitude install libopenssl-ruby1.9.1

When you run heroku keys:add again it will upload the public key from your .ssh directory. If you’ve got more than one it will offer you a choice.

From this point on you can pretty much follow the instructions at the post that inspired this one. The only changes I’d make are:

  • You don’t need the second (celeryd) line in the Procfile (unless of course you plan on using celery for async tasks)
  • You need to push to heroku before you syncdb, not after

Once I’d converted housepriceplease to this structure (we use buildout on our servers) I dumped the data on the old live server using python manage.py dumpdata housepriceplease > data.json, checked data.json into the fixtures directory of my main app and ran:


heroku run bin/python housepriceplease/manage.py loaddata data.json --app high-wind-5409

Finally I following the instructions for custom domains on the excellent Heroku dev center to point housepriceplease.co.uk at Heroku, and voila! we’re live:

housepriceplease.co.uk

Minor edit: On the Heroku/Facebook page (which is well scary, actually) there is a link to some instructions for installing the Heroku toolbelt for Linux. Looks very new, but likely that the faffing with Ruby at the start of this article may not be needed soon.