How to Install the Django Web Framework on a Ubuntu 22.04 VPS

Brief description

When building a website, similar components are required, and you do not have to reinvent the wheel every time you build a new website. Django is the tool you require for this.

In this article, various methods of installing Django on Ubuntu 22.04 LTS are covered.

Special note: consult HostAdvice’s Best Django Hosting Services and Best Linux hosting services pages to find the leading web hosts in each category, including expert and user reviews.

Overview

Django is a free open source program written in python. It allows the use of less code and eases the creation of complex websites. It helps developers to avoid some common security mistakes such as SQL Injection. Due to its speed, scalability, and security, it has quickly risen to be one of the best tools in its field. So that being said, we are going to install Django on an Ubuntu using a few methods.

We can install Django by:

  • Install from packages – Ubuntu repositories contain Django packages. By using the apt package manager, we can install them easily. The main disadvantage is that the version of Django included in the repositories may lag as compared to the official versions available from the Django website.
  • Install through pip – By installing pip, you can install Django for use by any user on the system. The pip should always contain the latest stable Django. The disadvantage is that it is less flexible.
  • Install through pip in a virtualenv – The virtualenv package allows you to create virtual environments. By using this method, you can install Django without affecting the entire system.

Git install– This method is suitable if you wish to install the latest development version. You usually have to go to the git repo to get the latest features.

Prerequisites

  • Ubuntu 22.04 server
  • Root privileges/ sudo privileges

Installing Django Installing from packages

Update your local packages with the apt command:

$ sudo apt-get update

 

screenshot of Install the Django Web Framework on a Ubuntu

Check the installed Python version by using this command:

$ python3 -V

screenshot of Install the Django Web Framework on a Ubuntu

Select which version of python you would like to use in the installation, for Python 2;

$ sudo apt-get install python-django

For python 3;

$ sudo apt-get install python3-django

That is it; it will take time to install all required packages on your system.

screenshot of Install the Django Web Framework on a Ubuntu

After completing the dijango installation, you can check whether it has been installed successfully or not by using the following command:

$ django-admin --version

If successful, the following result is displayed;

screenshot of Install the Django Web Framework on a Ubuntu

Installing through pip

The previous method does not install the latest version of Django, but by using pip, you get the newest version.

First, refresh your apt package index.

$ sudo apt-get update

Then install pip. For the python 2 users;

$ sudo apt-get install python-pip

For python 3 users;

$ sudo apt-get install python3-pip

screenshot of Install the Django Web Framework on a Ubuntu

Next, install Django. For python 2 users;

$ sudo pip install django

For python 3 use the pip3 command;

$ sudo pip3 install django

screenshot of Install the Django Web Framework on a Ubuntu

Django is installed. To verify it type:

$ django-admin --version

The result will be:

screenshot of Install the Django Web Framework on a Ubuntu

Installing through pip in a virtual environment

Just like the rest of the installations, we start by refreshing our local package index.

$ sudo apt-get update

Then we install pip. For python 2;

$ sudo apt-get install python-pip

For python 3;

$ sudo apt-get install python3-pip

Next, we use the virtualenv package, for Python 2;

$ sudo pip install virtualenv

For python 3;

$ sudo pip3 install virtualenv

screenshot of Install the Django Web Framework on a Ubuntu

Once that is done, you can now start a new project in Django. Remember that whenever you start a new project, start by creating and moving into a new project directory.

$ mkdir ~/newhostA

$ cd ~/newhostA

Create a virtual environment inside that project by typing;

$ virtualenv testhostA

screenshot of Install the Django Web Framework on a Ubuntu

Note: testhostA is just what we choose to call our environment, you can name it however you want.

This will install a version of python and pip into a directory within the project directory. Now to install packages inside the virtual environment you must activate it, so type the following command:

$ source testhostA/bin/activate

screenshot of Install the Django Web Framework on a Ubuntu

Your command line changes to show that you are now operating within the isolated environment.

(testhostA)username@hostname:~/newhostA$

Next, install Django by running the following code regardless of the python version you are running.

(testhostA)

$ pip3 install django

Finally, verify your installation by running;

(testhostA)

$ django-admin –version

If successful, your output should be:

screenshot of Install the Django Web Framework on a Ubuntu

To exit your virtual environment just type;

(testhostA)

$ deactivate

screenshot of Install the Django Web Framework on a Ubuntu

When you wish to resume work on your project, reactivate your virtual environment.

$ cd ~/newhostA

$ source testhostA/bin/activate

screenshot of Install the Django Web Framework on a Ubuntu

Git install

This method installs the development version of Django you have to download and install Django from its git repository.

Start by refreshing the package index;

$ sudo apt-get update

Next, install git. For python 2;

$ sudo apt-get install git python-pip

For python 3;

$ sudo apt-get install git python3-pip

screenshot of Install the Django Web Framework on a Ubuntu

Then you can clone the Django repository;

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

screenshot of Install the Django Web Framework on a Ubuntu

Now install the cloned repository using pip. We are using the –e option to install in editable mode.

For python 2;

$ sudo pip install -e ~/django-dev

For python 3;

$ sudo pip3 install -e ~/django-dev

After running the above command, the following lines should display on the terminal:

screenshot of Install the Django Web Framework on a Ubuntu

Verify the installation;

$ django-admin --version

The output should be;

screenshot of Install the Django Web Framework on a Ubuntu

Conclusion

Now that we have gone through all possible installation techniques, it is up to you to choose the one that’s most suitable for your needs. From getting the latest beta features to getting the most recent stable releases, Django has got your back. We hope you enjoy installing it as we did.

Was this article helpful?