Full Stack Developer
There 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.
By default, the Ubuntu 20.04 repository provides packages for Django Framework named 'python3-django'. At this time (tutorial writing), the Ubuntu repository provides 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.
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.
As a result, Django 2.12 is installed on ubuntu 20.04 through the official Ubuntu repository.
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.
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.
As a result, you've successfully installed the Django 3.0 using the PyPi repository.
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.
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.
Install Python Django from Source
Now you've learned how to install Python Django using multiple ways.