How to install Python Django on Ubuntu 20.04

Django is a Python-based free and open-source web framework that allows model-template-view architectural patterns. Django encourages rapid development and clean and pragmatic codes. It takes care of much of the hassle of web development so that you can focus on the code without reinventing the wheel.

Why Django?

  • Django is exceedingly fast as the developers was focusing on quick way to release the project as fast as possible.
  • Django is scalable. Building apps in Django makes it easy to scale as the traffic increases saving you on the hassle of adding additional server resources to accommodate the surge in traffic.
  • Django takes care of security seriously. It has different ways of authentication which makes it hard for intruders to penetrate.

Prerequisites

  • Install Python 3
  • Have a working Virtual environment
  • Have Pip installed
  • Install psycopg2 if you are using PostgreSQL
  • Install DB API driver like msqlclient if you are planning on MariaDB
  • Database such as Postgresql, MariaDB, SQLite if planning on large scale production code.
  • Basic programming skills

Related Articles

Installing Python Django on Ubuntu 20.04

1. Run system updates

The first thing to always do while installing any program is for you to update and do an upgrade of your system in order to make all the repositories up to date. This will ensure you eliminate running into unwarranted errors during your installation. So you can begin by using the following command;

$ sudo apt update && apt upgrade -y

When updates and upgrades are complete then begin by installing Python 3 into your system.

2. Install Python 3 on Ubuntu 20.04

Django 2 doesn’t support Python 2 anymore that is why we need to install python 3 in our system. So let’s install the version of Python we require with the following command;

$ sudo apt install python3

After the installation is over check the version of installed Python with the following command;

$ Python3 --version
Python 3.8.10

3. Installing Django virtual environment

We are going to create a virtual environment for our installation.

It is important to create a virtual environment because each project you create often comes with its installed libraries, so it is important to isolate each so that dependencies so that it would not clash at any time. To install a virtual environment run the following command;

# create a directory
$ sudo mkdir nextgentips
$ cd nextgentips

Now we can create a virtual environment. I will call it nextgen.

$ python3 -m venv nextgen

If you are running into this errors ensure you installed ensurepip first before coming here.

# output
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt install python3.8-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/root/nextgentips/nextgen/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

To install ensurepip run this command on your terminal.

$ sudo apt install python3.8-venv -y

After installation is over, move ahead and run python3 -m venv nexgen again.

To ensure that the virtual environment was created successfully, activate the environment first using the following command;

source nextgen/bin/activate

You will see the following output, which shows that you have successfully installed and activated your virtual environment.

(nextgen) root@ubuntu:~/nextgentips# 

Now it is time to install Django.

4. Install Django on Ubuntu 20.04

Having completed all the prerequisites we can now install Django using pip.

I am going to show the effective way to install Django. We will be creating a requirements.txt file that will have the Django version to install, psycopg2 for the PostgreSQL database. Everything you need to install with Django must be written inside the requiremts.txt file.

First, let’s ensure we are using the latest version of pip. use the following command to upgrade pip.

$ python -m pip install upgrade pip

You can check the version of pip

$ pip --version
pip 20.0.2 from /root/nextgentips/nextgen/lib/python3.8/site-packages/pip (python 3.8)

Let’s now create the requirements.txt file using your preferred editor.

$ touch requirements.txt

Add the following inside requirements.txt file using either vi or nano

# add django
djanog~=4.0.1

Lastly, run the following command to install Django.

$ pip install -r requirements.txt

You will get the following output.

#output
Collecting django~=4.0.1
  Using cached Django-4.0.1-py3-none-any.whl (8.0 MB)
Collecting asgiref<4,>=3.4.1
  Downloading asgiref-3.4.1-py3-none-any.whl (25 kB)
Collecting backports.zoneinfo; python_version < "3.9"
  Downloading backports.zoneinfo-0.2.1-cp38-cp38-manylinux1_x86_64.whl (74 kB)
     |████████████████████████████████| 74 kB 3.7 MB/s 
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB)
     |████████████████████████████████| 42 kB 2.4 MB/s 
Installing collected packages: asgiref, backports.zoneinfo, sqlparse, django
Successfully installed asgiref-3.4.1 backports.zoneinfo-0.2.1 django-4.0.1 sqlparse-0.4.2

That’s all with the installation with Django.

5. Starting a Django project.

Let’s have a look at one example of how to start a Django project on your virtual environment using the following command.

$ django-admin startproject nextgentips .

The period at the end is very important as it tells Django that the project is created at the current directory.

There are some settings you need to change before moving forward. On nextgentips/settings.py make the following changes.

NB: The best way to edit all these commands is by using a text editor such as VScode, Pycharm, etc.

$ cd nextgentips
$ ls
__init__.py  asgi.py  settings.py  urls.py  wsgi.py

Modify settings.py and add the following

$ nano settings.py
# Timezone 
TIME_ZONE = "Africa/Nairobi"
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'

Save and exit then run migrations to validate and update database.

Python manage.py migrate

NB: Make sure you are running the above command inside a folder that has manage.py

You will get the following output.

Output
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

To run the server use the following command.

$ python manage.py runserver

If you get the following output then you are good to continue.

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

System check identified no issues (0 silenced).
January 22, 2022 - 14:58:05
Django version 4.0.1, using settings 'nextgentips.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Go ahead and open it on your browser

$ http://127.0.0.1:8000/

Conclusion

Congratulations you have installed and set up the Django environment. Happy coding!

About Kipkoech Sang

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Kipkoech Sang →

Leave a Reply

Your email address will not be published.