There is a new version of this tutorial available for Ubuntu 22.04 (Jammy Jellyfish).

How to Install Django 1.9 on Ubuntu 15.04

Django is a web application framework written in python that follows the MVC (Model-View-Controller) architecture, it is available for free and released under an open source license. It is fast and designed to help developers get their application online as quickly as possible. Django helps developers to avoid many common security mistakes like SQL Injection, XSS, CSRF and clickjacking. Django is maintained by the Django Software Foundation and used by many big technology companies, government, and other organizations. Some large websites like Pinterest, Mozilla, Instagram, Discuss, The Washington Post etc. are developed with Django.

In this tutorial, we will install Django 1.9 on a Ubuntu 15.04 server. Django can be installed on a server in many ways, in this tutorial, I will show you 3 different ways to install Django:

  1. Django installation with pip.
  2. Install Django with virtualenv.
  3. Install Django fron it's github repository.

When the Django installation is done, I will show you the first steps to start a new project with the Django web framework.

Prerequisites

  • Ubuntu 15.04 - 64bit.
  • Root privileges.

Step 1 - Update the Ubuntu Repository

Before we start with the Django installation, we should update the ubuntu repository. Log into the server and gain sudo/root privileges:

sudo su
apt-get update

Step 2 - Install Django

In this step, I show you 3 different methods to install Django. Please select the one that suits best. So use either Pip, virtualenv or GIT but not all three methods at once.

Pip is a package management system for python. Python packages can be managed and installed easily with pip. Python has their own central package repository server, so we can download the python packages from there, it's called Python Package Index (PyPI).

In this tutorial, we will use Python version 3 for Django as recomended from the official Django site. If you have python 2 installed, you can install pip2. But in this tutorial we will install pip for Python 3, install python3-pip from the Ubuntu repository with the following apt command:

apt-get install python3-pip

The installation will install a new binary file called 'pip3', to makes it easy to use pip, I'll create a symlink for pip3 to pip :

which pip3
ln -s /usr/bin/pip3 /usr/bin/pip

Now check the version:

pip -V

PIP Installation.

The pip installation is done, and we can now use the pip command to install python packages.

1. Install Django with Pip

 Let's install django now in the server with the pip command below:

pip3 install django==1.9

Note: Use django==1.9 to install a specific version, in our case version 1.9. If you want a different version, just change the number to e.g. django==1.8.

When the installation is done, check the django version with the command below:

django-admin --version

We can use the short python script below as well to verify the Django version:

python3
import django
print(django.get_version())

Django is installed.

As you can see, Django 1.9 is installed on the system with pip.

2. Install Django with Virtualenv

Virtualenv is a python environment builder, it is used to create isolated python environments. We can select the version of python that will be installed in the environment. This is very useful for developers, they can run and develop the application with different python versions and different environment settings on one OS.

Virtualenv is available on PyPI, we can install it with the pip command:

pip install virtualenv

When the installation of virtualenv is done, we can use the virtualenv command to create a new python environment. So let's create it with python3 as the python version and pip3 for the django installation and project.

virtualenv --python=python3.4 mynewenv

Note :

--python=python3.4 is a binary file for python 3.

mynewenv is the name of the environment.

The command will create a new directory called 'mynewenv' that contains the directories bin, include and lib.

The "virtualenv" environment is created, now log into the virtual environment with the command below:

source myproject/bin/activate

If you do not have a source command, you can run this command:

. mynewenv/bin/activate

Django Python Virtualenv.

Note : If you want to get out of the virtual environment, you can use the command 'deactivate'.

Now check the pip version:

pip -V

Pip will automatically installed inside the virtual environment.

Next, install django in the virtualenvironment that we've created:

pip install django==1.9

When the installation finished, check the django installation:

django-admin --version

Django 19 Virtualenv.

Django 1.9 has been installed successfully in our virtualenvironment.

3. Install Django from Git Repository

In this part, we will install the Django web framework on the system directly and not in a virtual environment. I will show how to install the latest code manually from the Django GIT repository. Make sure that you have installed git on your server, if you have no git yet, install with command below:

apt-get install git -y

Now clone the django git repository with command git below:

cd ~
git clone git://github.com/django/django django-dev

And install django with the pip command below:

pip install -e django-dev/

-e =  Install a package in editable mode or local package, in this tutorial we install django from the local code that we've cloned.

When the installation process finished, check the Django version on the server:

django-admin --version
1.10.dev20151213153331

We see that the Django version is 1.10 dev.
Thats all to install the latest Django version manually.

Step 3 - Create your first Project with Django

In this chapter, we will install Django in a virtual environment and then start our first project with django.

Install virtualenv on the server and create a new environment named 'firstdjango':

pip install virtualenv
virtualenv --python=python3.4 firstdjango

Now go to the firstdjango directory and activate the virtual environment, then install Django with the pip command:

cd firstdjango/
source bin/activate
pip install django==1.9

First Django project.

Django has been installed.

Next, create a new project called 'myblog' with the django-admin command:

django-admin startproject myblog

This will create a new directory "myblog" which contains the Django files:

ll myblog

-rwxr-xr-x 1 root root  249 Dec 15 09:01 manage.py*
drwxr-xr-x 2 root root 4096 Dec 15 09:01 myblog/

Go to the "myblog" directory and run the 'manage.py' file:

cd myblog/
python manage.py runserver

The runserver option will create a HTTP connection with python on localhost and port 8000. If your development environment is on the server, in this example here I'm using a ubuntu server with IP: 192.168.1.100, you can use the server IP to access it from outside of the server.

python manage.py runserver 192.168.1.106:8000

Now check from your Browser: 192.168.1.106:8000

The Django Server is running.

The Django default page show up, so Django is working correctly. On the shell of the server you can verify that in the access log:

[15/Dec/2015 09:08:12] "GET / HTTP/1.1" 200 1767

Now we will configure "Django admin", Django will automatically generate the database for a superuser, before we create the superuser, run command below:

python manage.py migrate

migrate: adds your models (adding fields, deleting etc) into the database scheme, the default database is sqlite3.

Now create the admin user:

python manage.py createsuperuser

Username (leave blank to use 'root'): admin
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.

The Django super user has been added, now you can use the runserver command to start Django admin. Open the browser and visit the Django admin page:

python manage.py runserver 192.168.1.106:8000

Visit Django admin page at /admin/ URL: 192.168.1.106:8000/admin/. Login with the username "admin" and your password, you should see the admin page:

The Django Admin Dashboard.

Now Django is installed inside the virtual environment and we've created a Django sample project named 'firstdjango'.

Conclusion

Django is a web framework based on the Python programming language, it is released as free software under an open source license and maintained by the Django Software Foundation. Django is very fast and allows it to build web applications rapidly. Django is a web framework that uses the MVC (Model-View-Controller) paradigm. We can install Django on a server with the pip command, in a virtual environment with virtualenv and directly from the Django git repository.

Share this page:

3 Comment(s)