Installing FastAPI
To install FastAPI, you first create a virtual environment for Python. After you activate the virtual environment, you can use the pip installer to install FastAPI. To do this, follow these steps:- Log in to your server using SSH.
-
If you are running Ubuntu on your server, as the root user, type the following command:
📘 Note If you are running another distribution that already has curl and Python 3 virtual environment support installed, you do not need to install any additional packages. If your distribution does not have these packages installed, consult its documentation on how to do so.
-
As a regular (non-root) user, type the following commands:
-
The virtual environment is now created and activated. To update pip, type the following command:
-
To install FastAPI, type the following commands:
-
FastAPI is now installed. To determine which version is installed, type the following command:
Running an example application
FastAPI is now installed, and you can create and configure FastAPI applications. The following example shows how to do this.- Use a text editor to create a file named main.py.
-
Copy and paste the following code into the main.py file:
-
To start the development web server, type the following command::
-
In another terminal window, type the following command:
You should see the following JSON response:You now have a basic, functioning web API created using FastAPI.