How to Install Django Web Framework on Ubuntu

Django Web Framework Project

Django is a free, open-source web framework written in the most popular python programming language and used by millions of developers every year. Its popularity is due to its friendliness to both beginners and advanced programmers with lots of python libraries and support. Django is secure and robust enough to be used by the largest websites in the internet world like Instagram, Pinterest, Bitbucket, Mozilla, Disqus. It is also flexible enough to be a good choice for early-stage startups and prototyping personal projects.

Django inherited Python’s “batteries-included” approach and includes out-of-the-box support for common tasks in web development. Batteries-Included approach means having a rich and versatile standard library that is immediately available, without making the user download separate packages.

In this tutorial, we’ll setup the Django Framework Environment. We’ll install Python 3, Pip 3 and virtualenv, for providing necessary tools for your Django Web Application.

Install Django Video Tutorial

Install Python and Pip3

Here you are going to setup your own Django Environment on your computer in very easy instructions. This tutorial can be followed by Ubuntu, Windows and Mac users.

For installing Django you need Python and Python-Pip3 on your Computer. Don’t worry we are providing a link for installing Python: How to Install Python 3.

We can install Python and Pip3 using following commands

sudo apt-get install python3
sudo apt-get install -y python3-pip

Virtual Environment

Python applications will regularly utilize packages and modules that don’t come as a major aspect of the standard library. Applications will some of the time need a particular variant of a library.

To tackle all the problems which may occur, we use a virtual environment which is an isolated container containing all the software dependencies for a given project. For setting up a virtual environment we will be using virtualenv.

To install virtualenv, we will be using Pip3 command, as shown bellow

pip3 install virtualenv

Once it is installed, run a version command to verify that the installation has completed successfully:

virtualenv --version

We should see the following output or something similar like below:

Output
16.6.0

You have successfully installed virtualenv.

Install Django

There are two ways to install Django – Globally on your System or in Virtual Environment.

Installing Django in Virtual Environment is the recommended practice by Developers. It will remove dependencies and gives you a choice to install your own version packages.

Now we need to create a directory that will hold our Django Project. Run the following command to make a directory named django_project or name of your selection. Then move to that directory.

mkdir django_project
cd django_project 

While inside the django_project directory, create your virtual environment. Let’s call it env.

virtualenv env

Now, activate that virtual environment with the following command:

. env/bin/activate

You’ll grasp it’s activated once the prefix is modified to (env), which can look just like the following depending on what directory you’re in:

(env) huzaif@studygyaan:~/django_project$

Within the env, install the Django package using pip3. Installing Django allows us to create and run Django applications.

pip3 install django

Once installed, verify your Django installation by running a version command –

django-admin --version

It will give a similar output like bellow –

Output
2.2.2

You have successfully installed Django in your virtual environment.

Creating Your First Django Project

We now can generate an application using django-admin command, a command-line utility for administration tasks in Python. Then we can use the startproject command to create the project directory structure for our website.

django-admin startproject my_django_project .

Hurray! we have created the Django project and you will see the folder structure like this.

 .
 └── env (All ENV File)
 ├── manage.py
 └── my_django_project
     ├── init.py
     ├── settings.py
     ├── urls.py
     └── wsgi.py
 2 directory, 5 files

Now let’s confirm everything is working by running Django’s local webserver.

python3 manage.py runserver

If you visit http://127.0.0.1:8000/  or  http://localhost:8000/  you should see the following image:

How to Install Django Web Framework on Ubuntu

To stop local server press CONTROL-C. Then you can exit your virtual environment using the command deactivate

We can always reactivate the virtual environment again from the directory where we have created env:

. env/bin/activate

Done!!!. We have installed Python 3, Pip 3, virtualenv, Django and created a test project.

GitHub – Run Example Locally

This code is also available on GitHub: https://github.com/studygyaan/how-to-install-django-web-framework-on-ubuntu

Clone the Repository

git clone https://github.com/studygyaan/how-to-install-django-web-framework-on-ubuntu.git

Change Directory

cd how-to-install-django-web-framework-on-ubuntu

Create Virtual Environment – VirtualENV

virtualenv env

Activate Virtual Environment

source env/bin/activate

Run requirements file to install libraries using Pip3

pip3 install -r requirements.txt

Run the server

python3 manage.py runserver

And open http://localhost:8000/ in your browser.

You may find this useful

How To Setup Django Applications with Apache and mod_wsgi on Ubuntu

Share