How ToLinuxProgrammingTech/Web

How To Install Django on Ubuntu

Introduction

Django is a powerful, open-source web framework based on Python, widely used for building scalable web applications. This guide will walk you through the steps to install Django on Ubuntu.

Prerequisites

Before starting, ensure you have the following:

  • An Ubuntu system (20.04 or later recommended)
  • A user account with sudo privileges
  • Python 3 installed (Ubuntu 20.04+ ships with Python 3 by default)

Step 1: Update System Packages

It’s essential to keep your system packages updated. Run the following command:

sudo apt update && sudo apt upgrade -y

Step 2: Install Python and Pip

Install Python 3 and Pip (Python’s package manager):

sudo apt install python3 python3-pip -y

Verify installation:

python3 --version
pip3 --version

Step 3: Install Virtual Environment

Using a virtual environment ensures Django and its dependencies are isolated. Install the venv module:

sudo apt install python3-venv -y

Create a virtual environment:

python3 -m venv my_django_env

Activate the virtual environment:

source my_django_env/bin/activate

Your terminal should now display (my_django_env) indicating the environment is active.

Step 4: Install Django

Inside the virtual environment, install Django using Pip:

pip install django

Verify the installation:

django-admin --version

Step 5: Create a New Django Project

Run the following command to create a new Django project:

django-admin startproject myproject

Change into the project directory:

cd myproject

Step 6: Run the Development Server

To test your Django installation, run the development server:

python manage.py runserver

By default, the server will run on http://127.0.0.1:8000/. Open this URL in your browser to see the Django welcome page.

Step 7: Configure Firewall (If Required)

If you’re using UFW (Uncomplicated Firewall), allow traffic on port 8000:

sudo ufw allow 8000

Step 8: Create a Django App (Optional but Recommended)

To create a new app inside your Django project:

python manage.py startapp myapp

Add your app to the INSTALLED_APPS list in settings.py to enable it.

Step 9: Apply Migrations

Migrations ensure your database schema is updated. Run:

python manage.py migrate

Step 10: Create a Superuser

Create an admin user to access the Django admin panel:

python manage.py createsuperuser

Follow the prompts to set the username, email, and password.

Step 11: Access the Admin Interface

Run the development server again:

python manage.py runserver

Visit http://127.0.0.1:8000/admin and log in with your superuser credentials.

Step 12: Additional Configuration (Optional)

  • Database Setup: For production use, consider configuring a robust database like PostgreSQL or MySQL.
  • Static Files Management: Manage and serve static files efficiently in deployment.
  • Security Enhancements: Use HTTPS, set proper security headers, and configure firewall rules for enhanced security.

Conclusion

Congratulations! You have successfully installed Django on Ubuntu. From here, you can start building web applications, adding new apps, and expanding your project. If you have further questions, consult the Django documentation.


You may like also:

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

2 thoughts on “How To Install Django on Ubuntu

Leave a Reply

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