FastAPI - Beginner to Pro / FastAPI Setup & Installation

Installing Dependencies

📦 How to Install Dependencies

Once the virtual environment is activated, you can install packages using pip.

📥 Install a Package

pip3 install fastapi (Mac/Linux)
**OR**
pip install fastapi (windows)

Example:

pip3 install django (Mac/Linux)
**OR**
pip install django (windows)

📌 Install Multiple Packages

pip3 install fastapi uvicorn sqlalchemy (Mac/Linux)
**OR**
pip3 install fastapi uvicorn sqlalchemy (windows)

🔄 Upgrade a Package

pip3 install --upgrade fastapi
**OR**
pip install --upgrade fastapi

❌ Uninstall a Package

pip3 uninstall fastapi
**OR**
pip uninstall fastapi

🎯 Best Practices

  • Always create a virtual environment for every project

  • Add venv/ to .gitignore

  • Always commit requirements.txt

  • Activate the environment before installing packages


<aside> 💡

Going forward, we will use macOS commands as the default.

If any command differs on Windows, we’ll clearly provide the Windows equivalent alongside it.

</aside>


📄 Save Dependencies to a File (requirements.txt)

🧠 Why Do We Need This?

When working on a project, you install multiple packages like:

  • FastAPI

  • Django

  • Requests

  • SQLAlchemy

  • etc.

If someone else wants to run your project (or you move to another machine), they need the exact same package versions.

Instead of manually telling them:

“Install FastAPI 0.110.0, Uvicorn 0.29.0, Pydantic 2.6.1…”

We generate a file that automatically lists everything.

That file is called:

requirments.txt

🛠️ The Command

pip3 freeze > requirements.txt

🔍 What This Command Does

🔹 pip freeze

  • Lists all installed packages

  • Includes exact versions

  • Shows them in this format:

fastapi==0.110.0
uvicorn==0.29.0
pydantic==2.6.1

🔹 > requirements.txt

The > symbol means:

"Take the output of the command and save it into a file"

So this:

pip3 freeze > requirements.txt

Means:

Take all installed packages and write them into requirements.txt


🔁 How Someone Recreates Your Environment

After cloning your project:

1️⃣ Create & Activate Virtual Environment

python3 -m venv venv

Activate it.


2️⃣ Install Everything From File

pip3 install -r requirements.txt

The -r means:

“Read from this file”

Now all required packages are installed automatically.