Django requires Python, so the first step is installing a compatible Python version. Download it from python.org. During installation on Windows, ensure "Add python.exe to PATH" is selected. On macOS, proceed with the default installer.
Verify the installation:
python --version
# or on some macOS systems
python3 --version
A version number indicates successful installation.
Using a Virtual Environment (Recommended)
Isolate project dependencies using Python’s built-in venv module. This avoids conflicts between projects requiring different Django versions.
Create a virtual environment in your project directory:
python -m venv myenv
Replace myenv with your preferred environment name.
Activate the Environment
-
macOS/Linux:
source myenv/bin/activate -
Windows:
myenv\Scripts\activate
Once activated, your terminal prompt will show the environment name.
Daectivate
Exit the virtual environment anytime with:
deactivate
Install Django
With the virtual environment active, install Django via pip:
pip install django
# On systems where pip defaults to Python 2, use:
pip3 install django
To install a specific version:
pip install django==4.1
Confirm the installation:
django-admin --version
List installed packages:
pip list
Create a New Project
Generate a new Django project:
django-admin startproject mysite
This creates a mysite directory with the initial project structure.
Run the Developmetn Server
Navigate into the project folder and start the server:
cd mysite
python manage.py runserver
By default, the server runs at http://127.0.0.1:8000/. To use a different port:
python manage.py runserver 9000