Setting Up Your Development Environment
1. Installing Python
Django requires Python 3.10 or newer. Open your terminal and check if Python is already installed:
python3 --version
If you see output like Python 3.11.9, you are ready. If not, download the latest Python installer from python.org and follow the installer for your operating system. On Ubuntu/Debian Linux, you can install it directly:
sudo apt update
sudo apt install python3 python3-pip python3-venv -y
2. What is a Virtual Environment?
A virtual environment (venv) is an isolated Python installation for a single project. Imagine you have two projects: Project A uses Django 4.2 and Project B uses Django 5.0. If you install both globally on your machine, they will conflict. A virtual environment solves this by giving each project its own private folder of packages that cannot interfere with each other.
Think of it like this: your global Python installation is a shared office kitchen. A virtual environment is your own personal lunchbox — only your food, your rules.
3. Creating and Activating a Virtual Environment
First, create a project directory and navigate into it:
# Create a new directory for your project
mkdir myproject
cd myproject
Now create the virtual environment. The venv module is built into Python 3:
# This creates a hidden folder called 'venv' with a private Python copy
python3 -m venv venv
Before you install or run anything, you must activate the environment. Activating it tells your terminal to use the Python and pip inside the venv folder instead of the global ones:
# On macOS / Linux:
source venv/bin/activate
# On Windows (Command Prompt):
venv\Scripts\activate.bat
# On Windows (PowerShell):
venv\Scripts\Activate.ps1
You will know it is active because your terminal prompt changes to show (venv) at the beginning:
(venv) digamber@laptop:~/myproject$
To deactivate it when you are done working, simply type:
deactivate
4. Installing Django
With the virtual environment active, install Django using pip, Python’s package manager:
# pip installs packages from PyPI (the Python Package Index)
pip install django
This downloads and installs the latest stable version of Django and its dependencies into your venv folder. To pin a specific version (recommended for production):
pip install django==5.0.6
5. Verifying the Installation
Confirm Django is installed correctly:
python -m django --version
# Expected output: 5.0.6 (or your installed version)
6. Saving Your Dependencies
It is best practice to record every package your project depends on. This lets anyone (or any server) reproduce your exact environment later:
# Writes all installed packages and their versions to a text file
pip freeze > requirements.txt
Later, on a new machine or server, you can recreate the environment with:
pip install -r requirements.txt
7. Recommended: Install a Code Editor
For the best experience, use Visual Studio Code (free, from code.visualstudio.com) with the Python and Django extensions installed. These provide syntax highlighting, autocompletion, and debugging support specifically for Django templates and Python files.