What Flask is and why you need to install it first

Flask is a Python framework — a collection of tools and code that makes building web applications faster. Before you can write a Flask application, you need to install Flask on your computer. Installation means downloading the Flask code and putting it somewhere Python can find it when you run your programs.

Flask runs on top of Python, so you must have Python installed first. If you do not have Python yet, install it from python.org before you start these steps. Flask itself installs in seconds once Python is ready, but the setup process is different on Windows, Mac, and Linux.

Key Takeaways

  • Python must be installed on your computer before Flask can work — check by opening a terminal or command prompt and typing python --version.
  • Flask installs through pip, which is Python's package manager and comes automatically with Python 3.4 and newer.
  • On Windows, use Command Prompt; on Mac and Linux, use Terminal — both are built into your operating system.
  • After installation, test Flask by creating a simple Python file that imports Flask and runs without errors.

Check whether Python is already installed

Before installing Flask, open your computer's command line tool. On Windows, search for "Command Prompt" in the Start menu and click it. On Mac or Linux, search for "Terminal" and click it.

Type this command and press Enter:

python --version

If you see a version number like "Python 3.9.0", Python is installed and you can move to the next step. If you see "command not found" or "python is not recognized", Python is not installed — go to python.org, download the latest version, and run the installer before continuing.

Install pip if it is not already present

Pip is the tool that downloads and installs Flask. It comes built-in with Python 3.4 and newer, so you likely have it already. Check by typing this command in your terminal or Command Prompt:

pip --version

If you see a version number, pip is ready. If you see "command not found", you need to install it. On Windows, open Command Prompt as Administrator (right-click Command Prompt and select "Run as administrator"), then type:

python -m ensurepip --default-pip

On Mac or Linux, open Terminal and type the same command. This tells Python to set up pip for you.

Create a virtual environment for your Flask project

A virtual environment is a separate folder on your computer where Flask and other tools live for just one project. This keeps different projects from interfering with each other. You do not have to use a virtual environment, but it is the standard practice and prevents problems later.

In your terminal or Command Prompt, navigate to the folder where you want to keep your Flask project. Then type this command:

python -m venv flask_env

This creates a folder called "flask_env" in your current location. Replace "flask_env" with any name you prefer — it is just a folder name.

Now activate the virtual environment. On Windows, type:

flask_env\Scripts\activate

On Mac or Linux, type:

source flask_env/bin/activate

After you press Enter, you should see the environment name in parentheses at the start of your command line, like (flask_env). This tells you the virtual environment is active.

Install Flask using pip

With the virtual environment active, type this command:

pip install flask

Pip downloads Flask and all the code it depends on, then installs everything in your virtual environment folder. This takes a minute or two depending on your internet speed. When it finishes, you will see a message saying the installation succeeded.

If you see an error message, the most common cause is that your virtual environment is not active — check that you see the environment name in parentheses at the start of your command line. If the environment is active and you still see an error, try typing the command again.

Test your Flask installation

Create a new file in your project folder and name it app.py. Open it in any text editor and type this code:

from flask import Flaskapp = Flask(__name__)@app.route('/')def hello():    return 'Flask is working'if __name__ == '__main__':    app.run(debug=True)

Save the file. Then in your terminal or Command Prompt, make sure your virtual environment is still active and type:

python app.py

If Flask is installed correctly, you will see output that says something like "Running on http://127.0.0.1:5000". Open a web browser and go to that address. You should see the text "Flask is working". If you see it, Flask is installed and working. Press Ctrl+C in your terminal to stop the application.

Troubleshooting common installation problems

If you get an error saying "No module named flask", the virtual environment is not active. Go back to the step where you activate it and make sure you see the environment name in parentheses before your command prompt.

If pip says "command not found" on Mac or Linux, try typing pip3 instead of pip. Some computers have both Python 2 and Python 3 installed, and pip3 specifically uses Python 3. If that works, use pip3 for all future commands.

If you see a permission error on Mac or Linux, do not use sudo to run pip. Instead, make sure you are using a virtual environment — the virtual environment avoids permission problems by installing everything in your own project folder.

Frequently Asked Questions

Do I have to use a virtual environment?

No, but it is strongly recommended. A virtual environment keeps Flask and other tools separate for each project, so installing a new version for one project does not break another. Without it, all your projects share the same Flask installation, which causes conflicts.

What is the difference between pip and pip3?

Pip3 specifically uses Python 3, while pip might use Python 2 on older computers. If you have both Python versions installed, pip3 ensures you are using the right one. On most modern computers, pip and pip3 are the same.

Can I install Flask without a terminal or command prompt?

No. Flask installs through pip, which only works from the command line. Some code editors like Visual Studio Code have built-in terminals, so you can type commands without opening a separate window, but you still use the command line.

What do I do after Flask is installed?

Start building a Flask application. Create a Python file, import Flask, and define routes — these are the web addresses your application responds to. The test file in this guide is a complete working Flask application that you can modify and expand.

How do I deactivate the virtual environment when I am done?

Type deactivate in your terminal or Command Prompt. The environment name in parentheses will disappear from your command line. To use Flask again, activate the environment with the same command you used before.