How to Install Django on Ubuntu 20.04

It is not for nothing that Django is one of the most widely used web programming frameworks in the world. It allows us to use Python for our web applications which makes it popular thanks to the programming language it uses. Today, in this post, I will show you how to install Django on Ubuntu 20.04 and get it ready for you to start creating your application.

Introducing to Django framework

When we talk about web applications, we end up thinking about PHP. This language although very good and popular is not the only one that we can take advantage of to develop our applications. Python is also present thanks to frameworks like Django which is one of the most complete that exist.

According to Django’s official website

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

With Django, we can find a lot of things in other PHP-based frameworks but there are three features that the developers take a lot of credit for

  • Ridiculously fast because Django was designed to help developers take applications from concept to completion as quickly as possible.
  • Reassuringly secure It helps developers avoid many common security mistakes.
  • Exceedingly scalable Some of the busiest sites on the Web leverage Django’s ability to quickly and flexibly scale.

So, if you know some Python then you’re good to go with Django. Let’s go for it.

Install Django on Ubuntu 20.04

Before starting the installation, it’s a good idea to have your system up to date. That’s why you have to open a terminal from the main menu or by pressing CTRL + ALT + T keys and run

sudo apt update
sudo apt upgrade

Now install PIP which is the Python package manager. It will make it very easy to install.

sudo apt install python3 python3-pip

Generally, the version of pip that is included in the official repositories is outdated so we have to update it.

sudo -H pip3 install -U pip

At the end of the process, we can check the new pip version with the command.

pip3 -V
pip 21.2.2 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)

This way with pip updated we can continue.

Install and configure Django on Ubuntu 20.04

Now install Django by running the following command:

sudo -H pip3 install Django

Or if you prefer not to use the sudo command

pip3 install Django

The process shouldn’t take long, so when it’s finished, you can check the Django version with the following command:

django-admin --version
3.2.5

Now we have to start configuring Django before using it.

First, we have to create a new project. To do this you have to run:

django-admin startproject newproject
Creating a new Django project
Creating a new Django project

Replace newproject with the name of your project.

Running this command will create a new folder with the name of the project.

Access it, and from there perform the first migration to set the primary settings of the project.

cd newproject/
python3 manage.py migrate

The next step is to create an admin user for the project. As you can see Django takes security seriously. To do this, you can run:

python3 manage.py createsuperuser
Creating a new Django Superuser
Creating a new Django Superuser

You will be shown an interactive screen where you will have to define the user name, his Email, as well as the password. This password has to be strong, otherwise you will be shown a warning.

Then you have to make a configuration in the configuration file to set who can access Django. Again, this is for security purposes.

So, open the file

nano nano newproject/settings.py

Locate the ALLOWED_HOSTS line and add the IP address where Django is running.

Save the changes and close the editor.

To run the project in developer mode, you have to run

python3 manage.py runserver 0.0.0.0:8000
Serve the Django project in development mode
Serve the Django project in development mode

This will make any host on port 8000 accessible. This configuration is especially useful if you have installed Django on a virtual machine or on a server.

Now open your web browser and go to http://localhost:8000 or http://server:8000 and you will see this screen.

Django installed on Ubuntu 20.04
Django installed on Ubuntu 20.04

This indicates that Django is installed correctly and you can start working on your application.

Conclusion

Django is one of the most robust frameworks out there. Being based on Python makes it very popular among developers who are looking for maximum security but fast to create their applications. Now you know how to install it on Ubuntu 20.04

Share this post with your friends and leave us a comment.

Share This: