Linux

How to Install Django on Ubuntu 20.04 LTS

How to Install Django on Ubuntu 20.04 LTS

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

Django is managed by the Django Software Foundation and is used by many large technology companies, governments, and other organizations. Some big websites like Pinterest, Mozilla, Instagram, Discuss, The Washington Post, etc. 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 in Python 3 (as the default version on Ubuntu 20.04), and we will show you 4 different ways to install Django Framework, install it through the Apt repository, pip, and use Python Virtualenv.

What we will do?

  • Python and Pip settings
  • Install the Django Framework
  • Create your first project with Django

Step 1 – Setup Python and Pip

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

By default, Ubuntu 20.04 comes with Python 3 as the default python version. this is available as a ‘python3’ command, not ‘python’.

Check the python command as below.

python

You will get the ‘python’ command not found.

Command 'python' not found, did you mean:

  command 'python3' from deb python3
  command 'python' from deb python-is-python3

To resolve this, create a symbolic link from the Python3 binary ‘/ usr / bin / python3’ to ‘/ usr / bin / python’ as the 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

Python3 configuration has finished.

1

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

Install python3-pip using the apt command below.

sudo apt install python3-pip -y

After all installation is complete, make the command ‘pip3’ as the default ‘pip’ version.

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

After that, check the pip command below.

pip --version

Below are the results that 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.

2

Step 2 – Install the Django Framework

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

Install Django with Apt

By default, the Ubuntu 20.04 repository provides a package for the Django Framework named ‘python3-django’. At the moment (writing tutorial), 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

After all installation is complete, check the Django version using the following.

django-admin --version

You will get the following results.

3

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

Install Django with PIP

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

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

Install Django framework 3.0 using the pip command below.

pip install django==3.0.0

After all installation is complete, check the Django version using the following command.

django-admin --version

And you will get Django version 3.0 as the answer.

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 a Django 3.0 response as below.

4

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

Install Django with Virtualenv

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

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

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

pip install virtualenv

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

virtualenv myenv

Now, enter the ‘myenv’ directory and activate the virtual environment using the commands 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 Django Python Framework is now installed in a Python virtual environment. Check the Django version using the following command.

django-admin --version

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

To return to a normal user and disable the virtual environment, run the command.

deactivate

Now you will return to the normal user environment of your system.

5

Install Django from Git Repository

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

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

apt install git

Next, create a new virtual environment called ‘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

After all installation is complete, check the Django version.

django-admin --version

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

6

Now you have learned how to install Python Django using a number of ways.

Step 3 – Create Your First Project with Django Python

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

Create a ‘myproject’ new virtual environment.

virtualenv myproject

Open the ‘myproject’ directory and activate the virtual environment.

cd myproject/
source bin/activate

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

pip install django==3.0.0

Now you have installed the Django Framework.

7

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

django-admin startproject mysite

Now you will get a new directory called ‘mysite’ which contains all the Djaango configurations.

Open the ‘my site’ directory and check all configuration files.

cd mysite/; tree

Below are the results that you will get.

8

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

vim mysite/settings.py

In the ‘ALLOWED_HOSTS’ configuration, enter 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 uses a 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 password, then press enter.

9

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

Step 4 – Start the Django Python Project

After migrating the Django project database, 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 active on port ‘8000’.

10

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

http://10.5.5.32:8000/

And you will get the default Django main page.

11

Next, add the ‘admin’ path to 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.

12

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

13

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

Related posts

How to use the chmod command on Linux

Howto

How to Add SWAP Space on Ubuntu 20.04

Linux

How to Install Ruby On Rails on Ubuntu 20.04

Linux

How to Install Tomcat 9 on Ubuntu 20.04

Linux

How to install Webmin and secure it with Let’s Encrypt SSL on Ubuntu 20.04 LTS

Linux

What is Fedora Linux?

UBERHOWTO

How to make Ubuntu look like Windows XP

Windows

How to Upgrade From Windows 7 to Linux

Howto

How to restart the network on Ubuntu

Linux