FastAPI Setting Up Development Environment
Before you write any FastAPI code, your computer needs the right tools installed. This topic walks you through every step from a blank machine to a running FastAPI server.
What You Need Before Starting
You need Python 3.8 or higher. FastAPI uses modern Python features that older versions do not support. Check your version by running this in your terminal:
python --version
If you see Python 3.8.x or higher, you are ready. If Python is not installed, download it from python.org and run the installer.
Create a Project Folder
Keep your code organized from the start. Create a dedicated folder for your FastAPI project:
mkdir my-fastapi-project cd my-fastapi-project
Use a Virtual Environment
A virtual environment is like a private box for your project's packages. Libraries you install inside this box do not affect the rest of your computer, and libraries from other projects cannot interfere with yours.
Your Computer
│
├── Project A (virtual env)
│ └── fastapi 0.100, pydantic 2.x
│
└── Project B (virtual env)
└── fastapi 0.88, pydantic 1.x
Create and activate the virtual environment:
On Windows
python -m venv venv venv\Scripts\activate
On macOS or Linux
python -m venv venv source venv/bin/activate
Your terminal prompt changes to show (venv) at the start. That confirms the virtual environment is active.
Install FastAPI and Uvicorn
You need two packages. FastAPI is the framework. Uvicorn is the server that actually runs your FastAPI app and listens for requests.
pip install fastapi uvicorn
Here is how they work together:
Browser / Client
|
| HTTP request
v
[ Uvicorn ] ← the server (listens on port 8000)
|
| passes request to
v
[ FastAPI App ] ← your Python code
|
| returns response
v
[ Uvicorn ]
|
v
Browser / Client
Verify Your Installation
Check that both packages installed correctly:
pip show fastapi pip show uvicorn
You see version information for each package. No errors means the installation succeeded.
Choose a Code Editor
Any text editor works, but Visual Studio Code (free, from Microsoft) gives you the best experience for FastAPI development. Install the Python extension inside VS Code to get autocomplete, error highlighting, and debugging support.
Create Your First File
Create a file called main.py in your project folder and paste this code:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"status": "FastAPI is running!"}
Start the Server
Run this command in your terminal from inside the project folder:
uvicorn main:app --reload
The main part refers to your main.py file. The app part refers to the app = FastAPI() object inside it. The --reload flag tells Uvicorn to restart automatically every time you save a change to your code.
You see output like this:
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process
Open your browser and visit http://127.0.0.1:8000. You see {"status": "FastAPI is running!"}.
Explore the Automatic Docs
Visit http://127.0.0.1:8000/docs in your browser. FastAPI shows you an interactive documentation page — already built, without you writing anything extra.
Key Points
- Python 3.8 or higher is required.
- Always use a virtual environment to keep project dependencies separate.
- Install both
fastapianduvicornwith pip. - Run your app with
uvicorn main:app --reload. - Your app is live at
http://127.0.0.1:8000and docs at/docs.
