How to install FastAPI

Joël-Steve N.
2 min readJan 10, 2024

--

https://fastapi.tiangolo.com/
https://fastapi.tiangolo.com/

FastAPI is a fast, modern Web framework for building APIs with Python 3.8+ simply and efficiently. It offers high performance thanks to the use of native Python typing and Python’s asynchronous API. In this blog, we’ll take you step-by-step through how to install FastAPI on your machine.

Step 1: Prerequisites

Before installing FastAPI, make sure you have the following:

  • Python 3.8+ installed on your machine
  • A Python virtual environment (e.g. `venv`) is recommended to isolate your project.

Step 2: Create and activate the virtual environment

1 - Open your terminal or command prompt
2 - Navigate to your project directory

cd path/to/your/project

3 - Create a new virtual environment using `venv`

python3 -m venv my_env 

4 - Activate the virtual environment

  • On Windows :
my_env\Scripts\activate.bat
  • On macOS and Linux :
source my_env/bin/activate

Step 3: Install FastAPI

1 - Make sure your virtual environment is activated before continuing
2 - In the terminal, run the following command to install FastAPI using pip, the Python package manager:

pip install fastapi

This will install FastAPI and the necessary dependencies.

Step 4: Check installation

To check that FastAPI is correctly installed, you can create a minimal Python file with a Hello World example. Create a main.py file in your project directory and add the following code:

from fastapi import FastAPI

app = FastAPI()

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

Save the file and run it:

uvicorn main:app --reload

You should see a message indicating that the server is active at http://localhost:8000. Try to access this URL in your browser, and you should see the JSON response `{“Hello”: “World”}`.

Congratulations! You’ve successfully installed and run FastAPI.

Conclusion

FastAPI is an excellent choice for developing fast, high-performance APIs with Python. In this blog, we’ve covered the steps required to install FastAPI on your machine. Now it’s time to dive deeper into FastAPI’s documentation and explore all the powerful features it offers to speed up your web application development.

Don’t hesitate to consult the official FastAPI documentation to find out more and discover advanced usage examples.

--

--

Joël-Steve N.

Chief Technology Officer | Community Manager | Backend Developper (Python, Javascript, Java)