Skip to main content

FastAPI Installation

Welcome to the first step in your FastAPI journey! This guide will walk you through installing FastAPI and setting up your development environment. FastAPI is a modern, fast web framework for building APIs with Python based on standard Python type hints.

Prerequisites

Before installing FastAPI, make sure you have:

  • Python 3.7+ (FastAPI requires Python 3.7 or above)
  • Basic knowledge of Python programming
  • A terminal or command prompt

Installation Methods

The simplest way to install FastAPI is using pip, Python's package installer:

bash
pip install fastapi uvicorn

We're installing two packages:

  • FastAPI: The framework itself
  • Uvicorn: An ASGI server required to run FastAPI applications

Verifying Installation

To verify the installation was successful, you can check the installed versions:

bash
pip show fastapi
pip show uvicorn

Method 2: Using a Virtual Environment (Best Practice)

For a cleaner development environment, it's recommended to use virtual environments:

1. Create a virtual environment

bash
# On Windows
python -m venv fastapi-env

# On macOS/Linux
python3 -m venv fastapi-env

2. Activate the virtual environment

bash
# On Windows
fastapi-env\Scripts\activate

# On macOS/Linux
source fastapi-env/bin/activate

3. Install FastAPI and Uvicorn

bash
pip install fastapi uvicorn

Optional Dependencies

FastAPI has several optional dependencies that provide additional features:

bash
# For full features including data validation
pip install "fastapi[all]"

This will install:

  • Uvicorn: ASGI server with reload capability
  • Pydantic: For data validation
  • Starlette: The core of FastAPI
  • Email validator: For email validation
  • PyJWT: For JWT tokens
  • Python-multipart: For form data parsing
  • Itsdangerous: For security features
  • SQLAlchemy: For database support
  • Ujson: For fast JSON parsing

Creating Your First FastAPI App

Let's create a simple "Hello World" application to test our installation:

  1. Create a file named main.py with the following content:
python
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
return {"Hello": "World"}
  1. Run the server:
bash
uvicorn main:app --reload
  1. Open your browser and navigate to http://127.0.0.1:8000/. You should see:
json
{
"Hello": "World"
}

The --reload flag enables hot reloading, which means your application will automatically update when you make changes to the code.

Understanding the Installation Components

FastAPI Framework

The core framework that provides all the functionality for building APIs. It is built on top of Starlette for web parts and Pydantic for data validation.

Uvicorn

An ASGI server implementation that FastAPI applications run on. ASGI (Asynchronous Server Gateway Interface) allows asynchronous Python web applications to communicate with the server.

Troubleshooting Common Installation Issues

Issue 1: Python Version

If you encounter an error like:

ERROR: fastapi requires Python >=3.7

Solution: Update your Python version to 3.7 or higher.

Issue 2: Permission Errors

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied

Solution: Use the --user flag or run the command with administrator/sudo privileges:

bash
pip install --user fastapi uvicorn

Issue 3: Dependency Conflicts

If you have conflicting packages:

bash
pip install --upgrade pip
pip install "fastapi[all]" --upgrade

Real-world Application: Project Structure Setup

For a real project, you might want to set up a more organized structure:

my-fastapi-project/
├── app/
│ ├── __init__.py
│ ├── main.py
│ ├── models/
│ │ └── __init__.py
│ ├── routes/
│ │ └── __init__.py
│ └── services/
│ └── __init__.py
├── requirements.txt
└── README.md

Create a requirements.txt file:

fastapi>=0.100.0
uvicorn>=0.22.0
pydantic>=2.0.0

Then install dependencies:

bash
pip install -r requirements.txt

Summary

You've successfully installed FastAPI and learned how to:

  • Install FastAPI using pip
  • Set up a virtual environment for your project
  • Create a simple "Hello World" application
  • Structure a real-world FastAPI project

FastAPI is now ready to use on your system! With this setup, you're prepared to build high-performance, easy-to-develop APIs using Python's type hints.

Additional Resources

Exercises

  1. Create a FastAPI application with two different routes, returning different data types.
  2. Set up a FastAPI project using a virtual environment and create a custom requirements.txt file.
  3. Implement a FastAPI application that receives a query parameter and returns it as part of the response.


If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)