Epicserve

Setup a Django Dev Environment in Ubuntu

July 7, 2010 | 8:52 a.m. PDT

Recently I've had to switch from developing Django applications on a MacBook Pro to programming on a windows computer. I decided rather than setting up my Django development environment in Windows that it would be easier to setup my development environment in an Ubuntu VirtualBox virtual machine.

While I was setting up my environment I decided to take notes so I would have them for the next time I needed to setup my environment from scratch. I also thought it would be good to share my notes so I could learn about improvements I could make from people’s comments, so I could help people out there who wanted to try out Django for the first time and to help people who simply want to improve their own setup by comparing notes.

Test Environment Notes

It should be noted that I’ve tested this setup guide using the following system setup.

  • VirtualBox 3.2.6 r63112
  • Windows 7 Pro (Host Computer)
  • Ubuntu 10 (Guest Computer)

Installation Steps

This first step is optional because you can use whatever text editor is your favorite but I installed the full version of VIM because it adds syntax highlighting and it’s my favorite command line editor to use.

$ sudo aptitude install vim

You can either download django-environment and place for the purpose of this guide in the ~/Code/django-libs directory or install Git and download it with Git. I recommend using Git which can be used to install additional software and is also a very good distributed version control system that I use for code version control for my own projects.

$ sudo aptitude install git-core

Install python package installations tools.

$ sudo aptitude install python-setuptools
$ sudo easy_install pip

Install the python virtual environment. Virtualenv makes it easier to manage what python packages you need for each new Django project you work on.

$ sudo pip install virtualenv

Install virtualenvwrapper which makes working with python virtual environments easier. Make a directory where your python packages for your different virtual environments will be stored, add environment variables and install virtualenvwrapper.

$ mkdir -p ~/.virtualenvs
$ echo 'export WORKON_HOME=~/.virtualenvs' >> ~/.bashrc
$ source ~/.bashrc
$ sudo pip install virtualenvwrapper
$ echo 'source /usr/local/bin/virtualenvwrapper.sh' >> ~/.bashrc

Install django-environment which makes setting up the python django development enviroment easier. On a side note in the following steps I use an uppercase "C" and "S" for the "Code" and "Sites" directories solely for aesthetic reasons because all the default directories in a users home directory are title cased.

$ mkdir -p ~/Code/django-libs
$ cd ~/Code/django-libs

If you chose to download django-environment from the website then ignore this next step.

$ git clone https://github.com/epicserve/django-environment.git

Finish installing django-environment

$ mkdir -p ~/Sites
$ echo 'export DJANGO_SITES_ROOT=$HOME/Sites' >> ~/.bashrc
$ source ~/.bashrc
$ ~/Code/django-environment/django_env/bin/install

Test the the environment to make sure it is working correctly.

$ mkvirtualenv --no-site-packages example
Is this a Django-enviroment your creating (y/n)? y
Containing directory for your Django project? [Default: example.com]
Development server address? [Default: 127.0.0.1] 
Development server address? [Default: 8000] 
Create a blank Fabric fabfile in your project (y/n)? y
$ runserver

Open up Firefox and go to the address http://127.0.0.1:8000. If everything went well you should have a web page that comes up and says, "It worked! Congratulations on your first Django-powered page."

Install Nginx (optional)

If you don't want to append the port number in the URL in your browser you can install Nginx. I use Nginx on my development computer as well as my live production servers.

$ sudo aptitude install nginx

Setup nginx to run on port 80 and forward everything to the Django development server.

$ sudo rm /etc/nginx/sites-enabled/default
$ cd /etc/nginx/sites-available/

Create a file in the "sites-available" directory called "django".

$ sudo vi django

Add the following to the file and save it.

upstream  django_upstream  {
    server   127.0.0.1:8000;
}

server {
    listen   80;
    server_name  _;

    access_log  /var/log/nginx/django.access.log;
    error_log  /var/log/nginx/django.error.log;

    location / {
        proxy_pass  http://django_upstream;
    }
}

Enable the new Nginx virtual host

$ cd ../sites-enabled/
$ sudo ln -s ../sites-available/django

Restart Nginx

$ sudo /etc/init.d/nginx restart

If all went well you should be able to open up Firefox and load http://localhost and get the same Django "It worked!" page.

Related tags: Django, ubuntu, webdev

Comments

Jonny
1.   At 2:11 p.m. PDT on Oct. 8, 2011, Jonny wrote:

Great directions -- my only diff was:

My install.py ended up here:

~/Code/django-libs/django-environment/django_env/bin/install.py

NOT here:

~/Code/django-environment/django_env/bin/install

Now to start a project ! cheers

Ben
2.   At 1:37 p.m. PST on Jan. 5, 2012, Ben wrote:

Great tutorial. Had the same issue as Jonny but that solved it.

Took me a while to figure out how to use the virtualenv. For those wondering you can jump out of it with the command "deactivate" and jump back in with "workon "

Thanks for the easy setup.

Aaron Newton
3.   At 7:24 a.m. PST on Jan. 18, 2012, Aaron Newton wrote:

Two things I noticed when following this tutorial.

1) make absolutely sure you're in Bash - I was in Fish, which wasn't showing certain errors

2) I had some issues with virtualenv - see http://stackoverflow.com/questions/5071385/python-ubuntu-... for the cause and solution

Comments are closed.

Comments have been close for this post.