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.pyurls.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

Install and Configure Redis on Ubuntu 18.04: Remote Access Guide

Redis is an open-source, in-memory data structure store used as a database, cache, and message broker.…

47 minutes ago

Upgrade Ubuntu 16.04 to 18.04 Bionic Beaver: Step-by-Step Guide

Ubuntu 18.04 LTS (Bionic Beaver) was released on April 26, 2018, with five years of official…

1 hour ago

Set Up Apache Virtual Hosts on Ubuntu 18.04: Complete Guide

Apache Virtual Hosts let you run multiple websites on a single server. Each site gets its…

1 hour ago

Configure MySQL Master-Slave Replication on Ubuntu 18.04

MySQL replication is the process of automatically copying data from one database server to one or…

2 hours ago

Install Joomla on Ubuntu 18.04 with Apache: LAMP Stack Guide

Joomla is one of the most popular open-source content management systems in the world. It is…

1 day ago

Install WordPress on Ubuntu 18.04 with Apache: LAMP Stack Guide

WordPress is the most popular open-source content management system in the world, powering over a quarter…

1 day ago