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 --versionIf 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_project3. 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 venvFor Windows:
python -m venv venv4. Activate the Virtual Environment
After creating the virtual environment, you need to activate it.
For Linux/macOS:
source venv/bin/activateFor Windows:
venv\Scripts\activateOnce 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 pip6. Install Django
With the virtual environment active, install Django using pip:
pip install djangoTo confirm the installation, check the Django version:
django-admin --version7. Create a Django Project
Now that Django is installed, create your first Django project:
django-admin startproject myprojectThis will create a directory named myproject with the following structure:
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py8. 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 runserverOpen 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:
deactivateThese are the basic steps to install and set up Django with a virtual environment (venv).