Installing A Django Development Environment on Ubuntu

Django is easily the most popular web framework written in Python. It strikes a delicate balance between feature completeness and efficiency, including powerful features like automatic migration generation and a full-featured admin interface. Setting up a Django development environment in Ubuntu is fairly easy, and can be done in only a few steps.

Installing Virtualenv

It’s best to use a Python virtual environment when developing in a complex framework like Django, especially if you intend to work on multiple projects at once. It’s also probably a good idea to make sure that you have both versions of Python up to date.

$ sudo apt-get install virtualenv python python3

Using Virtualenv

Creating The Environment

Virtualenv allows a project to be sandboxed away from the system’s Python install. This allows a regular user to install Python packages and projects to use specific versions that may be different than the system versions. Creating a virtual environment with Virtualenv can be done with one command.

$ virtualenv -p python3 project-directory

The option -p python3 specifies the version of Python to use in the environment. You can get more specific with something like -p python3.4, but just running it like in the example above will choose the latest release of that chosen Python version currently installed on the system. Virtualenv will not pull in new versions of Python that aren’t installed on the system.

Activating The Environment

To use Virtualenv, cd into the directory that you created, then activate the environment.

$ cd project-directory
$ source bin/activate

You will see the name of the directory appear at the beginning of your command prompt in parentheses. This indicated that you are using the virtual environment.

Installing Django

The virtual environment includes the Python package manager, Pip, to allow you to easily install and manager Python packages. Django is one of those packages, as are many of its add-ons and plug-ins. Pip can be used similarly to a distribution’s package manager, so use it to install Django, and it will pull in all of Django’s dependencies as well.

$ pip install django

This will pull in the newest available version of Django. If you want a specific version, it can be specified to Pip as well.

$ pip install django==1.9

Setting Up A Django Project

Now, you can start up a Django project and get working. The Django package that Pip installed provides a command to create the project.

$ django-admin startproject project-name

From there, cd into your project.

$ cd project-name

django-admin set up the project as well as the basic files needed to get started. If you look in the current directory, you will see them. The most important one for getting set up is manage.py. It is the central management script for the entire project. It is also the the script used to create database migrations and migrate them. So, use it to create any migrations, migrate, then set up your superuser account for the admin interface.

$ python manage.py makemigrations
$ python manage.py migrate
$ python manage.py createsuperuser

Closing

That’s it. Now, your Django project is set up and ready to go for development! When you’re done, you can leave your virtual environment with another simple command.

$ deactivate