In this tutorial, I will guide you on How to Install Django on Ubuntu 20.04 LTS. After installing we will create and run our first Django application.

Introduction

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Django is a free and open-source web framework written in Python that adheres to the model template view (MTV) software architectural pattern. According to the Django Software Foundation, the model is the single definitive source of your data, the view describes the data that gets represented to the user via a Python callback function to a specific URL, and the template is how Django generates HTML dynamically.

How to Install Django on Ubuntu 20.04 LTS

Step 1 – Install Python and pip

Python is preinstalled in Linux. You may have to upgrade it. To install/update Python we have to update the local APT repository. Open your terminal window, we’ll input the command below. You can also ignore it.

#For Linux user 
$ sudo apt-get update && sudo apt-get -y upgrade

Django Software Foundation recommended that to use Python 3, we can install Python 3 by using the following command:

$ sudo apt install python3

To verify the installation enter the following command on your command prompt.

$ python3 -V

The output will look similar to this:

Output

Python 3.7.3

Now that we have successfully installed Python 3,we also need to install pip in order to install packages from PyPi, Python’s package repository.

$ sudo apt install -y python3-pip

To verify that pip is successfully installed, run the following command:

$ pip3 -V

The output will look similar to this:

Output

pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

Step 2 — Install virtualenv

virtualenv is a virtual environment where you can install software and Python packages in a contained development space, which isolates the installed software and packages from the rest of your machine’s global environment. This convenient isolation prevents conflicting packages or software from interacting with each other.

To install virtualenv, run the following command.

$ pip3 install virtualenv

Once it is installed, run the following command to verify the installation.

$ virtualenv --version

Output

virtualenv 20.0.31 from /home/pyprogramming/.local/lib/python3.8/site-packages/virtualenv/__init__.py

You have successfully installed virtualenv.

Step 3 — Install Django

First, we have to create a new directory for our project. You can create manually or follow the command.

$ mkdir MyBlog
$ cd MyBlog

MyBlog is a director, you can pick any name you want for your project.

While inside the MyBlog directory, let’s create our virtual environment and call it env.

$ virtualenv env
# To activate the virtual environment
$ . env/bin/activate

You will know it’s activated once the prefix is changed to (env), which will look similar to the following depending on what directory you are in.

While in the environment, let’s install the Django package using pip. Installing Django allows us to create and run Django applications.

$ pip install django

Once installed, verify your Django installation.

Output

3.0.9

With Django installed, we can move on to creating a test project to make sure everything is working correctly.

Step 4 — Creating a Django Test Project

To test the Django installation, we will be creating a (blog) web application.

While in the MyBlog directory with active env, run the following command:

$ django-admin startproject MyBlog

Note: Running the django-admin startproject (project name) {in my case its MyBlog} command will name both project directory and project package the (project name) and create the project in the directory in which the command was run. If the optional (destination) parameter is provided, Django will use the provided destination directory as the project directory and create manage.py and the project package within it.

mr.wixXsid

Now we can look to see what project files were just created. Navigate to the testsite (MyBlog) directory then list the contents of that directory to see what files were created:

$ cd MyBlog
$ ls

Output

manage.py  MyBlog

You will notice output that shows this directory contains a file named manage.py and a folder named MyBlog (testsite). The manage.py file is similar to django-admin and puts the project’s package on sys.path. This also sets the DJANGO_SETTINGS_MODULE environment variable to point to your project’s settings.py file.

Now navigate to the (testsite) MyBlog directory to view the other files that were created:

$ cd MyBlog
# to see the file list
$ ls

Output

Now navigate to the testsite directory to view the other files that were created.

Now we’ll need to add our server IP address to the list of ALLOWED_HOSTS in the settings.py file. Open settings.py with your favorite text editor like VScode. located in ~/MyBlog/Myblog/MyBlog/.

Add your server’s IP address inside the square brackets within single or double quotes. I am using localhost so my IP address is 127.0.0.1

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '@%46-rdvjoh2=)@10$yimeo10m(dn&w^*9n*rcv5fr)!1nbh5c'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Edit the line below with your server IP address
ALLOWED_HOSTS = ['your-server-ip']
#ALLOWED_HOSTS = ['127.0.0.1']


# Application definition

With this completed, be sure to navigate back to the directory where manage.py is located:

$ cd ~/django-apps/testsite/

Now, run the following command

$ python manage.py runserver

For first time it will ask you to migrate.

$ python manage.py migrate

Then run the server again with python manage.py runserver

Your app address will be (http://your-server-ip:8000/) . In my case its 127.0.0.1:8000

This is to confirm that Django is properly installed and our test project is working correctly. When you are done testing your app, you can press CTRL + C to stop the runserver command. This will return you to your programming environment.

When you are ready to leave your Python environment, you can run the deactivate command:

$ deactivate

Deactivating your programming environment will put you back to the terminal command prompt.

Conclusion :

In this tutorial, you have successfully upgraded to the latest version of Python 3. You’ve also installed pip 3, virtualenv, and Django. Now you have the tools needed to get started building Django web applications.

Congratulations! Now you have successfully installed Django. Thanks for using this tutorial to Install Django on Ubuntu 20.04 LTS system. For more interesting articles like this please subscribe to our newsletter.

If you face any difficulties/errors during the process, feel free to comment we will try our best to solve your issue. Thank You

Share.

I am a Full-Stack Web Developer & Security Analyst from Bangladesh. I have built web/online applications on various Open Source Stacks and love information security testing.

Leave A Reply

Exit mobile version