How to Install Django on Ubuntu 20.04 (Step by Step Tutorial)

How to Install Django on Ubuntu 20.04.  Don’t you think having a flexible framework will make your task easier while developing the applications? Of course yes. Using a flexible framework will help you to automate the implementation, reduce task complexity, a time consuming, and most important complete focus on application logic. In that case, today we are going to explain one of the popular Python frameworks that are known as “DJANGO” and also known as model-template-view. Let’s know fundamental concepts that come under the Django framework.

What is Django?

Django vs Laravel

Django is a python based full stack framework. This framework works based on the model-template-view architectural patterns. Django is considered to be one of the popular web based development frameworks for developing Python’s server applications. Now it is listed as one of the 10 web based development python frameworks of 2020. Django works based on Don’t Repeat Yourself principles. It is a free to use and open source python Full stack framework. It is the most widely used high level framework that enables developers to develop secure and maintainable websites. The main purpose to use Django is to avoid any commonly occurring security mistakes and also protect the websites automatically.

Django server has been increasing its popularity with the help of mechanisms like counting the number of Github projects and stack overflow questions for each tool or platform. The high profile websites that use Django are Disqus, Instagram, MacArthur Foundation, National Geographic channel, Knight Foundation, Pinterest, Open knowledge foundation, and open stack software. In recent times Django is a thriving and collaborative open source project that has been used by contributors and developers.

Features of Django

In the intro section only we have explained why the Django framework is a popular and widely used platform among others. Now it’s time to know about the salient features of Django:

1. It is a popular python web framework: as we already know that Django is built using python language. Python is robust in nature with the features like easy to learn and open source language. Especially this feature makes Django a great choice for web development.

2. High scalability: Scalability grows as the application requirement increases. Many top companies prefer to have a Django framework in the web development they are; Instagram, YouTube, Pinterest, Spotify, Quora, and Stack overflow. The number of Django application users has been increasing day by day and it is a complete full stack application development framework.

3. Exceptionally secure application: Django is a highly secured framework because it successfully covers the loopholes of web developments, helps developers to avoid any mistakes, and is also a secure way to manage accounts.

4. Thoroughly tested: Django is a durable framework and powerful enough to withstand the dynamic modifications that any application development requires. Moreover, the Django community supports fully tested features that make Django a bug free framework.

5. Fast development: one plus point of using Django is that it is a combination of both frontend and backend applications that offers a complete package of functional websites. While working with Django the developers no need to separate server files to design the database and connect with different database servers. So we dont need the extra files to handle plenty of tasks.

6. Active community support: Django online community and experts help the developers to reduce the web application development cost and complexity. Most of the Django communities have been working non stop to help developers to resolve any issues.

7. Support model-view-template architectural patterns: Django not only supports large size enterprises but also supports mid level and small size enterprises because of the MVT (model view pattern) architecture pattern. The MVT pattern in Django also helps developers to build complex websites keeping business logic and user interface in mind.

In this post, we will show you how to install Django on Ubuntu 20.04

Set Up Python and Pip

By default, Python 3 comes pre installed on Ubuntu 20.04. However, it is available as a python3, not python. So you will need to create a symbolic link of the Python3 binary from /usr/bin/python3 to /usr/bin/python. You can create it by running the following command:

				
					update-alternatives --install /usr/bin/python python /usr/bin/python3 1
				
			

Next, verify the python command using the following command:

				
					python --version
				
			

You will get the Python version in the following output:

				
					Python 3.8.10
				
			

Next, you will also need to install Pip to your system. You can install it by running the following command:

				
					apt-get install python3-pip -y
				
			

Next, create a symbolic link of the pip3 binary from /usr/bin/pip3 to /usr/bin/pip.

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

Next, verify the pip command using the following command:

				
					pip --version
				
			

You will get the Pip version in the following output:

				
					pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
				
			

Install Django on Ubuntu 20.04

There are several ways to install the Python Django Framework on Ubuntu 20.04 server. In this section, we will explain each method to install Django on Ubuntu 20.04.

Install Django Using APT

By default, Django version 2.2.12 is available in the Ubuntu 20.04 default repository. You can install it by just running the following command:

				
					apt-get install python3-django -y
				
			

After installing the Django Framework, verify the Django version with the following command:

				
					django-admin --version
				
			

You will get the following output:

				
					2.2.12
				
			

Install Django Using PIP

You can also use the PIP command to install Django via the PyPI repository. This method allows you to install the specific version of Django for your project.

To install the Django version 3.0.0, run the following command:

				
					pip install django==3.0.0
				
			

After the successful installation, verify the Django version with the following command:

				
					django-admin --version
				
			

You will get the following output:

				
					3.0
				
			

Install Django Using Virtualenv

Python Virtualenv allows you to create isolated python environments for your project. It is very useful because it allows developers to run and develop an application with different python versions.

First, install the Python virtualenv package using the following command:

				
					pip install virtualenv
				
			

After the installation, create a new Python virtual environment named venv using the following command:

				
					virtualenv venv
				
			

Next, change the directory to venv and activate the virtual environment with the following command:

				
					cd venv/
source bin/activate
				
			

Next, install the Django version 3.0.0 using the following command:

				
					pip install django==3.0.0
				
			

You will get the following output:

				
					Collecting django==3.0.0
Using cached Django-3.0-py3-none-any.whl (7.4 MB)
Collecting asgiref~=3.2
Using cached asgiref-3.5.0-py3-none-any.whl (22 kB)
Collecting pytz
Downloading pytz-2021.3-py2.py3-none-any.whl (503 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 503.5/503.5 KB 19.3 MB/s eta 0:00:00
Collecting sqlparse>=0.2.2
Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.3/42.3 KB 11.3 MB/s eta 0:00:00
Installing collected packages: pytz, sqlparse, asgiref, django
Successfully installed asgiref-3.5.0 django-3.0 pytz-2021.3 sqlparse-0.4.2

				
			

Next, verify the Django version with the following command:

				
					django-admin --version
				
			

You will get the following output:

				
					3.0
				
			

Next, deactivate from the virtual environment using the following command:

				
					deactivate
				
			

Install Django Using Git Repository

Using this method, you can install the latest development version of Django to your system.

First, install the Git package with the following command:

				
					apt-get install git -y
				
			

After the Git installation, create a Python virtual environment with the following command:

				
					virtualenv django
				
			

Next, change the directory to the Django virtual environment and activate it using the following command:

				
					cd django
source bin/activate
				
			

Next, download the Django development version using the following command:

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

Next, install the downloaded Django using the following command:

				
					pip install -e django-dev
				
			

Once Django is installed, verify the Django version with the following command:

				
					django-admin --version
				
			

You will get the following output:

				
					4.1.dev20220218123749
				
			

Create a Django Project

In this section, we will show you how to create and run a Django project. First, create a new virtual environment named project with the following command:

				
					virtualenv project
				
			

Next, change the directory to the project and activate it with the following command:

				
					cd project
source bin/activate
				
			

Next, install the Django version 3.0.0 using the following command:

				
					pip install django==3.0.0
				
			

Next, create a new Django project named djangosite with the following command:

				
					django-admin startproject djangosite
				
			

Next, change the directory to djangosite and edit the settings.py file:

				
					cd djangosite
nano djangosite/settings.py
				
			

Add your server ip address in the ALLOWED_HOSTS:

				
					ALLOWED_HOSTS = ["your-server-ip"]
				
			

Save and close the file then migrate the Django database with the following command:

				
					python manage.py migrate
				
			

Next, create a superuser to log into the Django admin interface:

				
					python manage.py createsuperuser
				
			

Provide your admin username, password, and email as shown below:

				
					Username (leave blank to use 'root'): admin
Email address: admin@example.com
Password:
Password (again):
Superuser created successfully.

				
			

Next, start the Django project with the following command:

				
					python manage.py runserver 0.0.0.0:8080
				
			

You will get the following output:

				
					Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
February 19, 2022 - 04:42:42
Django version 3.0, using settings 'djangosite.settings'
Starting development server at http://0.0.0.0:8080/
Quit the server with CONTROL-C.

				
			

Access Django Home Page

Now, open your web browser and access the Django home page using the URL http://your-server-ip. You should see the Django home page in the following screen:

To access the Django admin interface, type the URL http://your-server-ip/admin. You will be redirected to the Django login page:

django login page

Provide your admin username, password, and click on the Log in button. You should see the Django dashboard on the following screen:

django dashboard

Install Django on Ubuntu 20.04 Conclusion

In this post, we explained four methods to install Django on Ubuntu 20.04. You can now use your preferred method to install Django on Ubuntu 20.04. For more information, visit the Django documentation page.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x