FastAPI Project: Installation and Setup Guide

FastAPI offers a reliable and practical framework for creating Python APIs, and the project setup and installation procedures are simple. FastAPI is a fantastic option for creating high-quality web APIs because of its robust features and fantastic performance.

Dependencies installation

The complete directions for installing Python 3.7 and the required dependencies for FastAPI can be found here:

  • Update the system's package manager and install essential packages:

apt update
apt install -y software-properties-common curl
Package updation
  • Install pyenv for managing Python versions:

curl https://pyenv.run | bash
pyenv installation
  • Add pyenv to the system's environment:

echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
source ~/.bashrc
Adding pyenv to system's environment
  • Install build dependencies for Python:

apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev \
libsqlite3-dev llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
Installing build dependencies
  • Install Python 3.7 using pyenv:

pyenv install 3.7.12
pyenv global 3.7.12
Python 3.7 installation using pyenv
  • Verify that Python 3.7 is installed:

python --version
Python version check
  • Install pip and the necessary FastAPI dependencies:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
pip install fastapi pydantic uvicorn
pip and FastAPI dependencies installation

With these steps, you'll have Python 3.7 installed using pyenv and the necessary dependencies for FastAPI.

Please note that the installation steps assume a Linux-based environment. If you're using a different operating system, the steps may vary. Additionally, ensure you have the necessary privileges to perform system-level installations.

Step-by-step guide

Here's a guide for setting up a basic FastAPI project.

  • Create a new project directory:

mkdir fastapi-project
cd fastapi-project
Creating a project directory
  • Set up a virtual environment:

python3 -m venv venv
source venv/bin/activate
Setting the virtual environment
  • Install FastAPI and Uvicorn:

pip install fastapi uvicorn
Installing Fast API and Uvicorn
  • Create a main.py file and open it in a text editor:

touch main.py
Creating the main.py file
  • Inside the main.py file, copy and paste the following code as a starting point:

from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
main.py
  • Save the main.py file and close the text editor.

  • Start the FastAPI development server:

uvicorn main:app --reload
Starting the FastAPI server

Open a web browser and navigate to http://localhost:8000 to see the "Hello World" message.

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

FastAPI application utput

You now have a basic FastAPI project set up with a couple of examples of routes. You can continue building your API by adding more routes, models, and logic per your project requirements.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved