Django is a free and open-source, Python-based web framework that allows developers to quickly and efficiently create feature-rich, scalable, and secure web applications. Django web framework is built by experienced developers and used, tested, and enhanced for more than a decade by a very active community. It is used by some of the largest websites and online communities around the world, which makes it one of the most popular and widely used full-stack Python web frameworks.
In this tutorial we will guide you through the steps of installing Django on an Ubuntu 20.04 VPS.
We can install Django on Ubuntu 20.04 using few different methods:
- From the official Ubuntu 20.04 repository
- Install the latest development version from Git
- Install through pip in a virtual environment
- Ubuntu 20.04 VPS or Cloud Dedicated Server. In this tutorial we will use a VPS with the SSD 2 VPS hosting plan.
- System user with sudo privileges.
Install Django from repository
We can install Django from the official Ubuntu 20.04 repository maintained by Canonical. This is pretty easy and straightforward method of installing Django, as all dependencies will be automatically installed globally for all users on the server. It is worth mentioning that we can install a single version by the repository and often it is not the latest release of the framework.
In order to install Django on your Ubuntu 20.04, make sure that the system is up to date. Run the following commands to update the package index and install all available upgrades for the packages installed on your VPS:
apt update && apt upgrade
As we already mentioned, Django is Pythhon-based web framework and we need to have Python installed on the VPS. Ubuntu 20.04 comes with Python 3.8 by default. Run the following command to check what version of Python we’ve installed:
python -V<br>Python 3.8.5
Once we confirm that Python 3.8 is installed, we can proceed and install Django. We can easily done it by running this command:
apt -y install python3-django
After the installation of all packages i done, verify the Django installation:
We have successfully installed Django version 2.2.12 , and we can start working on our first Django project.
Install Django from Git Repository
The next method we will explain is how to install Django from their Git repository. The Git repository of the Django project contains the latest development version of Django, so if you need all latest features, improvements and bug fixes you should use this method.
First, install Git from the Ubuntu repositories:
apt -y install git
We have to install Django inside Python virtual environment, so let’s install the necessary Python packages:
apt install python3-pip python3-venv
Clone the Django repository from their Github account to the ‘/opt/django’ directory on your server:
git clone git://github.com/django/django /opt/django
Change the working directory and create Python virtual environment inside it:
cd /opt/django/ && python3 -m venv django_venv
Activate the virtual environment:
Install the development version of Django using the package management system – pip:
(django_venv) # pip install -e ../django/
If the installation is successful you should get the following output:
Successfully installed Django asgiref-3.4.0 pytz-2021.1 sqlparse-0.4.1
Check the installed version of Django:
(django_venv) # django-admin --version<br>4.0.dev20210630080855
As you can notice, the version installed from Git repository is much newer than the one installed from the official Ubuntu repository.
Deactivate the Python virtual environment:
(django_venv) # deactivate
Install Django with pip in a virtual environment
When we install Python some module, including Django it is always recommended to install it inside a virtual environment, because it will not affect the other packages and modules installed on the server. You can have different virtual environments, with separate modules and version for every Python project on your server.
First of all, check if we have Python installed on the server:
python -V<br>Python 3.8.5
Install the necessary packages for creating the virtual environment:
apt -y install python3-pip python3-virtualenv
Create new Python virtual environment for Django:
Activate the newly created virtual environment:
cd /opt/django_environment && source bin/activate
Install Django with pip using the following command:
(django_environment) # pip install django
One of the advantages of installing Django with pip, you can specify what version of Django you will install. For example, if we need Django version 3.2.4 we can install it with the following command:
(django_environment) # pip install django==3.2.4
Check the installed version:
(django_environment) # django-admin --version<br>3.2.4
Deactivate the virtual environment:
(django_venv) # deactivate
Of course, you don’t have to Install Django on Ubuntu 20.04 VPS if you use one of our Django VPS Hosting plans, in which case you can simply ask our expert Linux admins to install Django on your Ubuntu VPS for you. They are available 24/7 and will take care of your request immediately.
PS. If you liked this post on how to install Django on Ubuntu 20.04 VPS, please share it with your friends on the social networks or simply leave a reply in the comments sections. Thanks.