FastAPI Installation and Environment Setup
FastAPI depends on Python 3.8 or higher. This section explains how to install FastAPI and configure the development environment.
Check Python version
Before installing FastAPI, first confirm your Python version:
$ python --version
Python 3.11.0
# 或者
$ python3 --version
Python 3.11.0
If your Python version is lower than 3.8, please upgrade Python first.
Create a virtual environment
It is recommended to install FastAPI in a virtual environment to avoid conflicts with system Python packages:
# 创建虚拟环境
$ python -m venv venv
# Activate virtual environment (macOS/Linux)
$ source venv/bin/activate
# Activate virtual environment (Windows)
$ venv\Scripts\activate
Virtual environments are a best practice in Python development. Using a separate virtual environment for each project avoids dependency conflicts between different projects.
Install FastAPI
Install FastAPI using pip:
$ pip install fastapi
This command installs only the FastAPI core package. If you need to install FastAPI with all its optional dependencies at once, you can use:
$ pip install "fastapi[all]"
fastapi[all] The following components will be installed:
| Package name | Purpose |
|---|---|
uvicorn[standard] |
ASGI server for running FastAPI applications |
python-multipart |
Form data and file upload support |
jinja2 |
HTML template engine |
python-jose[cryptography] |
JWT token support |
passlib[bcrypt] |
Password hashing and encryption |
python-dotenv |
Environment Variable Management |
Install Uvicorn
FastAPI is an ASGI framework and needs an ASGI server to run. The most commonly used one is Uvicorn:
$ pip install "uvicorn[standard]"
[standard] This installs uvloop (high-performance event loop) and httptools (high-performance HTTP parser), significantly improving performance.
ASGI (Asynchronous Server Gateway Interface) is the standard interface between Python asynchronous web servers and applications, and is the asynchronous version of WSGI. Traditional frameworks like Flask use WSGI (synchronous), while FastAPI uses ASGI (asynchronous), enabling more efficient handling of concurrent requests.
Run your first FastAPI application
After installation, create a file named main.py:
Example
from
fastapi
import
FastAPI
Create a FastAPI application instance
app
=
FastAPI
(
)
Define a GET route for the root path
@
app.
get
(
"/"
)
async
def
root
(
)
:
return
{
"message"
:
"Hello World"
}
Start the application from the command line:
$ uvicorn main:app --reload
Parameter Description:
| Parameters | Description |
|---|---|
main:app |
main refers to the filename main.py,app refers to the name of the FastAPI instance variable created in the file |
--reload |
Development mode, automatically reloads the server after code changes (for development only) |
After a successful start, the terminal will display:
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process
INFO: Started server process
INFO: Waiting for application startup.
INFO: Application startup complete.
Open the browser and visit http://127.0.0.1:8000, you will see the following JSON response:
{"message": "Hello World"}

At the same time, FastAPI automatically generates interactive API documentation:
| Address | Document type | Description |
|---|---|---|
| http://127.0.0.1:8000/docs | Swagger UI | Documentation interface for interactively testing the API |
| http://127.0.0.1:8000/redoc | ReDoc | Documentation interface focused on a better reading experience |
Using the FastAPI CLI
Newer versions of FastAPI provide the fastapi command-line tool, making it easier to run applications:
# 开发模式(自动重载)
$ fastapi dev
# Production mode
$ fastapi run
fastapi dev automatically finds the FastAPI application in the project and starts the development server, equivalent to uvicorn main:app --reload。
Recommended development tools
| Tool | Description |
|---|---|
| VS Code | Free and lightweight; with the Python and Pylance extensions it offers an excellent FastAPI development experience |
| PyCharm | Professional Python IDE with built-in FastAPI project templates and powerful debugging features |
| Alibaba Qoder | An AI programming IDE launched by Alibaba, deeply customized based on VS Code |
| ByteDance Trae | AI-powered development tool launched by ByteDance, with built-in Python support |
VS Code plus the Python extension is recommended; it offers the best support for FastAPI type annotations and autocompletion, and it is completely free.