How to install Django on Ubuntu 14.04
Django is a Python web framework. All it does is to help you rapidly build high performance and efficient web applications. Its very much liked by the developer community because of some of its amazing features like template system, URL design,etc. Django supports both Python 2.7.x and Python 3.x . Some of the famous web applications built using Django are :
Instagram:A photo sharing app for android and IOS.
Matplotlib:A powerful python 2D plotting Library.
Mozilla:creators of firefox, browser and OS.
And many more. This encouraged me to start learning Django and try building my own web application. But when I started searching for resources, I found it difficult as a beginner to find and install what I needed to get it running. I was confused because of the variety of choices I was given when I decided to install and set it up. But after a lot of searching and experimenting, I found the exact method to do it , which is good enough for a beginner. So, let's start.
If you don't use Ubuntu 14.04, we have a Django tutorial for Ubuntu 16.04 at howtoforge as well.
1 Installing Python
The first step is to install python. Generally most linux OS have python 2.7 installed by default. To check if it exists, use the following command:
you may get an output similar to
or any other version installed. If not, then, it can be downloaded from HERE.
2 Installing a database system (SQLite)
Since most of the web applications need a database and querying has to be done upon it, it's better to have a database setup on your system. Django provides the usage of database engines like
PostgreSQL, MySQL, SQLite, Oracle.
SQLite is a database we can use, it is a light weight database and its good enough to begin with. For any simple web applications that you develop, you can use SQLite itself and later upgrade it to suit your needs. So, to install SQLite, use the following command:
sudo apt-get install sqlite
Please do note that in some linux systems SQLite is preinstalled along with python, in such cases, the above command can be ignored.
3 Installing pip and easy_install
Any previous versions of Django if existing has to be removed. But if you have
for installation then you don't have to worry about removing the previous versions because the pip or easy_install will do it for you. So, install both of them by using the commands:
sudo apt-get install python-setuptools
The above command installs the required python setup tools along with easy_install. Most of the cases, "pip" is preinstalled. If in any case it isn't, install pip as given in the official documentations HERE.
Before proceeding, confirm that python, SQLite, pip and easy_install has been installed. To do so, use the commands one after another given in the image below and the output of each command should be similar(not same) as shown in the image below.
4 Installing a virtual environment
In this step, we install a "Virtual Environment." After a lot of searching and testing, I found that Django can be run very easily on a virtual environment. A virtual environment is created to encapsulate all the data and resources required to run Django at one place so that all the changes made remain in that environment itself. Another important benefit of the virtual environment is that it supports the light weight web server provided by Django by default. This allows the installation and integration of apache server to be avoided.
One of the easiest way to install virtual environment on linux is by using the "easy_install" command. This script comes with a package called python-setuptools which we have installed in a previous step. So now, we can install the environment using the following command:
sudo easy_install virtualenv
Be patient, as it may take some time depending on the speed of the internet. When finished, the terminal output should be similar to the image below.
5 Creating and setting up the virtual environment
Now we create a folder using virtualenv so that the folder can act as the virtual environment to contain Django. Type the following command in the terminal:
virtualenv --no-site-packages django-user
Here django-user is the folder that will be created and used as the environment. It will be created under the directory you are currently in. Now to start the environment use the command:
Now if you see your folder name
at the beginning of the prompt , it means that the environment is started. Refer to the image below.
Navigate to the folder django-user using the command.
Upon listing the items in the folder using the "ls" command, you will be able to see directories like bin, lib, include, local. So what this virtual environment does is that any command or operation performed in the environment will not affect anything outside the environment. So the changes are isolated and this allows us to easily create as many environments as we want and test many things very easily.
6 Installing the Django framework
The final step is installing Django within this environment that we have created in the previous step. Remember that you still have to be in the virtual environment in the django-user folder else django will be installed outside the environmant and cannot be used. To install Django use the command:
As a reference, view the following image. Note that the beginning of the prompt says (django-user) which means that you are currently in the virtual environment and before installing django, you should be within the "django-user" directory. This is very important.
Thats it! Django is installed on your system with all required functionality for beginners to develop and learn the framework. Now you can go ahead and try out the DJANGO tutorial to learn the different functionalities and run your first web app. You can find the tutorial in the official Django documentation HERE.