FastAPI - Beginner to Pro / FastAPI Setup & Installation

Setting up FastAPI

πŸ–₯️ Step 1: Create Project Folder

🍎 macOS / Linux

mkdir fastapi-project
cd fastapi-project

πŸͺŸ Windows

mkdir fastapi-project
cd fastapi-project

🐍 Step 2: Create Virtual Environment

🍎 macOS / Linux

python3 -m venv venv

πŸͺŸ Windows

python -m venv venv

This creates a folder named venv/ containing the isolated Python environment.


▢️ Step 3: Activate Virtual Environment

🍎 macOS / Linux

source venv/bin/activate

πŸͺŸ Windows

venv\\Scripts\\activate

After activation, your terminal should look like:

(venv) fastapi-project %

That means your virtual environment is active βœ…


πŸ“¦ Step 4: Install FastAPI & Uvicorn

We need:

  • FastAPI β†’ Web framework

  • Uvicorn β†’ ASGI server to run FastAPI

πŸ“Œ What is ASGI?

ASGI (Asynchronous Server Gateway Interface) is a standard that allows Python apps to handle:

  • HTTP requests

  • WebSockets

  • Async operations

FastAPI is built on ASGI.

So it needs an ASGI-compatible server to run.

🍎 macOS / Linux

pip install fastapi uvicorn

πŸͺŸ Windows

pip install fastapi uvicorn

(Inside virtual environment, pip works correctly on both.)


πŸ“ Step 5: Create Main Application File

Create a file named:

main.py

Add this code:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, FastAPI πŸš€"}

▢️ Step 6: Run the Server

🍎 macOS / Linux

uvicorn main:app --reload

πŸͺŸ Windows

uvicorn main:app --reload

πŸ” What This Command Means

  • main β†’ filename (main.py)

  • app β†’ FastAPI instance

  • -reload β†’ auto-restarts server on code changes


🌐 Step 7: Open in Browser

Visit:

<http://127.0.0.1:8000>

You should see:

{"message": "Hello, FastAPI πŸš€"}