
Typically, VSCode is used to develop a FastAPI app. However, you can get a FastAPI app up and running on a Linux system.
On a Debian distribution (Mint, Ubuntu), apt-get can be used to install python-virtualenv. On a Red Hat Distribution (CentOS, Fedora, Red Hat), dnf install or yum install can be used.
dnf install python-virtualenv
If using version 2 of Python, the python -m virtualenv <something unique> command can be used to create a virtual environment. In this example, a virtual environment named demo is created.
python -m virtualenv demo
If using version 3 of Python, the python3 -m venv <something unique> command can be used to create a virtual environment.
python3 -m venv demo
The source command to activate the virtual environment.
source demo/bin/activate
pip install can be used to install FastAPI in the virtual environment. The fastapi[standard] package is used so you can run your FastAPI app on the command line using the fastapi CLI. The uvicorn package is used so that you can run your FastAPI app on the command line using the uvicorn CLI.
(demo) [john.doe@localhost ~]$ pip install fastapi
(demo) [john.doe@localhost ~]$ pip install "fastapi[standard]"
(demo) [john.doe@localhost ~]$ pip install uvicorn
The pip list command should return something like this.
~]$ pip list
Package Version
----------------- ----------
annotated-types 0.7.0
anyio 4.8.0
certifi 2024.12.14
click 8.1.8
dnspython 2.7.0
email_validator 2.2.0
fastapi 0.115.6
fastapi-cli 0.0.7
h11 0.14.0
httpcore 1.0.7
httptools 0.6.4
httpx 0.28.1
idna 3.10
Jinja2 3.1.5
markdown-it-py 3.0.0
MarkupSafe 3.0.2
mdurl 0.1.2
pip 23.2.1
pydantic 2.10.5
pydantic_core 2.27.2
Pygments 2.19.1
python-dotenv 1.0.1
python-multipart 0.0.20
PyYAML 6.0.2
rich 13.9.4
rich-toolkit 0.12.0
shellingham 1.5.4
sniffio 1.3.1
starlette 0.41.3
typer 0.15.1
typing_extensions 4.12.2
uvicorn 0.34.0
uvloop 0.21.0
watchfiles 1.0.3
websockets 14.1
Create a file named app.py that contains something like this.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"Hello": "World"}
Run the app.
(demo) [john.doe@localhost ~]$ fastapi run app.py
Something like this should be displayed.
FastAPI Starting production server 🚀
Searching for package file structure from directories with __init__.py files
Importing from /home/john.doe
module 🐍 app.py
code Importing the FastAPI app object from the module with the following code:
from app import app
app Using import string: app:app
server Server started at http://0.0.0.0:8000
server Documentation at http://0.0.0.0:8000/docs
Logs:
INFO Started server process [2667570]
INFO Waiting for application startup.
INFO Application startup complete.
INFO Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
In another Terminal on the same Linux system, the curl command can be used to see if you can get a response from your FastAPI app.
~]$ curl http://127.0.0.1:8000/
{"Hello":"World"}
Optionally, you can use import uvicorn and then use uvicorn.run to pass in additional options, such as host and port.
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
def home():
return {"Hello": "World"}
if __name__ == "__main__":
uvicorn.run(app, host='0.0.0.0', port=12345)
And then you can run the FastAPI app using the Python CLI instead of the fastapi CLI.
(demo) [john.doe@localhost ~]$ python3 app.py
INFO: Started server process [2681853]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:12345 (Press CTRL+C to quit)
Did you find this article helpful?
If so, consider buying me a coffee over at