Friday, July 11, 2025

How To Install Fast API with MongoDB on Ubuntu 24.04 LTS

 In this tutorial, you'll learn how to install FastAPI with MongoDB on Ubuntu 24.04 LTS . 
FastAPI is a web framework based on Python for creating API services. It's a modern, fast, high-performance framework supporting asynchronous operations.
__________________________________________________________________________________________________________________
Testing Environment: Ubuntu 24.04.1 LTS                   Hostname -                       ip Address - 
__________________________________________________________________________________________________________________
apt update ; apt install build-essential net-tools curl git software-properties-common gnupg python3 python3-pip python3-venv -y

curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
--dearmor

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

sudo apt update && sudo apt install mongodb-org -y

sudo systemctl enable --now mongod ; sudo systemctl status mongod
mongosh


su - username
mkdir -p ~/app; cd ~/app
python3 -m venv .venv
source .venv/bin/activate
deactivate
pip3 install fastapi uvicorn
mkdir -p server/{models,routes}
touch main.py server/{app.py,database.py} server/models/itemModels.py server/routes/item.py

nano app.py  | nano server/app.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
 return {"message": "Hello FastAPI!"}

nano main.py
import uvicorn

if __name__ == "__main__":
 uvicorn.run("server.app:app", host="0.0.0.0", port=8080, reload=True)

python3 main.py
__________________________________________________________________________________________________________________

No comments:

Post a Comment