Setup a Django Dev Environment in Ubuntu
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
- Save this article for later, bookmark it!
- del.icio.us digg newsvine blinklist magnolia
Post Your Comment
Post Guidelines
Please be considerate of others. Keep comments relevant. Content deemed inappropriate or offensive may be edited and/or deleted. Email addresses are never displayed.
Line breaks and paragraphs are automatically converted — no need to use p or br/. Quotes, apostrophes, and double-dashes are automatically converted to smart punctuation. Be careful when copying and pasting portions of entries or other comments.
Links can be created using the standard <a href="http://url">urlName</a>. The following inline HTML elements may also be used: strong, em, cite, & code. The title attribute is allowed within any element. All other code will get removed before posting.
Latest Photos
Good Reading
- Endeca User Interface Design Pattern Library | UX Booth
- Netflix – An example of multi-device UX | UX Booth
- The UXBooth Reviews Silverback | UX Booth
- Building a custom HTML5 video player with CSS3 and jQuery - Opera Developer Community
- The easiest way to write your life story | OhLife
- Python Package Index : pyflakes 0.4.0