Here's a step-by-step guide for installing Django using a virtual environment (venv):
1. Install Python
Ensure Python is installed on your system. Django works with Python versions 3.7 and above. You can check your Python version with:
python --version
If Python is not installed, you can download it from the official website: Python Downloads (opens in a new tab).
2. Create a Project Directory
Create a directory where your Django project will reside.
mkdir my_django_project
cd my_django_project
3. Set Up a Virtual Environment
To keep your project dependencies isolated, it's a good practice to use a virtual environment.
For Linux/macOS:
python3 -m venv venv
For Windows:
python -m venv venv
4. Activate the Virtual Environment
After creating the virtual environment, you need to activate it.
For Linux/macOS:
source venv/bin/activate
For Windows:
venv\Scripts\activate
Once activated, your command line will show (venv)
indicating the virtual environment is active.
5. Upgrade pip
Before installing Django, it's recommended to upgrade pip
to the latest version.
pip install --upgrade pip
6. Install Django
With the virtual environment active, install Django using pip:
pip install django
To confirm the installation, check the Django version:
django-admin --version
7. Create a Django Project
Now that Django is installed, create your first Django project:
django-admin startproject myproject
This will create a directory named myproject
with the following structure:
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
8. Run the Development Server
Navigate into the project directory and run the development server to ensure everything is set up properly.
cd myproject
python manage.py runserver
Open a browser and visit http://127.0.0.1:8000/
to see the default Django welcome page.
9. Deactivate the Virtual Environment
When you're done working, you can deactivate the virtual environment by running:
deactivate
These are the basic steps to install and set up Django with a virtual environment (venv
).