How To

Install Django on Ubuntu 18.04: Python venv Setup Guide

Django is a free, open-source Python web framework built for developing secure, scalable, and maintainable web applications. It follows a batteries-included philosophy, shipping with tools for authentication, URL routing, database abstraction, form handling, and an automatic admin interface. Django powers some of the world’s busiest sites, including Instagram, Pinterest, and Disqus.

You can install Django system-wide using apt, but the version in Ubuntu’s repositories lags several releases behind the latest. Installing into a Python virtual environment with pip is the better approach. It isolates each project’s dependencies, lets you run different Django versions on the same machine, and keeps your system Python installation clean.

This guide walks you through installing Django on Ubuntu 18.04 inside a virtual environment and creating your first project.

<strong>Prerequisite:</strong>&nbsp;You need sudo access.

Install Django on Ubuntu: Set Up Python and a Virtual Environment

Ubuntu 18.04 ships with Python 3.6 by default. Verify it is available:

bashpython3 -V
Python 3.6.6

Install the python3-venv package, which provides the module for creating virtual environments:

bashsudo apt install python3-venv

Create a project directory and navigate into it:

bashmkdir my_django_appcd my_django_app

Create a new virtual environment:

bashpython3 -m venv venv

The venv folder contains a copy of the Python binary, pip, and the standard library. Each virtual environment is fully independent. Changes in one project do not affect any other.

Activate the environment:

bashsource venv/bin/activate

Your shell prompt changes to show the environment name (venv). Inside this environment, use pip instead of pip3 and python instead of python3.

Install Django:

bashpip install django

Confirm the installation:

bashpython -m django --version
2.1.2

Create a Django Project and Set Up the Database

Use django-admin to create a new project:

bashdjango-admin startproject mydjangoapp

This creates a mydjangoapp directory with manage.py (the project management script) and a subdirectory containing settings.py, urls.py, and wsgi.py.

Navigate into the project:

bashcd mydjangoapp

By default, Django uses a SQLite database, which is fine for development. For production, you can switch to PostgreSQL, MySQL, or MariaDB. Apply the initial migrations to set up the database tables:

bashpython manage.py migrate

Create an admin user for the Django admin interface:

bashpython manage.py createsuperuser

The command prompts you for a username, email address, and password. Avoid using admin as your username for security.

Run the Development Server and Admin Panel

Start the development server:

bashpython manage.py runserver
Starting development server at http://127.0.0.1:8000/

Open http://127.0.0.1:8000 in your browser to see the default Django landing page. Access the admin panel at http://127.0.0.1:8000/admin/ and log in with your superuser credentials.

<strong>Note:</strong>&nbsp;If Django is running on a remote server, add the server's IP to the&nbsp;<code>ALLOWED_HOSTS</code>&nbsp;list in&nbsp;<code>settings.py</code>&nbsp;before accessing it from a browser.

Stop the server with CTRL+C. When you are done working, deactivate the virtual environment:

bashdeactivate

Your shell returns to normal and the project’s isolated Python environment is no longer active.

Django is now installed and your first project is running on Ubuntu 18.04. Visit the Django documentation to learn how to build your first full application. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

What Is a Data Protection Platform and Why Your Organization Needs One

A data protection platform is a unified system that helps organizations discover, classify, monitor, and…

2 weeks ago

How Swiss Privacy Rules Affect AI Companies Under GDPR

AI companies operating in or around Switzerland face a compliance challenge that most legal teams…

2 weeks ago

Swiss FADP vs EU GDPR: Key Differences for AI and Data Privacy

Two major data privacy laws now govern how organizations handle personal data across Europe; the…

2 weeks ago

Kali Linux Commands Cheat Sheet: Complete Quick Reference

This cheat sheet covers the essential Kali Linux commands every pentester and ethical hacker uses…

2 months ago

What I Wish I Knew Before Learning Malware Analysis and Reverse Engineering

When I first started learning malware analysis and reverse engineering, I thought the hardest part…

2 months ago

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

2 months ago