How to Install Django on Ubuntu 22.04 LTS

Hello, world! Welcome to “TechNHIT ”  In this tutorial, we are going to install Django on Ubuntu 22.04 LTS server. 

Let’s Know about Django

Django is a full-featured Python web framework for developing dynamic websites and applications. Using Django, you can quickly create Python web applications.

In this guide, you will get Django up and running on an Ubuntu 22.04 server. After installation, you will start a new project to use as the basis for your site.

So, there are different ways to install Django, depending upon your needs and how you want to configure your development environment.

Here we are going to install Django in a self-contained environment using tools like venv and virtualenv. A virtual environment allows you to install Django in a project directory without affecting the larger system. You can therefore select Python packages on a per-project basis, regardless of conflicts with other projects’ requirements. This is typically the most practical and recommended approach to working with Django.

Prerequisites

Before you begin, you should have a non-root user with sudo privileges available on your Ubuntu 22.04 server.

Let’s begin by refreshing the local package index: let’s open a terminal and type

sudo apt update

Before instal Django, let’s check for the python installed in your system. Actually, ubuntu Jammy Jellyfish by default comes with python version 3.10. 

python3 -V

Next, let’s install pip and venv from the Ubuntu repositories: use the following command.

sudo apt install python3-pip python3-venv

Now, whenever you start a new project, you can create a virtual environment for it. Start by creating and moving into my new project directory: My Django Project.

mkdir ~/myDjangoproject
cd ~/myDjangoproject

Next, create a virtual environment within the project directory using the python command compatible with your Python version.

python3 -m venv my_env

We will call our virtual environment is my_env, you can name it something descriptive related to your project:

This will install standalone versions of Python and pip into an isolated directory structure within your project directory. A directory will be created with the name you select, which will hold the file hierarchy where your packages will be installed.

To install packages into the isolated environment, you must activate it by typing: 

source my_env/bin/activate

Your prompt should change to reflect that you are now in your virtual environment.

Note: To leave your virtual environment, you need to issue the deactivate command from anywhere on the system.

deactivate

Now it’s time to install Django. Type command pip install Django.

pip install django

Let’s check my Django admin version so type:

django-admin --version

To build your project, you can use Django-admin with the star project command. We will call our project djangoproject, but you can replace this with a different name. 

django-admin startproject djangoproject .

To avoid having too many nested directories, let’s tell Django to place the management script and inner directory in the current directory using the ending dot.

startproject will create a directory within your current working directory that includes: A management script manage dot py. And A directory that includes the actual project code.

Now its time to migrate the database. let’s use the migrate command with manage.py. 

python manage.py migrate

Finally, create an administrative user so that you can use the Djano admin interface. Let’s do this with the create super user command:

python manage.py createsuperuser

This will prompt for user name, if you left it blank then it will grab the ubuntu default username as you can see here my username is techblog. Lets set my user name admin.

Putting an email and password for my admin account.

Once you have a user, you can start up the Django development server using forllowng command:

python manage.py runserver localhost:8000
or
python manage.py runserver 0.0.0.0:8000

Now open up a web browser and here we go it working. And here you see the log reflection while accessing the server.

http://your_server_ip:8000

Now open admin dashboard using slash admin followed by our ip or localhost. Login using your credential and here is your admin panel.

http://your_server_ip:8000/admin

So, guys finally we deployed Django in Ubuntu 22.04 LTS server, hope this article is helpful to you Thank you.

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.
subroto
  • subroto
  • I like Programming and New Technologies. And work with Linux.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.