How to install Django on Ubuntu 14.04 LTS for python geeks

About Django

Django is a great web application development framework for the perfectionist with deadlines built in python programming language. It provides a very good structure and common methods that help to do the heavy lifting when writing web applications.

If you have never used django before and are looking for an installation guide on your Ubuntu 14.04 LTS then you are in the right place. Through this article we will teach you how to setup a very simple django development environment step by step.

Some Tools We Need

There are many different ways to install the django framework in Ubuntu. In order to stay updated with the latest python development industry standards we need to install a few tools before getting Django up and running on our machine.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

The first tool is called pip which is a very practical python package management system widely used by python developers all over the world. I mainly use pip to install packages for my python projects, generate dependencies of a project and also uninstall different python packages from my machine when they are no longer required.

Before installing pip we highly recommend to our users to update the package index on their machine with the help of the command shown below.

sudo apt-get update

The following command can be used to install the pip python package manager inside Ubuntu 14.04 LTS.

sudo apt-get install python-pip

Now we can easily install python packages on our Ubuntu 14.04 LTS machine by following the syntax shown below.

pip install name-of-python-package-here

But we do not recommend installing packages yet! Python developers use another tool during the development of their python projects.

This tool is called virtualenv. It is really useful as it helps to manage dependency conflicts of different python projects on the same machine. In short words the tool virtualenv helps to create isolated virtual environments where you can develop without any worries about package conflicts.

To install virtualenv on Ubuntu 14.04 LTS the following command can be used.

sudo pip install vitualenv

What is the next step? Do we need to make use of the tool virtualenv? For sure!

Create A Virtual Environment For Your Project

virtualenv can be easily used to create isolated virtual environments for your python projects. For example to create a virtual environment under the name of venv1 the following command can be used.

virtualenv venv1

In order to work on an isolated environment it needs to be activated. The following command does it for you.

source venv1/bin/activate

But working with virtualenv is a bit annoying as there are many different commands you need to remember and type on your terminal emulator.

The best solution is to install and use virtualenvwrapper which makes working with python virtual environments very easy. Once you have finished installing and configuring virtualenvwrapper working on a virtual environment is as easy as typing the following command.

workon venv1

Now that pip and virtualenv tools are available on your machine it is time to install and configure virtualenvwrapper.

To install virtualenvwrapper use pip like shown below.

pip install virtualenvwrapper

There are a few steps you need to follow in order to do this properly. On your command prompt run the following command.

source /usr/local/bin/virtualenvwrapper.sh
All virtual environments you create will be available inside the directory ~/.virtualenvs.

If you want to keep your virtual environments inside another directory then use the following commands like shown in the official documentation of the virtualenvwrapper.

export WORKON_HOME=~/Name-Of-DIrectory-Here
mkdir -p $WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh

I like to keep my virtual environments inside ~/.virtualenvs. My projects are inside ~/projects.

To create a virtual environment just use the command mkvirtualenv like shown below.

mkvirtualenv linuxtuts
The following output is displayed on my terminal when executing the above command.

New python executable in linuxtuts/bin/python
Installing setuptools, pip...done.

To work on the virtual environment linuxtuts use the following command.

workon linuxtuts
Once the virtual environment has been activated your command propmpt will change. Mine looks like shown below.

(linuxtuts)oltjano@baby:~/Desktop$
As you can see from the output shown above linuxtuts is the name of the virtual environment we created.

Make the changes permanent

In order for the commands offered by virtualenvwrapper to work anytime you open the terminal it is needed to make some changes in the .bashrc file.

The .bashrc file is used to setup environment variables, functions aliases and many other stuff you want to have when opening a new terminal window. Read more on .bashrc.

Open the .bashrc file with your text editor, copy paste the following in it and then save the file.

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Install Django

Inside the virtual environment use the command which python to see the python executable you are using.

which python
The above command produces the following output on my machine. Depending on the name of your directory for the virtual environments you will get a different output, but structured in a very similar way.

/home/oltjano/.virtualenvs/linuxtuts/bin/python

The above output is telling us that the python executeable being used is inside the virtual environment which in this case is linuxtuts.

Deactivate the virtual environment with the command deactivate.

deactivate

Remove the virtual environment linuxtuts with the help of the following command.

rmvirtualenv linuxtuts
The reason why we decided to remove linuxtuts is because we did not choose the version of python we want to use inside the virtual envrionment we created.

It can be done with the following command.

mkvirtualenv -p /usr/bin/python-version-here

In a project I am working on we use python3.2. To run the project I create a special environment for it.

mkvirtualenv -p /usr/bin/python3.2 linuxtuts

The above command produces the following output.


Running virtualenv with interpreter /usr/bin/python3.2
New python executable in linuxtuts/bin/python3.2
Installing setuptools, pip...done
Also creating executable in linuxtuts/bin/python

The command prompt will look similar to mine.

(linuxtuts)oltjano@baby:~/Desktop$
You can use any python version required by your project. The installation of django is very easy.

Just run the following command.

pip installl django

The above command produces the following output.

You are using pip version 6.0.7, however version 6.0.8 is available.
You should consider upgrading via the ‘pip install –upgrade pip’ command.
100% |################################| 7.4MB 40kB/s

Collecting django
Downloading Django-1.7.6-py2.py3-none-any.whl (7.4MB)
Successfully installed django-1.7.6
Installing collected packages: django

To install a different version of django than the default one specify the version like shown below.

pip install Django==1.0.4

It is up to you which version of python and django you want to use for your projects. Our mission is to help how to install them.

Setup your first simple project in django

After you have finished the installation of django in a virtual environment you probably want to start your first project.

Fortunately for us django offers management tools for your projects. The django-admin.py tool can be used to start the project.

Create a fresh virtual environment for your new project.

mkvirtualenv -p /usr/bin/python2.7 fresh_project

Note: Depending on the version of python you want to use you need to change the path listed above.

Install a new copy of django inside your virtual environment.

pip install django

Use the following comand to start a fresh django project.

django-admin.py startproject fresh_project
Then cd to your project directory and list the files inside your project. The directory structure will be similar to the one shown below.

fresh_project manage.py

The manage.py ,is another tool you have to use on your django projects. Each django project is composed of apps.

You can create a new app with the following command.

python manage.py startapp myapp

If you do an ls now the directory structure should look like the one shown below.

fresh_project manage.py myapp

It is always a good idea to generate the requirements.txt file for every project you write in django so when you show it to your friends or want to run the project in another machine you can easily install the needed packages to run the project.

Create a new file called requirements.txt on the top level directory of your project and append the packages in there using the following syntax.

django==1.9.5

As you can see from the above command you append the package name then you add its version which really important.

If you do an ls now inside your project you should get the following.

fresh_project manage.py myapp requirements.txt
And you install the requirements for the project on a new machine by using the following command.

pip install -r requirements.txt
Are you asking yourself how to run the django project? Just run it using the following command which starts the builtin django development server.

python manage.py runserver
And then visit http://127.0.0.1:8000/. You will get a message from django congratulating you running your first django project like shown in the following screenshot.
django installation finished

Conclusion

Knowledge on tools such as virtualenv and virtualenvwrapperis a must for a python developer. In this article I showed you how to install django on Ubuntu 14.04 LTS and also how to setup a very basic django development environment.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *

*