How to Install FastAPI: A Complete Beginner’s Guide
FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.6+ based on standard Python type hints. It helps developers quickly create clean and efficient RESTful APIs with automatic interactive documentation. In this tutorial, you will learn how to install FastAPI and run your first API app from scratch.
Prerequisites
- A computer with Python 3.7 or newer installed. If you don’t have Python installed, download it from the official Python website (Official site).
- Basic knowledge of Python programming.
- A command-line interface (CLI) or terminal access.
- Optional: A code editor like Visual Studio Code for writing your FastAPI codes.
Step 1: Create a Virtual Environment (Recommended)
It’s best practice to create a virtual environment to isolate your FastAPI project’s dependencies. Run the following commands in your terminal:
python -m venv fastapi-env
Activate the virtual environment:
- On Windows:
fastapi-env\Scripts\activate
source fastapi-env/bin/activate
Step 2: Install FastAPI and Uvicorn
FastAPI is the web framework; Uvicorn is a lightning-fast ASGI server used to run FastAPI apps. Install both with pip:
pip install fastapi uvicorn
Step 3: Create Your First FastAPI App
Using your favorite code editor, create a new Python file named main.py and add the following code:
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
def read_root():
return {"Hello": "FastAPI"}
This simple app creates a GET endpoint at the root URL that returns a JSON greeting.
Step 4: Run the FastAPI App
Execute this command to start the Uvicorn server, which will host your FastAPI application:
uvicorn main:app --reload
The --reload flag enables auto-reload on code changes, handy during development.
Open your browser and visit http://127.0.0.1:8000 to see the JSON response.
Step 5: Explore Automatic API Documentation
FastAPI provides interactive API documentation generated automatically. Visit:
- Swagger UI for an interactive web interface.
- ReDoc for an alternative styled API docs.
Troubleshooting Common Issues
- Python Version: Ensure Python 3.7+ is installed by running
python --version. Upgrade if necessary. - Virtual Environment: Activate your virtual environment before installing packages.
- Port in Use: If port 8000 is busy, specify a different one with
uvicorn main:app --reload --port 8001. - Missing Packages: If you get import errors, verify FastAPI and Uvicorn are installed correctly.
Summary Checklist
- Installed Python 3.7 or above.
- Created and activated a virtual environment.
- Installed FastAPI and Uvicorn using pip.
- Created a basic FastAPI app in
main.py. - Ran the app with Uvicorn server.
- Accessed API endpoints and interactive docs in browser.
For further enhancement, you may want to learn how to install Flask and compare backend Python frameworks. FastAPI complements many modern Python ecosystems for web development and APIs.
With this installation done, you are now ready to dive deeper into FastAPI features like path parameters, request bodies, security, and async programming!
