How to Install Django on Ubuntu

How to Install Django on Ubuntu

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Whether you’re building a simple blog or a complex web application, Django provides the tools and libraries you need to get your project up and running quickly. In this tutorial, we’ll walk through the entire process of installing Django on an Ubuntu system, creating and running your first project, and even setting up a basic production deployment using Gunicorn and Nginx.

This guide is designed for readers who have basic familiarity with the Linux command line and Python. By the end, you’ll have a local Django development environment and a minimal production-ready deployment setup.

Table of Contents

  1. Prerequisites
  2. Step 1: Update Your System
  3. Step 2: Install Python 3 and pip
  4. Step 3: Create a Python Virtual Environment
  5. Step 4: Install Django
  6. Step 5: Start Your First Django Project
  7. Step 6: Run the Development Server
  8. Step 7: Create a Django Admin Superuser
  9. Step 8: Configure Static Files (Optional)
  10. Step 9: Deploying with Gunicorn and Nginx (Production)
  11. Troubleshooting Tips
  12. Conclusion

Prerequisites

Before we begin, make sure you have:

  • An Ubuntu 20.04+ server or desktop, with sudo privileges.
  • Basic familiarity with the terminal and Linux file system.
  • Basic knowledge of Python (optional but helpful).

If you don’t already have an Ubuntu instance, you can launch one in the cloud (DigitalOcean, AWS, Google Cloud, etc.) or install Ubuntu Desktop/Server locally in a VM.

Step 1: Update Your System

First, update your package index and upgrade any out-of-date packages. Open your terminal and run:

sudo apt update
sudo apt upgrade -y
  • sudo apt update refreshes the list of available packages and their versions.
  • sudo apt upgrade -y installs newer versions of the packages you have.

Keeping your system updated ensures security patches and bug fixes are applied.

Step 2: Install Python 3 and pip

Django requires Python 3. Ubuntu 20.04 and later come with Python 3 pre-installed, but it’s good practice to ensure you have the latest point release:

sudo apt install -y python3 python3-pip python3-venv
  • python3 installs the Python 3 interpreter.
  • python3-pip installs pip, the Python package manager.
  • python3-venv provides the venv module to create virtual environments.

Verify the installation:

python3 --version
pip3 --version

Step 3: Create a Python Virtual Environment

Using a virtual environment isolates your project dependencies and prevents conflicts between global and project-specific packages.

  1. Navigate to your projects directory (or create one):
  2. mkdir -p ~/projects/django_demo
    cd ~/projects/django_demo
  3. Create a virtual environment named env:
  4. python3 -m venv env
  5. Activate the virtual environment:
  6. source env/bin/activate
  7. When activated, your prompt will change to show (env).
  8. Upgrade pip within the virtual environment:
  9. pip install --upgrade pip setuptools wheel

Step 4: Install Django

With the virtual environment active, install Django using pip:

pip install django

You can verify the installation and see which version was installed:

django-admin --version

Step 5: Start Your First Django Project

Django provides the django-admin utility to create new projects.

  1. Create a new project called mysite:
  2. django-admin startproject mysite .
  3. Note: The trailing dot (.) tells Django to use the current directory.
  4. Project structure:
  5. django_demo/
    ├── env/
    ├── manage.py
    └── mysite/
        ├── __init__.py
        ├── asgi.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py
  • manage.py: Command-line utility for administrative tasks.
  • mysite/settings.py: Configuration for your Django project.
  • mysite/urls.py: URL declarations for this project.
  • mysite/wsgi.py / mysite/asgi.py: Entry-points for WSGI/ASGI servers.

Step 6: Run the Development Server

Test that everything is configured correctly by running Django’s development server:

python manage.py runserver

You should see output like:

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

System check identified no issues (0 silenced).
July 25, 2025 - 19:00:00
Django version 4.x, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Visit http://127.0.0.1:8000/ in your browser, and you’ll see the default Django welcome page.

Step 7: Create a Django Admin Superuser

The Django admin interface provides a powerful, ready-to-use back-end.

  1. Run the createsuperuser command:
  2. python manage.py createsuperuser
  3. Provide username, email, and password when prompted.
  4. Access the admin site:
  5. Stop the server (CTRL+C), then run:
  6. python manage.py runserver
  7. Navigate to http://127.0.0.1:8000/admin/, log in with your superuser credentials, and you’re in!

Step 8: Configure Static Files (Optional)

For production, Django needs to collect static assets (CSS, JS, images) into a single directory.

  1. In mysite/settings.py, add:
  2. STATIC_URL = '/static/'
    STATIC_ROOT = BASE_DIR / 'staticfiles'
    
  3. Collect static files:
  4. python manage.py collectstatic
  5. Verify that a staticfiles/ directory was created with your assets.

Step 9: Deploying with Gunicorn and Nginx (Production)

When ready for production, you’ll replace the development server with Gunicorn and use Nginx as a reverse proxy.

  1. Install Gunicorn:
  2. pip install gunicorn
  3. Test Gunicorn:
  4. gunicorn --bind 0.0.0.0:8000 mysite.wsgi:application
  5. Create a systemd service for Gunicorn (/etc/systemd/system/gunicorn.service):
  6. [Unit]
    Description=gunicorn daemon
    After=network.target
    
    [Service]
    User=youruser
    Group=www-data
    WorkingDirectory=/home/youruser/projects/django_demo
    ExecStart=/home/youruser/projects/django_demo/env/bin/gunicorn \
              --access-logfile - \
              --workers 3 \
              --bind unix:/home/youruser/projects/django_demo/gunicorn.sock \
              mysite.wsgi:application
    
    [Install]
    WantedBy=multi-user.target
  7. Start and enable Gunicorn:
  8. sudo systemctl start gunicorn
    sudo systemctl enable gunicorn
  9. Install and configure Nginx:
  10. sudo apt install -y nginx
  11. Create /etc/nginx/sites-available/mysite:
  12. server {
        listen 80;
        server_name example.com www.example.com;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/youruser/projects/django_demo;
        }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/home/youruser/projects/django_demo/gunicorn.sock;
        }
    }
  13. Enable the site and restart Nginx:
  14. sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled
    sudo nginx -t
    sudo systemctl restart nginx

Visit http://example.com/ (or your server’s IP) to see your Django application live.

Troubleshooting Tips

  • Permission denied on the socket file: Ensure www-data can access the directory and socket.
  • Static files not loading: Confirm collectstatic ran and Nginx’s root path matches STATIC_ROOT.
  • Debugging errors: Check Gunicorn logs (sudo journalctl -u gunicorn) and Nginx logs (/var/log/nginx/error.log).

Conclusion

Congratulations! You’ve successfully installed Django on Ubuntu, created a virtual environment, started your first project, and deployed it with Gunicorn and Nginx. This setup forms a solid foundation for further development—whether you’re building simple web apps or large-scale projects. From here, explore Django’s rich ecosystem: apps, middleware, REST frameworks, and more. Happy coding!

#Django #Ubuntu #Python #web framework #install Django Ubuntu #Django tutorial #virtual environment #pip #Gunicorn #Nginx #Django deployment #Python 3 #Ubuntu 20.04 #Django project setup

Subscribe to our newsletter

12k+

Subscribers

Weekly

Frequency

Free

Always