# Setting up Django Project with virtual Environment

I’ll walk you through **Django + virtual environment** on **Linux** and **Windows**, step-by-step, clean and beginner-safe.

---

## 1️⃣ Prerequisites (both Linux & Windows)

Make sure Python is installed:

```bash
python --version
```

or

```bash
python3 --version
```

If it prints `Python 3.x.x`, you’re good.

---

## 2️⃣ Create a Project Folder

```bash
mkdir django_project
cd django_project
```

This folder will hold:

* virtual environment
    
* Django project code
    

---

# 🪟 WINDOWS (CMD / PowerShell)

## 3️⃣ Create Virtual Environment

```plaintext
python -m venv env
```

---

## 4️⃣ Activate Virtual Environment

### ▶ CMD

```plaintext
env\Scripts\activate
```

### ▶ PowerShell

```powershell
env\Scripts\Activate.ps1
```

If PowerShell blocks it, run **once** as admin:

```powershell
Set-ExecutionPolicy RemoteSigned
```

Then activate again.

You’ll see:

```text
(env) C:\django_project>
```

---

## 5️⃣ Upgrade pip

```plaintext
python -m pip install --upgrade pip
```

---

## 6️⃣ Install Django

```plaintext
pip install django
```

Check:

```plaintext
django-admin --version
```

---

## 7️⃣ Create Django Project

```plaintext
django-admin startproject mysite .
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">here specifying <code>mysite .</code> will create the project in current folder/directory as . represent current directory</div>
</div>

```plaintext
django-admin startproject mysite
cd mysite
```

---

## 8️⃣ Run Server

```plaintext
python manage.py runserver
```

Open browser 👉  
👉 [**http://127.0.0.1:8000/**](http://127.0.0.1:8000/)

---

## 9️⃣ Deactivate Virtual Environment

```plaintext
deactivate
```

---

# 🐧 LINUX (Ubuntu, Debian, Arch, etc.)

## Create Virtual Environment

```bash
python3 -m venv env
```

This creates a folder called `env`.

---

## Activate Virtual Environment

```bash
source env/bin/activate
```

You’ll see something like:

```text
(env) user@machine:~/django_project$
```

👉 This means **virtual env is active**

---

## Upgrade pip (recommended)

```bash
pip install --upgrade pip
```

---

## Install Django

```bash
pip install django
```

Verify:

```bash
django-admin --version
```

---

## Create Django Project

```bash
django-admin startproject mysite
cd mysite
```

Folder structure:

```text
django_project/
├── venv/
└── mysite/
    ├── manage.py
    └── mysite/
```

---

## Run Development Server

```bash
python manage.py runserver
```

Open browser 👉  
👉 [**http://127.0.0.1:8000/**](http://127.0.0.1:8000/)

🎉 Django welcome page = SUCCESS

---

## Deactivate Virtual Environment

```bash
deactivate
🔥 Common Mistakes (Important)
```

❌ Forgetting to activate venv  
❌ Installing Django globally  
❌ Running `runserver` outside project folder  
❌ Using `python` vs `python3` incorrectly on Linux

---

# 🧠 Pro Tip (Best Practice)

Managing Dependency using `pip freeze`

`pip freeze` will output the list of packages with versions installed in the current virtual environment (if activated)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1770175610052/2a5b0c36-0a57-4a9d-ad8e-4a648ab3b9aa.png align="center")

We can then save that list to a file named `requirements.txt` for future ( so we can reinstall everything in new setup environment)

```bash
pip freeze > requirements.txt
```

This will make a file named `requirements.txt` and save the output of `pip freeze` that has packages and its versions

Later you can install everything with:

```bash
pip install -r requirements.txt
```
