Installing Flask

Before you write a single line of Flask code, you set up your development environment. This topic walks you through installing Python, creating a virtual environment, and installing Flask — step by step.

What You Need Before Installing

You need Python 3.8 or newer installed on your computer. Open a terminal or command prompt and run this command to check:

python --version

If Python is installed, you see something like Python 3.11.2. If not, download it from python.org and install it first.

Why Use a Virtual Environment

Imagine you are working on two Flask projects. Project A needs Flask version 2.2. Project B needs Flask version 3.0. If you install both on your computer's main Python, they overwrite each other and cause errors.

A virtual environment is like a separate toolbox for each project. Each toolbox holds its own version of Flask and other packages. Nothing leaks between toolboxes.

Your Computer
│
├── Project A/
│   └── venv/  (Flask 2.2 inside)
│
├── Project B/
│   └── venv/  (Flask 3.0 inside)
│
└── System Python  (no Flask installed here)

Creating a Virtual Environment

Navigate to your project folder in the terminal, then run:

On Windows:

python -m venv venv

On macOS / Linux:

python3 -m venv venv

This creates a folder called venv inside your project directory. That folder holds a private copy of Python and pip.

Activating the Virtual Environment

After creating the virtual environment, you activate it. Activation tells your terminal to use the private Python inside venv instead of the system Python.

On Windows (Command Prompt):

venv\Scripts\activate

On Windows (PowerShell):

venv\Scripts\Activate.ps1

On macOS / Linux:

source venv/bin/activate

Once activated, your terminal prompt shows (venv) at the beginning. That indicator means the virtual environment is active.

Before activation:  C:\Projects\myapp>
After activation:   (venv) C:\Projects\myapp>

Installing Flask

With the virtual environment active, install Flask using pip:

pip install flask

pip downloads Flask along with its two dependencies — Werkzeug and Jinja2. You see output in the terminal as each package installs. The process takes about 10–30 seconds depending on your internet speed.

Verifying the Installation

Confirm Flask installed correctly by running:

python -m flask --version

You see output similar to:

Python 3.11.2
Flask 3.0.0
Werkzeug 3.0.1

Those three lines confirm Flask is ready to use.

Saving Dependencies with requirements.txt

When you share your project with a teammate or deploy it to a server, that person needs to install the same packages. The requirements.txt file lists every package your project uses. Generate it with:

pip freeze > requirements.txt

The generated file looks like this:

Flask==3.0.0
Werkzeug==3.0.1
Jinja2==3.1.2
click==8.1.7
itsdangerous==2.1.2
MarkupSafe==2.1.3

Anyone who clones your project installs all dependencies with one command:

pip install -r requirements.txt

Deactivating the Virtual Environment

When you finish working on your project, deactivate the virtual environment by running:

deactivate

The (venv) prefix disappears from your terminal, and your system Python takes over again.

Installation Summary Diagram

Step 1: Check Python  →  python --version
Step 2: Create venv   →  python -m venv venv
Step 3: Activate      →  source venv/bin/activate
Step 4: Install       →  pip install flask
Step 5: Verify        →  python -m flask --version
Step 6: Save deps     →  pip freeze > requirements.txt

Summary

Setting up Flask takes five minutes. Create a virtual environment so your project stays isolated, install Flask inside it, and verify the installation. From this point forward, every command you run in the terminal for your Flask project should happen inside the activated virtual environment.

Leave a Comment

Your email address will not be published. Required fields are marked *