How to create an executable of FastAPI server using python
Write this server
app.py
from fastapi import FastAPI
import uvicorn
import multiprocessing
app = FastAPI()
@app.get("/")
def hello():
return "Hello, World!"
if __name__ == '__main__':
multiprocessing.freeze_support() # For Windows support
uvicorn.run(app, host="0.0.0.0", port=8009, reload=False, workers=1)
Install this packages
pip install pyinstaller
pip install fastapi uvicorn
After installing the command and checking if the server properly runs in development run this command to generate executable Note: This is cuurently implemented for linux
# Create the executable
pyinstaller --onefile --name my-fastapi-server main.py
# Navigate to the dist folder
cd dist
# Make it executable (if needed)
chmod +x my-fastapi-server
# Run the FastAPI server
./my-fastapi-server
Make sure the server post is not already occupied by other services
it can be also execute terminal-less, just select the exec in nautilus a.k.a File explorer, sideclick then press run
it will run without showing terminal