How to install Django on Ubuntu 22.04

Estimated reading: 4 minutes 790 views

Introduction

Django is a Python-based free and open-source web framework, which follows the model-template-view architectural pattern. It is maintained by the Django Software Foundation, an independent organization established as a 501 non-profit. Django’s primary goal is to ease the creation of complex, database-driven websites. In this article, we will show you the steps to Install Django on Ubuntu 22.04

Prerequisite

  • Sudo access to your Ubuntu 22.04

Step 1 – Installing Python and Pip

If you have launched a new server. Then we recommend updating the server with the below command

apt-get update

By default, Python 3 is installed on your VPS, but if your system doesn’t have Python installed. Execute the below commands to install it

apt-get install python3

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

sudo apt install python3-pip python3-venv

Step 2: Installing Django

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

Step 3: Creating a Super User for Django Application

We will now be creating a superuser account for the administration of the Django application. Run the following command from your Django application directory.

python3 manage.py createsuperuser

Step 5: Run the Django Application

Your Django application is ready to use. By default, Django doesn’t allow external hosts to access the web interface. To allow external hosts, edit settings.py file and add IP under ALLOWED_HOSTS.

nano ~/myDjangoproject/djangoproject/settings.py

Add the IP

ALLOWED_HOSTS = ['your_server_IP']

Finally, run the Django application server with below command

python manage.py runserver your_server_ip:8000

Django application server is running now. Open your web browser and access the Django Application with your server IP on port 8000. This will show you the default Django web page. Before accessing the Django application on browser,You need to open the port on the server to access it externally

To access the admin interface, add /admin/ to the end of your URL and it will take you to the admin login page.

Conclusion

You have now installed Django application on your server. After accessing and login to browser you can add more users and groups for your application and proceed with your setup.


Leave a Comment

Share this Doc

How to install Django on Ubuntu 22.04

Or copy link

CONTENTS