# How to start a new FastAPI project

FastAPI is a very popular, modern, high-speed backend framework based on Python. It has became very popular recently after rising of AI and machine learning. Here is a step bby step guide on how to create you first project with FastAPI.

## Step 1: Install Python:

Before creating a FastAPI project, you need to have **Python 3** installed on your computer. FastAPI is a Python-based web framework, so Python is a necessary prerequisite.

Follow the instructions below according to your operating system

**Installing Python on Windows**

1. Visit the official Python website: <https://www.python.org/downloads/>
2. Click the Download Python 3 button to download the latest stable version.
3. Once the download is complete, open the installer.
4. On the installation screen, make sure to check the Add Python to PATH option.
5. Click Install Now and wait for the installation to finish.

**Verify the installation:**

Open Command Prompt (CMD) or PowerShell and execute:

```shell
python --version
```

**Installing Python on macOS**

macOS may come with Python-related system components, but it's recommended to install a current Python 3 version separately.

2. Visit <https://www.python.org/downloads/macos/>
3. Download the latest stable macOS installer (.pkg).
4. Open the downloaded installer.
5. Follow the installation instructions.
6. Complete the installation.

**Verify the installation:**

Open Terminal and execute:

```shell
python3 --version
```

You should see the installed Python 3 version.

**Installing Python on Linux (Ubuntu/Debian)**

Most Linux distributions either include Python 3 by default or provide it through their package manager.

For Ubuntu or Debian-based distributions, follow these steps:

**Step 1:** Open your terminal.

**Step 2:** Update your package lists.

```plaintext
sudo apt update
```

**Step 3:** Install Python 3.

```plaintext
sudo apt install python3
```

**Step 4:** Install pip, the Python package manager.

```plaintext
sudo apt install python3-pip
```

**Verify the installation:**

```plaintext
python3 --version
```

To verify pip:

```plaintext
pip3 --version
```

If both commands display their respective versions, Python 3 and pip have been installed successfully.

## Step 2: Create a virtual environment

Once you have successfully installed Python on your computer, the next step is to create a **virtual environment** for your FastAPI project.

A virtual environment is an isolated Python environment that allows you to manage project-specific dependencies separately. This prevents package conflicts between different Python projects.

First, create an empty directory for your project. You can name it `fastapi-project` or choose any name you prefer.

Next, open this directory in **Visual Studio Code (VS Code)** and open its integrated terminal.

Now, follow the instructions below according to your operating system.

**Creating a Virtual Environment on Windows**

**Step 1:** Make sure you are inside your project directory in the VS Code terminal.

**Step 2:** Execute the following command to create a virtual environment:

```bash
python -m venv <your_env_name>
```

If the `python` command doesn't work, try:

```bash
py -m venv <your_env_name>
```

This will create a new directory named after your environment name inside your project directory. It contains the isolated Python environment.

**Step 3:** Activate the virtual environment.

For Command Prompt (CMD):

```shell
<your_env_name>\Scripts\activate.bat
```

For PowerShell:

```shell
.<your_env_name>\Scripts\Activate.ps1
```

If PowerShell blocks script execution, you may need to allow scripts for the current user or use Command Prompt instead.

Once activated, you should see your environment name in parentheses at the beginning of your terminal prompt.

**Creating a Virtual Environment on macOS**

**Step 1:** Open the VS Code terminal inside your project directory.

**Step 2:** Create a virtual environment using:

```shell
python3 -m venv <your_env_name>
```

**Step 3:** Activate the virtual environment:

```shell
source <your_env_name>/bin/activate
```

Once activated, you should see your environment name in parentheses at the beginning of your terminal prompt.

**Creating a Virtual Environment on Linux**

**Step 1:** Open the terminal inside your project directory.

**Step 2:** First, make sure the Python virtual environment package is installed. On Ubuntu/Debian, execute:

```shell
sudo apt install python3-venv
```

**Step 3:** Create your virtual environment:

```shell
python3 -m venv <your_env_name>
```

**Step 4:** Activate the virtual environment:

```shell
source <your_env_name>/bin/activate
```

Once activated, you should see your environment name in parentheses at the beginning of your terminal prompt.

**Note:** Whenever you open a new terminal session to work on this project, remember to activate the virtual environment again.

Also, replace &lt;your_env_name> with any name you prefer, such as venv, env, or fastapi-env. Do not include the angle brackets when executing the commands.

## Step 3: Installing Required Packages

Once you have successfully created and activated your virtual environment, it's time to install the packages required for your FastAPI project.

The first package you need is **FastAPI**, which is the web framework we will use to build our API.

The second important package is **Uvicorn**, an ASGI server that runs your FastAPI application and handles incoming requests.

You can install these packages individually using commands like:

```bash
pip install fastapi
pip install uvicorn
```

However, we won't install them this way. Instead, we will use a better approach by maintaining a list of all the required packages in a separate file.

Why?

Imagine you want to run your project on another computer or share it with someone else. Without a record of the required packages, it becomes difficult to know which dependencies need to be installed.

To solve this problem, Python projects commonly use a file named `requirements.txt` to maintain a list of project dependencies.

### Step 1: Create requirements.txt

Inside your project's root directory (the main project folder), create a new text file named:

```text
requirements.txt
```

### Step 2: Add the required packages

Open `requirements.txt` and add the following package names:

```text
fastapi
uvicorn
```

Save the file.

### Step 3: Install all required packages

Now, open your VS Code terminal (make sure your virtual environment is activated) and execute the following command:

```bash
pip install -r requirements.txt
```

Here, the `-r` flag tells pip to read the package names from the specified requirements file and install them.

That's it! Pip will automatically download and install FastAPI, Uvicorn, and their required dependencies inside your virtual environment.

### Why use requirements.txt?

The biggest advantage is that you don't have to remember or manually install every package when setting up the project on another computer.

Simply share your `requirements.txt` file, create and activate a virtual environment on the new computer, and run:

```bash
pip install -r requirements.txt
```

All the listed dependencies will be installed automatically.

**Congratulations!** You have successfully installed the essential packages required to build and run your FastAPI application.
