Hey, everyone, I hope you’re doing great so today I will be showing you how to install Django on Ubuntu Linux operating systems.
What is Django and why do we use it?
Django is an open source framework written in Python and used to develop web applications based on Python. A framework is a collection of built-in functions and module used to make development easier.
It can provide functionality like authentication support, management and admin panels, contact forms, comment boxes, file upload support, and more. In other words, if you were creating a website from scratch, you would need to develop these components yourself. By using a framework instead, these components are already built, you just need to configure them properly to match your site.
Django offers many modules you can use in your projects or website development.
So now we know what is Django and why is it used let’s start installing it. Now, as Django is a Python framework you must have Python installed on your machine. For installing Python simply type this in your terminal
$ sudo apt-get install python
Since Django 2.2.2 (latest version) as per now requires Python 3 to be installed so for this just type
$ sudo apt-get install python3
To check the version of the Python installed on your system type
$ python --version
$ python -V
To check the version, python 3 just type
$ python3 --version
In order to install Django you need to install ‘pip’ so you can easily download Django and install otherwise you have to download it from the site which is here.
What is pip? Pip is the standard package manager for Python. It allows you to install and manage additional packages which are not part of the Python standard library. You can install pip by typing this in your terminal
$ sudo apt-get install python3-pip
After this, we need to install the Virtual Environment.
What is Virtual environment?
A virtual environment is a tool which helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools which most of the Python developers use.
Why do we need a virtual environment?
Imagine a scenario where you are working on two web-based python projects and one of them uses a Django 1.9 and the other uses Django 2.2.2 and so on. In such situations, virtual environments can be really helpful to maintain dependencies of both the projects.
The great thing about it is that there are no limits to the number of environments you can have since they’re just folders containing a few scripts.
We use a module named virtualenv which is a tool used to create isolated Python environments. Virtualenv creates a folder which contains all the necessary executable files to use the packages that a Python project would need.
In order to install the virtual environment just type this in the terminal
$ sudo apt-get install python3-venv
$ pip install virtualenv
Now that the virtual environment is installed, we need to create a directory where we will create a virtual environment. Type this in the terminal to create a folder named project.
$ mkdir project
and then change directory to project
$ cd project
Now we will create the virtual environment here and for that, we will type
$ python3 -m venv env
It will create a folder named env inside the folder ‘project’ that we have just created like this
And as you can see there are some files inside this folder ‘env’
Now that we have created the environment, we need to activate the environment so that whenever we execute python or any command it will be loaded from this folder, for example, to see from which folder python is being loaded into memory you have to type
$ which python3
As we can see that its path is /usr/bin/python.
Now we will activate the environment and for this, you need to type this in the terminal,
$ source env/bin/activate
And if we check from where python is being loaded now we can see that here it is loaded from a new environment which is
/home/tabinda/project/env/bin/python3 which means that the virtual environment is activated.
Now let’s install Django and execute this command in the terminal
$ pip install django
After Django is installed in the new environment let us create a new project named heyworld like this
$ django-admin startproject heyworld
At this stage, Django is installed properly in the environment and we will run this project.
Also Read: How to Use 7Zip in Linux [Quick Tip]
Django has its own built-in web server which can be used for development purposes and we will use to execute our projects.
Execute these commands in the terminal
$ cd heyworld
$ python manage.py runserver
And now Django has been installed and I hope you find this article helpful. If you have any question use the comment section and feel free to leave some suggestions too.