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> You need sudo access.
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
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.
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> If Django is running on a remote server, add the server's IP to the <code>ALLOWED_HOSTS</code> list in <code>settings.py</code> 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.