Django is a web application framework written in python that follows the MVC (Model-View-Controller) architecture, it is available for free and released under an open-source license. It is fast and designed to help developers get their applications online as quickly as possible. Django helps developers to avoid many common security mistakes like SQL Injection, XSS, CSRF, and clickjacking.

Django is maintained by the Django Software Foundation and used by many big technology companies, governments, and other organizations. Some large websites like Pinterest, Mozilla, Instagram, Discuss, The Washington Post, etc. are developed with Django.

In this tutorial, we will show you how to install Django 3.0 on the latest Ubuntu 20.04 server. we will install Django with Python 3 (as default version on Ubuntu 20.04), and we will show you 4 different ways to install Django Framework, install through the Apt repository, pip, and using the Python Virtualenv.

What we will do?

  • Setup Python and Pip
  • Install Django Framework
  • Create Your First Project with Django

Step 1 – Setup Python and Pip

In this first step, we will set up Python and Pip on the Ubuntu 20.04 system.

By default, the Ubuntu 20.04 cames with the Python 3 as default python version. it’s available as a ‘python3’ command, not a ‘python’.

Check the python command as below.

python

You will get the ‘python’ command is not found.

Command 'python' not found, did you mean:

  command 'python3' from deb python3

  command 'python' from deb python-is-python3

To solve this, make a symbolic link of the Python3  binary ‘/usr/bin/python3’ to ‘/usr/bin/python’ as default python command using the following command.

update-alternatives --install /usr/bin/python python /usr/bin/python3 1

Now check the python command as below.

python --version

And you will get the following response.

Python 3.8.2

The configuration of Python3 has bee completed.

<img alt="setup Python" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/1.png5ecfec51a6cb9.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="262" src="data:image/svg xml,” width=”750″>

Next, we will install the package manager for python packages ‘python3-pip’.

Install python3-pip using the apt command below.

sudo apt install python3-pip -y

Once all installation is completed, make the ‘pip3’ command as default ‘pip’ version.

update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1

After that, check the pip command below.

pip --version

Below is the result you will get.

pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

As a result, the installation of the Python package manager (pip) has been completed.

<img alt="Install Python Pip" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/2.png5ecfec51e574c.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="226" src="data:image/svg xml,” width=”750″>

Step 2 – Install Django Framework

After installing and configure Python and Pip, we will install the Django Framework on our system. Thre are 4 different to install the Python Django Framework, install using the Apt repository, using pip command, using the virtualenv, or using the Django git version.

Install Django with Apt

By default, the Ubuntu 20.04 repository provides packages for Django Framework named ‘python3-django’. At this time (tutorial writing), the Ubuntu repository provides the Django 2.2.12, which is not the latest stable version of Django.

Check the ‘python3-django’ package using the apt command below.

apt show python3-django

And you will get details about the ‘python3-django’ package.

To install the package, run the apt command below.Advertisements

apt install python3-django

Once all installation is completed, check the Django version using the following.

django-admin --version

You will get the following result.

<img alt="Install Python Django Framework using Apt" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/3.png5ecfec5247571.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="358" src="data:image/svg xml,” width=”750″>

As a result, Django 2.12 is installed on the ubuntu 20.04 through the official Ubuntu repository.

Install Django with PIP

The main advantage of installing the Django Framework with the pip command through the PyPI repository is that you can choose the right version of Django for your project.

For this section, we will install the Django 3.0 (latest stable version) from the PyPi repository.

Install Django framework 3.0 using the pip command below.

pip install django==3.0.0

Once all installation is completed, check the Django version using the following command.

django-admin --version

And you will get the Django version 3.0 as the response.

optionally, you can check through the Python interactive shell, run the python command below.Advertisements

python

Import the Django module and show the Django version using the following query.

import django

print(django.get_version())

You will get the Django 3.0 response as below.

<img alt="Install Python Django with Pip" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/4.png5ecfec52881ba.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="314" src="data:image/svg xml,” width=”750″>

As a result, you’ve successfully installed the Django 3.0 using the PyPi repository.

Install Django with Virtualenv

This is the most recommended practice for installing Django Framework, by using the python Virtualenv. It’s a python environment builder that can be used to create isolated python environments.

You can choose the Python version that will be installed in the virtual environment, and all installation in the Python virtual environment will not affect the system. This is very useful for developers, they can run and develop an application with different python versions and different environments on one OS.

First, install the Python virtualenv packages from the PyPi repository using the pip command below.

pip install virtualenv

Once all installation is completed, create a new Python virtual environment ‘myenv’ using the following command.

virtualenv myenv

Now go to the ‘myenv’ directory and activate the virtual environment using the command as below.

cd myenv/

source bin/activate

After that, install the Python Django Framework using the pip command below.

pip install django==3.0.0

And the Python Django Framework is now installed on the Python virtual environment. Check the Django version using the following command.

django-admin --version

You will get the Django 3.0 as the response, and the Django Framework is installed on the Python virtual environment.

To back to the normal user and deactivate the virtual environment, run the command.

deactivate

Now you will be switched back to the normal user environment of your system.

<img alt="Install Python Django Framework using Virtualenv" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/5.png5ecfec52d1a16.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="303" src="data:image/svg xml,” width=”750″>

Install Django from Git Repository

This method is recommended if you want to install the development version of Django Framework.

First, install the git packages using the apt command below.

apt install git

Next, create a new virtual environment named ‘django-git’ using the virtualenv command below.

virtualenv django-git

Go to the ‘django-git’ directory and activate the virtual environment.

cd django-git/

source bin/activate

Clone the latest Django version from the official Django GitHub repository and install the Django development version using the pip command as below.

git clone git://github.com/django/django django-dev

pip install -e django-dev

Once all installation is completed,  check the Django version.

django-admin --version

And you will get the latest version of the Django Framework development version.Advertisements

<img alt="Install Python Django from Source" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/7.png5ecfec53392e7.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="434" src="data:image/svg xml,” width=”750″>

Now you’ve learned how to install Python Django using multiple ways.

Step 3 – Create Your First Project with Python Django

After installing the Django project, we will show you how to start a new project with the Python Django framework.

Create a new virtual environment ‘myproject’.

virtualenv myproject

Go to the ‘myproject’ directory and activate the virtual environment.

cd myproject/

source bin/activate

After that, install the Django 3.0 using the pip command as below.

pip install django==3.0.0

Now you’ve installed the Django Framework.

<img alt="Create Python Django Project" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/8.png5ecfec5377ac5.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="256" src="data:image/svg xml,” width=”750″>

Create a Django project named ‘mysite’ using the ‘django-admin’ command as below.

django-admin startproject mysite

Now you will get a new directory named ‘mysite’ which contains all Djaango configuration.

Go to the ‘mysite’ directory and check all files configuration.

cd mysite/; tree

Below is the result you will get.

<img alt="Create New Project Django" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/9.png5ecfec53b3830.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="321" src="data:image/svg xml,” width=”750″>

Next, edit the configuration ‘settings.py’ inside the ‘mysite’ django directory.

vim mysite/settings.py

On the ‘ALLOWED_HOSTS’ configuration, input your IP address as below.

ALLOWED_HOSTS = ["your-server-ip"]

Save and close.

Next, we will migrate the database for our Django project. By default, the Django framework used the SQLite database.

Migrate the Django database using the following command.

python manage.py migrate

After that, create a new admin user for your Django project.

python manage.py createsuperuser

Now type your username, email address, and the password, then press enter.

<img alt="Django Migrate Database and Create Superuse" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/10.png5ecfec53eced7.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="568" src="data:image/svg xml,” width=”693″>

As a result, the default database SQLite for the Django project has been migrated, and the admin user has been created.

Step 4 – Start the Python Django Project

After migrating the database of the Django project, we will run the Django project.

Run the Python Django runserver command below.

python manage.py runserver 0.0.0.0:8000

As a result, your Django project is now up and running on port ‘8000’.

<img alt="Running Django Project" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/11.png5ecfec5437344.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="258" src="data:image/svg xml,” width=”750″>

Open your web browser and type the server IP address with port ‘8000’ on the address bar.

http://10.5.5.32:8000/

And you will get the default Django home page.

<img alt="Django Index.html" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/12.png5ecfec547db23.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="383" src="data:image/svg xml,” width=”750″>

Next, add the ‘admin’ path on the address bar and you will get the Django admin login page.

http://10.5.5.32:8000/admin/

Type your username and password, then click the ‘Login’ button.

<img alt="Django Admin Login" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/13.png5ecfec54b3728.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="339" src="data:image/svg xml,” width=”750″>

And you will get the default Django admin dashboard as below.

<img alt="Django Admin Dashboard" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/05/echo/14.png5ecfec54eebfa.jpg" ezimgfmt="rs rscb1 src ng ngcb1" height="248" src="data:image/svg xml,” width=”750″>

As a result, the installation of the Django Framework with Python 3 on Ubuntu 20.04 has been completed successfully.