How to Install the Django Web Framework on Ubuntu 20.04

Reading Time: 5 minutes

What is Django?

Django is a Python-based web framework that is used for developing complex, database-driven websites. It also operated under an open-source license indicating it is free to use. Django is ultra-fast and encourages security, and it is exceptionally adaptable, which is the cause of its immense popularity.

Using Django, we can create a new app or a website in a short amount of time. Django is explicitly designed to help developers write less code as this framework takes care of much of the initial structure.

In this article, we will learn how to get Django up and running on Ubuntu 20.04 server using several different methods to achieve this.

Installation Methods

Installing Django on Ubuntu 

Django packages are incorporated into the official Ubuntu repositories by default, so we can install them using the apt package manager. This global installation method is relatively easy to complete. However, it is not as flexible as some other methods we will be discussing. The main downside to this method is that the Ubuntu repositories might not be the latest official versions available from the Django project.

Installing Django Using PIP

This method of installing Django is the most popular one. Using the pip command via the PyPI repository allows us to choose the exact version of Django we need to install. This method is a global installation. 

Installing Django in a Virtual Environment

Developers prefer this method most often as it allows them to choose the version of Python as well as the version of Django that will be installed. Thus specific method will enable us to run and develop applications within a closed virtual environment without affecting the global system.

Installing Django from a GitHub Repository

In case you want to install one of the development versions of Django, then this is the method to use. While official repositories offer the latest stable version, the Git repository allows us to download the latest development version. Of course, keep in mind a development version will offer all the latest features; however, it may not be as stable.

Prerequisites

  • To install Django and its prerequisites, we will need to be logged into our server as a user with sudo privileges or as the root user.
  • Python
  • Pip – package manager for python packages. (A tutorial on how to install PIP can also be found in our Knowledge Base.)

Install Django Using Apt

Install Updates

This installation method is pretty straightforward, so let’s begin by updating our packages and doing any upgrades needed by running the following commands.

 ikettnich@ubuntu-django:~$ sudo apt update -y
 ikettnich@ubuntu-django:~$ sudo apt -y upgrade 

Confirm Python Version

Once we have updated our packages, let’s confirm that Python is installed. Since we are using Ubuntu 20.04 and Python 3.8 is preinstalled, the exact version can be checked using the following command.

 ikettnich@ubuntu-django:~$ python3 -V
 Python 3.8.5 

Now that we have confirmed Python is present, the next step is to install Django,  

Install Django

To begin use the following command.

ikettnich@ubuntu-django:~$ sudo apt install -y python3-django

Confirm Installation

And that’s it. We can confirm the software was successful using the following command.

 ikettnich@ubuntu-django:~$ django-admin --version
 2.2.12 

Install Django Using the pip Command

As mentioned earlier, the advantage of using this method is that we can choose which version of Django is installed. Let’s begin by installing the python3-pip package as the pip command is the primary focus of this method.

Install PIP

ikettnich@ubuntu-django:~$ sudo apt install python3-pip -y

Once the installation completes, let’s confirm it and check the pip version by using the following command.

 ikettnich@ubuntu-django:~$ pip3 -V
 pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8) 

Install Django

Next, we can proceed to install the latest stable version of Django, 3.1.4. 

ikettnich@ubuntu-django:~$ sudo pip3 install Django==3.1.4

Check Django Version

And finally, let’s confirm our Django version

 ikettnich@ubuntu-django:~django-admin --version
 3.1.4 

We could also confirm the installed version via Python shell, run the python3 command

 ikettnich@ubuntu-django:~ python3
 Python 3.8.5 (default, Jul 28 2020, 12:59:40)
 [GCC 9.3.0] on linux
 Type "help", "copyright", "credits" or "license" for more information.
 >>>  

Now, import the Django module and check the version by using the following query

 >>> import django
 >>> print(django.get_version())
 3.1.4 

Install Django Using pip in a Virtual Environment

Install Venv

We’ll begin by installing the python3-venv package that we will use to create our virtual environment for this method. Then we will create our virtual environment. 

ikettnich@ubuntu-django:~$ sudo apt install python3-venv

Create Virtual Environment

To create our virtual environment, we’ll need to make the directory we’re going to use and move into this new directory. Of course, feel free to name it whatever you like.

 ikettnich@ubuntu-django:~$ mkdir new_django_project && cd new_django_project
 ikettnich@ubuntu-django:~/new_django_project$ 

In our new_django_project directory, we’re now going to make a virtual environment called django_env.

ikettnich@ubuntu-django:~/new_django_project$ python3 -m venv django_env

Activate Virtual Environment

Next, we activate it using the following command.

 ikettnich@ubuntu-django:~/new_django_project$ source django_env/bin/activate
 (django_env) ikettnich@ubuntu-django:~/new_django_project$ 

You’ll now notice that the name of our virtual environment is preceding our command prompt. This indicates we are now working within our virtual environment.

Install Django

We can proceed with the Django installation using the pip command. To construct a bit of a different environment, we’ll use version 3.0.8 this time.

 (django_env) ikettnich@ubuntu-django:~/new_django_project$ sudo pip3 install Django==3.0.8
 Collecting Django==3.0.8
   Downloading Django-3.0.8-py3-none-any.whl (7.5 MB)
      |████████████████████████████████| 7.5 MB 2.0 MB/s
 Requirement already satisfied: pytz in /usr/lib/python3/dist-packages (from Django==3.0.8) (2019.3)
 Requirement already satisfied: sqlparse>=0.2.2 in /usr/lib/python3/dist-packages (from Django==3.0.8) (0.2.4)
 Requirement already satisfied: asgiref~=3.2 in /usr/local/lib/python3.8/dist-packages (from Django==3.0.8) (3.3.1)
 Installing collected packages: Django
 Successfully installed Django-3.0.8 

Verify Installation and Version

And once again, let’s confirm our Django version

 (django_env) ikettnich@ubuntu-django:~/new_django_project$ django-admin --version
 3.0.8 

Exit Virtual Environment

To exit our virtual environment, we’ll use the deactivate command as follows.

 (django_env) ikettnich@ubuntu-django:~/new_django_project$ deactivate
 ikettnich@ubuntu-django:~/new_django_project$ 

If you later decide to go back to that specific project, move your project directory back and reactivate your virtual environment again.

 ikettnich@ubuntu-django:/$ cd ~/new_django_project/
 ikettnich@ubuntu-django:~/new_django_project$ source django_env/bin/activate 

Test Alternative Python Versions

It is also possible to test with Django on other Python versions in our virtual environment if we so choose. Here is the knowledgebase article that will teach us how to update our Python version yo 3.9. The official documentation also provides a guide on “which Python version to use with which version of Django.”

Installing Django from a GitHub Repository

Lastly, for those who like to test newer Django options to get all the latest updates without worrying too much about any possible issues, we can install a development version of Django using the git command

Create Virtual Environment

For this install, let’s create new virtual environment.

 ikettnich@ubuntu-django:~$ mkdir development_django
 ikettnich@ubuntu-django:~$ cd development_django/
 ikettnich@ubuntu-django:~/development_django$ python3 -m venv dev_django_env 

Activate Virtual Environment

 ikettnich@ubuntu-django:~/development_django$ source dev_django_env/bin/activate
 (dev_django_env) ikettnich@ubuntu-django:~/development_django$ 

Clone Django Repository

As we prepared the virtual environment we are now ready to clone the Django repository.

 (dev_django_env) ikettnich@ubuntu-django:~/development_django$ git clone git://github.com/django/django ~/django-dev
 Cloning into '/home/ikettnich/django-dev'...
 remote: Enumerating objects: 11, done.
 remote: Counting objects: 100% (11/11), done.
 remote: Compressing objects: 100% (10/10), done.
 remote: Total 467691 (delta 1), reused 5 (delta 1), pack-reused 467680
 Receiving objects: 100% (467691/467691), 197.98 MiB | 25.17 MiB/s, done.
 Resolving deltas: 100% (341516/341516), done. 

Install Django Using PIP

Next, we’ll now install the cloned repository by using pip command. This time we’ll use the -e flag (editable) as it’s needed while installing from version control.

 (dev_django_env) ikettnich@ubuntu-django:~/development_django$ pip install -e ~/django-dev
 Obtaining file:///home/ikettnich/django-dev
 Collecting asgiref>=3.2.10
   Downloading asgiref-3.3.1-py3-none-any.whl (19 kB)
 Collecting pytz
   Downloading pytz-2020.4-py2.py3-none-any.whl (509 kB)
      |████████████████████████████████| 509 kB 26.2 MB/s
 Collecting sqlparse>=0.2.2
   Downloading sqlparse-0.4.1-py3-none-any.whl (42 kB)
      |████████████████████████████████| 42 kB 2.2 MB/s
 Installing collected packages: asgiref, pytz, sqlparse, Django
   Running setup.py develop for Django
 Successfully installed Django asgiref-3.3.1 pytz-2020.4 sqlparse-0.4.1 

Check Django Version

Finally, let’s check our version to confirm the installation.

 (dev_django_env) ikettnich@ubuntu-django:~/development_django$ django-admin --version
 3.2 

If you decide to update your copy of development Django, you can run the command git pull from within your Django directory. This option will pull down any changes using the git download command.

Reserver Your Spot Now!

In this tutorial, we have learned how to install Django using four different methods, and now it’s time to start building our first Django project. Have fun, play with it, and use it to its full potential!

We pride ourselves on being The Most Helpful Humans In Hosting™!

Our technical support staff is always available to assist with any issues related to this article, 24 hours a day, 7 days a week 365 days a year.

We are available, via our ticketing systems at support@liquidweb.com, by phone (at 800-580-4986) or via a LiveChat or whatever method you prefer. We work hard for you so you can relax.

Refer a friend and get hosting credit!