How to Install Django on Ubuntu 24.04
Learn how to install Django on Ubuntu 24.04 LTS. Step-by-step tutorial covering Python virtual environment, Django setup, Gunicorn service, and Nginx proxy.

7 min read
Learn how to install Django on Ubuntu 24.04 LTS. Step-by-step tutorial covering Python virtual environment, Django setup, Gunicorn service, and Nginx proxy.
- Prerequisites and Supported Releases
- Step 1: Updating System Packages and Installing Python 3 Developer Tools
- Step 2: Creating an Isolated Python Virtual Environment
Interactive Builder for this Guide
Build your custom nginx syntax (How to Install Django on Ubuntu 24.04)
server {
listen 80;
listen [::]:80;
server_name app.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name app.example.com;
ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
gzip on;
gzip_types text/plain text/css application/json application/javascript application/xml;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}server_nameBinds this virtual host to app.example.comreturn 301 https://...Redirects all plain HTTP requests permanently to TLS/HTTPSproxy_pass http://127.0.0.1:3000Forwards HTTP + WebSocket requests to local application serverX-Content-Type-Options / X-Frame-OptionsProtects users against MIME sniffing and clickjackingDjango is a high-level, open-source Python web framework that encourages rapid development and clean, pragmatic architectural design. Engineered by experienced software professionals, Django handles much of the boilerplate of web engineering, allowing developers to focus on writing application features without reinventing common patterns like database ORMs, administrative dashboards, authentication systems, and CSRF protection.
Deploying Django on Ubuntu 24.04 LTS provides an exceptionally stable and scalable Linux runtime environment powered by Python 3.12. Learning how to install django on ubuntu 24.04 enables software engineers and system administrators to build, test, and deploy enterprise-grade Python web applications with isolated virtual environments, robust WSGI servers, and secure HTTP reverse proxying.
In this step-by-step tutorial, we demonstrate how to install Django on Ubuntu 24.04 LTS (Noble Numbat). We cover updating system packages, configuring isolated virtual environments adhering to PEP 668, installing Django via pip, initializing a sample project, running database migrations, configuring Gunicorn as a systemd service, and establishing an Nginx reverse proxy with static asset caching.
Install and configure the Django web framework on Ubuntu 24.04 LTS using Python 3.12, isolated virtual environments (PEP 668), Gunicorn WSGI server, and Nginx reverse proxy.
Prerequisites and Supported Releases
This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat). The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.
Before proceeding, ensure you have the following requirements satisfied:
- A server or virtual machine running
Ubuntu 24.04 LTS. - A non-root user account configured with administrative
sudoprivileges. - At least
1 GB of RAM(2 GB recommended for production workloads with PostgreSQL). - Python 3.12 installed on your system (default on Ubuntu 24.04).
Step 1: Updating System Packages and Installing Python 3 Developer Tools
Ubuntu 24.04 LTS ships with Python 3.12 as its default interpreter. To adhere to modern packaging standards (PEP 668), Ubuntu prevents direct system-wide pip package installations that could corrupt the OS package manager.
First, refresh your APT package index and upgrade existing system packages to their latest stable security releases.
Install foundational Python developer packages including python3-pip, python3-venv, python3-dev, build-essential, and libpq-dev for database drivers using APT.
Confirm that Python 3 and its virtual environment module are ready for isolated package deployments.
# Refresh repository lists and upgrade packages
sudo apt update && sudo apt upgrade -y
# Install Python developer dependencies
sudo apt install -y python3-pip python3-venv python3-dev build-essential libpq-dev curl git
# Confirm Python version
python3 --version
Step 2: Creating an Isolated Python Virtual Environment
Creating an isolated virtual environment guarantees that Django and its associated third-party dependencies do not conflict with system-level packages or other applications on the server.
Create a dedicated directory for your Django projects, such as /var/www/myproject or within your user home directory.
Initialize a virtual environment named myprojectenv using the python3 -m venv command.
Activate the virtual environment so that subsequent pip commands operate strictly within this isolated context.
# Create project directory
mkdir -p ~/django_projects/myproject
cd ~/django_projects/myproject
# Create isolated virtual environment
python3 -m venv myprojectenv
# Activate the virtual environment
source myprojectenv/bin/activate
# Verify active environment
which python
Step 3: Installing Django and Gunicorn via pip
With the virtual environment active, use pip to install the latest Long-Term Support (LTS) release of Django alongside Gunicorn, a battle-tested WSGI HTTP server.
Upgrade pip, setuptools, and wheel to ensure binary wheel installations proceed without compilation errors.
Install django and gunicorn inside the activated environment.
Verify the installed Django version using the django-admin utility command.
# Upgrade core packaging tools
pip install --upgrade pip setuptools wheel
# Install Django and Gunicorn WSGI server
pip install django gunicorn
# Verify installed Django version
django-admin --version
Step 4: Initializing a New Django Project and Configuring Settings
Use the django-admin command-line utility to bootstrap a new Django application project structure.
Run django-admin startproject myproject . to generate the configuration files in the current working directory without unnecessary nested subfolders.
Open myproject/settings.py and add your server domain name or public IP address to the ALLOWED_HOSTS list.
Define the STATIC_ROOT setting at the bottom of the file so Django knows where to collect production static assets (CSS, JavaScript, images).
# Bootstrap new Django project in current directory
django-admin startproject myproject .
# Configure ALLOWED_HOSTS in settings.py
sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = ['your_domain_or_ip', 'localhost', '127.0.0.1']/" myproject/settings.py
# Append STATIC_ROOT setting for production assets
echo "STATIC_ROOT = BASE_DIR / 'staticfiles'" >> myproject/settings.py
Step 5: Running Database Migrations and Collecting Static Assets
Django includes an integrated Object-Relational Mapper (ORM) that manages database schemas through automated migrations. By default, Django initializes an SQLite database for immediate development and testing.
Apply initial database migrations using python manage.py migrate to create the core authentication and administrative schema tables.
Create an administrative superuser account using python manage.py createsuperuser to manage your application via the Django admin interface.
Execute python manage.py collectstatic --no-input to compile all admin static files into the designated staticfiles directory.
# Run database migrations
python manage.py migrate
# Create Django administrative superuser
python manage.py createsuperuser
# Collect static assets for production web serving
python manage.py collectstatic --no-input
Step 6: Configuring Systemd Service Daemon for Gunicorn
In a production environment, Gunicorn should execute as a background systemd service daemon rather than an interactive console process. Systemd ensures automatic process restarts after server reboots or unexpected crashes.
Create a systemd unit file at /etc/systemd/system/gunicorn.service.
Configure the service unit to execute under your non-root user, point to the virtual environment binary path, and bind to a local UNIX socket at /run/gunicorn.sock.
Reload systemd, enable the service, and verify that the Gunicorn socket is active and listening.
# Create systemd service unit file
sudo bash -c 'cat <<EOF > /etc/systemd/system/gunicorn.service
[Unit]
Description=Gunicorn daemon for Django project
After=network.target
[Service]
User='$USER'
Group=www-data
WorkingDirectory='$HOME'/django_projects/myproject
ExecStart='$HOME'/django_projects/myproject/myprojectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
myproject.wsgi:application
[Install]
WantedBy=multi-user.target
EOF'
# Reload systemd and start Gunicorn service
sudo systemctl daemon-reload
sudo systemctl enable --now gunicorn
sudo systemctl status gunicorn --no-pager
Step 7: Configuring Nginx Reverse Proxy and Static File Routing
Nginx acts as the public-facing HTTP reverse proxy. It terminates visitor connections, directly serves static assets with high throughput, and forwards dynamic Python application requests to Gunicorn via the local UNIX socket.
Install Nginx using APT if it is not already installed on your server.
Create an Nginx server block configuration in /etc/nginx/sites-available/django.conf.
Configure location directives for /static/ pointing to your collected staticfiles directory, and forward all other traffic to http://unix:/run/gunicorn.sock.
Enable the server block by creating a symlink in /etc/nginx/sites-enabled/, test the Nginx configuration syntax, and reload Nginx.
# Install Nginx
sudo apt install -y nginx
# Create Nginx configuration block
sudo bash -c 'cat <<EOF > /etc/nginx/sites-available/django.conf
server {
listen 80;
server_name your_domain_or_ip;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias '$HOME'/django_projects/myproject/staticfiles/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
EOF'
# Enable site and reload Nginx
sudo ln -s /etc/nginx/sites-available/django.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Step 8: Configuring UFW Firewall and Accessing the Django Admin Panel
Configure Ubuntu's Uncomplicated Firewall (UFW) to allow inbound HTTP traffic on port 80 and HTTPS traffic on port 443.
Open your web browser and navigate to http://your_domain_or_ip to view the default Django congratulations page confirming successful installation.
Navigate to http://your_domain_or_ip/admin and log in using the superuser credentials created in Step 5.
The administrative dashboard provides full control over users, groups, database records, and site configuration.
# Allow Nginx Full profile in UFW firewall
sudo ufw allow 'Nginx Full'
# Confirm firewall status
sudo ufw status numbered
# Test HTTP response via curl
curl -I http://localhost
Troubleshooting and Common Errors
When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:
Nginx returns '502 Bad Gateway' when accessing the Django site
Resolution: This indicates that Nginx cannot communicate with Gunicorn. Verify that the Gunicorn service is active: sudo systemctl status gunicorn. Ensure the UNIX socket exists at /run/gunicorn.sock and that Nginx (www-data user) has read/write permissions to it.
Django Admin renders without CSS styles or images
Resolution: Ensure you executed python manage.py collectstatic and that the Nginx location /static/ directive points to the exact absolute path of your staticfiles directory with appropriate read permissions.
Django displays 'DisallowedHost at /' error in the browser
Resolution: Open myproject/settings.py and verify that the domain name or IP address entered in your browser is explicitly included in the ALLOWED_HOSTS list, then restart Gunicorn: sudo systemctl restart gunicorn.
Security Best Practices and Hardening
Set 'DEBUG = False' in myproject/settings.py for all production deployments to prevent sensitive environment variables and stack traces from leaking to visitors.
Never store Django's 'SECRET_KEY' directly in version control; load it dynamically from environment variables or a secured '.env' file.
Enforce HTTPS encryption using Certbot and Let's Encrypt, and enable 'SECURE_SSL_REDIRECT', 'SESSION_COOKIE_SECURE', and 'CSRF_COOKIE_SECURE' in settings.py.
Restrict database access credentials and configure PostgreSQL with dedicated unprivileged database users for production environments.
Related Ubuntu Tutorials and Documentation
Deepen your Linux system administration skills with our related production-tested guides:
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
runserver command is a single-threaded development server that is neither hardened nor optimized for concurrent requests. Gunicorn is a production-grade multi-worker WSGI server, and Nginx handles SSL termination, static file serving, and DDoS protection.source myprojectenv/bin/activate) and run pip install --upgrade django. Always test your test suite and run python manage.py migrate before restarting the Gunicorn daemon.pip install psycopg2-binary) in your virtual environment and update the DATABASES dictionary in myproject/settings.py with your PostgreSQL host, user, password, and database name.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
Rajesh Ray
I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides
Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!