How to Install Django on Ubuntu 20.04

Install Django on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure Django on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Django is a high-level Python Web framework that is free, open-source, and rapid development. It provides higher security for the application and avoids developers for making common security mistakes. By using Django you can build secure, scalable, and maintainable dynamic web applications.

Install Django on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install the Python virtual environment.

If your system doesn’t have Python installed, Execute the below commands to install it. Also, install pip on your system:

sudo apt install python3 python3-pip

Now check the python command as below:

python3 -V

And pip version is:

pip3 -V

Step 3. Install Django on the Ubuntu system.

  • Install the Django using pip.

Run the below command:

pip3 install Django

Verify the installation using the following command which will print the Django version:

django-admin --version
  • Install Django using Apt.

To install the package, run the apt command below:

sudo apt install python3-django

Check the Django version:

django-admin --version

Step 4. Create a new Django project.

Now, I will create a new project called linuxtips:

cd /var/www/
sudo django-admin startproject linuxtips

Next, navigate into the linuxtips folder and migrate to set the initial configuration:

cd linuxtips 
sudo python3 manage.py migrate

Then, you have to create the superuser to manage the project:

sudo python3 manage.py createsuperuser

The next step is to edit the project configuration file to make it accessible from any computer:

nano example/settings.p

And edit the following entry:

ALLOWED_HOSTS = ['YOUR_COMPUTER_IP_ADDRESS_WHICH_IS_RUNNING_DJANGO']

### EXAMPLE ###

ALLOWED_HOSTS = ['192.168.77.21']

Next, serve the project:

sudo python3 manage.py runserver 0.0.0.0:8000

Step 5. Accessing the Django Web App

Once installation and create project done, open http://192.168.77.21:8000 in your web browser and you will be presented with the default Django landing page:

Install Django on Ubuntu 20.04

That’s all you need to do to install Django on Ubuntu 20.04 Focal Fossa. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.