Rohan Yeole - HomepageRohan Yeole

Setup Django with a Virtual Environment

By Rohan Yeole

When working with Django, using a virtual environment is a best practice that ensures your project dependencies are isolated from system-wide packages. This guide walks you through setting up Django with a virtual environment on Ubuntu, preparing your project for a clean, manageable deployment.

Prerequisites

  • Ubuntu server (or local machine running Ubuntu)
  • Python 3 and pip installed
  • Basic knowledge of terminal commands

Install Python and pip

Ensure Python 3 and pip are installed

sudo apt update
sudo apt install python3 python3-pip

Install venv (if not already installed)

sudo apt install python3-venv

Create a Project Directory

mkdir ~/myproject
cd ~/myproject

 Create and Activate the Virtual Environment

python3 -m venv venv
source venv/bin/activate
After activating, your shell prompt should change to show the environment name -

(venv) user@hostname:~/myproject$

Install Django

With the virtual environment activated

pip install django

Create a Django Project

django-admin startproject myproject

Verify Django Installation

Run the development server

python manage.py runserver
Visit http://127.0.0.1:8000/ in your browser to confirm Django is working.

Configure Static and Media Files

Update settings.py 

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static' # only on production

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

Frequently Asked Questions (FAQ)

Do I really need a virtual environment for Django?

Yes. Virtual environments isolate your project dependencies from system packages. This prevents version conflicts and makes your project easier to manage and deploy.

Are virtual environments portable?

Not entirely. Virtual environments store absolute paths that may not work if moved to another machine or OS. It’s best to recreate the environment using requirements.txt.

Where is my virtual environment stored?

In this example, it is stored at ~/myproject/venv. You can place it anywhere, but keeping it within your project directory is a common convention.

What’s the difference between venv, virtualenv, and pipenv?

  • venv: Built-in with Python 3; simple and sufficient for most projects.

  • virtualenv: Works with both Python 2 and 3; offers some extra features.

  • pipenv: Combines virtual environment and dependency management.

Is Django still maintained?

Absolutely. Django is actively developed and widely used in production by companies like Instagram, Pinterest, and NASA. Visit djangoproject.com for updates.

Did you forget to activate your virtual environment?

If you encounter module not found errors, make sure to run:

source venv/bin/activate

How does a Python virtual environment work?

venv creates a folder containing a copy of the Python interpreter and a place to install dependencies using pip. Activating it ensures all operations (e.g., pip install) are scoped to that environment.

Which Django version is installed?

Run -

django-admin --version

Why use a virtual environment at all?

It ensures your project doesn’t interfere with system-level Python packages, making it easier to collaborate, deploy, and maintain.

Must Read -

How to use Gunicorn with Django?

How To Set Up Django with PostgreSQL, Gunicorn, and Nginx on Ubuntu (Production-Ready)