Install FastAPI and Uvicorn: Step-by-step setup
Informational article in the Building REST APIs with FastAPI topical map — Getting Started & Fundamentals content group. 12 copy-paste AI prompts for ChatGPT, Claude & Gemini covering SEO outline, body writing, meta tags, internal links, and Twitter/X & LinkedIn posts.
Install FastAPI and Uvicorn by creating an isolated Python environment and running pip install fastapi uvicorn; FastAPI requires Python 3.8 or later and Uvicorn is an ASGI server that runs FastAPI applications. Using a virtual environment keeps global packages untouched and prevents permission conflicts common with system pip. The minimal install consists of two packages. The default development server listens on port 8000, which is commonly used for local testing. Typical developer workflow uses Python virtual environments created with venv, virtualenv, or pipx to isolate dependencies across projects and ensure reproducible installs. After running the server, OpenAPI documentation is available at http://127.0.0.1:8000/docs for verification in a browser or terminal locally.
FastAPI works by building on the Starlette framework and the Pydantic data model, delivering ASGI-compatible request handling and fast validation. Uvicorn implements the ASGI specification and runs apps with an event loop based on asyncio or uvloop for improved throughput. For FastAPI installation, common options include pip install fastapi uvicorn inside a virtualenv or using pipx install uvicorn for a globally available server binary while keeping project dependencies isolated. Developers aiming for local development often run uvicorn main:app --reload to enable automatic code reloads; in production, combining Uvicorn with Gunicorn or using Docker images provides process management and multi-worker scaling. A common production flag set is uvicorn --workers 4 --host 0.0.0.0 --port 8000 with uvloop available.
A frequent misconception is that global pip installs are acceptable for app development, which leads to permission errors and dependency conflicts; using venv or virtualenv fastapi project isolation avoids this. Windows users often fail at activation because PowerShell blocks scripts by default and may see an error that Activate.ps1 cannot be loaded; resolving this requires running Set-ExecutionPolicy RemoteSigned -Scope CurrentUser or using Command Prompt activation. Another common omission is not shipping a minimal runnable app, so running uvicorn --reload main:app works as a quick verification. For production, a single Uvicorn worker should be replaced with process managers like Gunicorn or containerized multi-worker setups to leverage multiple CPU cores. Alternatively, pipx can install Uvicorn globally and Docker images provide consistent runtime.
With the install and environment details established, a practical sequence is to create a virtual environment (python -m venv .venv), activate it, run pip install fastapi uvicorn, create a minimal app module that exposes an ASGI app object named app (for example an app with from fastapi import FastAPI; app = FastAPI()), and run uvicorn app:app --reload --host 127.0.0.1 --port 8000. The running server exposes OpenAPI at http://127.0.0.1:8000/docs for verification in a browser or terminal locally. Alternatively, pipx can install Uvicorn globally and Docker images enable reproducible testing. This page contains a structured, step-by-step framework.
- Work through prompts in order — each builds on the last.
- Click any prompt card to expand it, then click Copy Prompt.
- Paste into Claude, ChatGPT, or any AI chat. No editing needed.
- For prompts marked "paste prior output", paste the AI response from the previous step first.
install fastapi
Install FastAPI and Uvicorn
authoritative, practical, conversational
Getting Started & Fundamentals
Python developers (beginners to intermediate) who want a fast, reliable step-by-step guide to installing and running FastAPI with Uvicorn for local development and light production testing
A friction-reducing, cross-platform step-by-step install guide that covers venv/pipx/Docker, minimal runnable example, common errors and fixes, performance flags, and exact commands for Windows/macOS/Linux so readers can finish setup in 10 minutes.
- FastAPI installation
- Uvicorn setup
- pip install fastapi uvicorn
- pipx install uvicorn
- virtualenv fastapi
- run FastAPI server
- uvicorn --reload
- Not using a virtual environment — authors often show global pip installs which cause permission issues and dependency conflicts on reader machines.
- Omitting Windows-specific commands — many guides only show macOS/Linux commands so Windows users fail at 'python -m venv venv' or PowerShell path activation.
- Not showing a minimal runnable app — missing a tiny app.py example means readers can't immediately verify the server runs.
- Failing to mention uvicorn --reload and host flags — readers run into binding issues or don't see auto-reload for development.
- Skipping troubleshooting for common errors like 'ModuleNotFoundError: fastapi' or 'address already in use' — leaving novices stuck.
- Not covering pipx or Docker alternatives — tutorial assumes virtualenv only and misses readers who prefer pipx or containers.
- No performance or production-light notes (workers, --workers, Gunicorn integration) — leading to unsafe uses in production.
- Recommend explicit Python versions (e.g., Python 3.10+ or 3.11) and show the exact 'python3 -V' check — editors that include version checks reduce support questions.
- Provide copy-paste terminal blocks for Windows PowerShell and macOS/Linux separately; include exact activation commands to avoid confusion (e.g., 'venv\Scripts\Activate.ps1' vs 'source venv/bin/activate').
- Show a one-line 'smoke test' curl command (curl -sS localhost:8000/ | jq .) and a browser screenshot so readers validate the setup immediately.
- Include pipx as a lightweight alternative: show 'python -m pip install --user pipx; pipx install uvicorn' and explain why pipx is safer for global CLI tools.
- When mentioning Docker, provide a minimal Dockerfile and 'docker run --rm -p 8000:80 <image>' example and explain port differences between Uvicorn host/port and container port.
- Add recommended development flags for Uvicorn ('--reload --host 127.0.0.1 --port 8000') plus one-line note on security risk of '--host 0.0.0.0' during development.
- Suggest a short 'dev' entry in pyproject.toml or Makefile for repeatable commands (e.g., 'make dev' or 'scripts' in pyproject) to help readers avoid typing errors.
- Include gitignore and brief note to exclude virtualenv directories and .env files; that small step prevents credential leaks and messy repos.