How to Install Flask on Linux, Windows, and macOS

August 29, 2024

Introduction

Flask is one of the most popular web application frameworks written in Python. It is a microframework designed for an easy and quick start. Extending with tools and libraries adds more functionality to Flask for more complex projects.

This article explains how to install Flask in a virtual testing environment and create a simple Flask application.

How to install Flask on Windows, Linux, and macOS - a tutorial.

Prerequisites

Step 1: Install Virtual Environment

A virtual environment in Python is an isolated workspace that allows you to manage dependencies for different projects without conflicts with the system's global Python installation. We will install Flask in a virtual environment to avoid problems with conflicting libraries.

Python 3 comes with a preinstalled virtual environment module called venv. If your system does not have venv or you previously removed it, the sections below show how to install the module on Linux, macOS, and Windows.

Install virtualenv on Linux

In this tutorial, we will use Ubuntu. The virtualenv module is available in the official Ubuntu repository, and we will install it using apt. Follow the steps below:

1. Open the Linux terminal.

2. Use the following command to install virtualenv:

sudo apt install python3-venv

Wait for the process to complete.

Install virtualenv on macOS

On macOS, we will use pip, a package manager for Python that allows you to install, update, and manage libraries and dependencies for your Python projects. Follow the steps below:

1. Open the terminal.

2. Install virtualenv on macOS with the following command:

sudo python3 -m pip3 install virtualenv

Install virtualenv on Windows

Like in macOS, we will use the pip package manager to install virtualenv:

1. Open the command line with administrator privileges.

2. Use pip to install virtualenv on Windows:

pip install virtualenv
Installing virtualenv on Windows using pip.

Step 2: Create an Environment

After setting up the virtualenv module on your system, create an environment for Flask. First, create a new directory by following the steps below, and then move on to the section for your operating system:

1. Make a separate directory for your project:

mkdir [project_name]

2. Navigate to the directory:

cd [project_name]

Create an Environment in Linux and macOS

We will initialize the virtual environment for Flask within the directory you created in the previous step. When you create an environment, a new folder with the environment's name appears in your project directory.

To create a virtual environment, use the venv module and choose a name:

python3 -m venv [environment_name]

For example:

python3 -m venv env_test

The command creates a new virtual environment named env_test.

Create an Environment in Windows

Create and name a virtual environment in Windows using the following syntax:

py -3 -m venv [environment_name]

List the folder structure using the dir command to confirm it was created:

dir *[project_name]*
Project directory with created virtual environment in Windows

The project directory shows the newly created environment.

Step 3: Activate the Environment

Activate the virtual environment before installing Flask. The name of the activated environment shows up in the CLI after activation. Follow the steps in the section pertaining to your operating system:

Activate the Environment on Linux and macOS

Use the syntax below to activate the virtual environment in Linux and macOS:

source [environment_name]/bin/activate
Activating virtual environment on Ubuntu.

The output shows the environment name in the next line.

Activate the Environment on Windows

For Windows, activate the virtual environment with:

[environment_name]\Scripts\activate
Command line change after activating the environment on Windows.

The output shows test, the environment name, in the next line.

Step 4: Install Flask

Install Flask within the activated environment using pip:

pip install Flask

Flask is installed automatically with all the dependencies.

Installing Flask on a virtual environment in Ubuntu.

Note: When working in a typical environment, users need to use pip3 and python3 to ensure they are using the Python 3.x installation. However, when you set up a virtual environment, especially for a Flask project, the virtual environment isolates the Python version. So, pip and python within that environment automatically refer to the correct Python 3.x versions.

Step 5: Test the Development Environment

This section shows how to test your Flask development environment by creating a simple Hello World application that prints "Hello World!". Follow the steps below:

1. Create a new file in the Flask project directory called hello.py. In Linux and macOS, you can use the touch command:

touch hello.py

Note: Pick any name for the project except flask.py. The Flask library is in a flask.py file.

2. Edit the file using a text editor and paste the following code to make an application that prints "Hello world!":

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello world!'

3. Save the file and close.

4. Using the terminal, navigate to the project directory using the cd command.

5. Set the FLASK_APP environment variable. Run the command below:

  • For Linux and macOS:
export FLASK_APP=hello.py
  • For Windows:
setx FLASK_APP "hello.py"

Note: Windows users must restart the console to set the environment variable. Learn more about setting environment variables by reading one of our guides: How to Set Environment Variables in Linux, How to Set Environment Variables in macOS, How to Set Environment Variables in Windows.

6. Run the Flask application with:

flask run
Running a flask application in the terminal.

The output prints out a confirmation message and the address.

7. Copy and paste the address into the browser to see the project running:

Flask app running on local server.

The app prints the "Hello world!" message, which means that it is running properly on the local server.

Conclusion

The guide showed how to install Flask on major operating systems. Flask is one of the most popular web application frameworks for Python.

Next, read about the best Python IDEs and code editors for further web development with Flask.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Best Python IDEs And Code Editors
November 6, 2023

An IDE is like a toolbox and a Code editor is like a power tool. In this article, we review 9 IDEs and 5 Code...
Read more
How to Calculate Website or Web-Based Application Bandwidth Requirements
November 12, 2020

Read through the article to learn how to determine the required bandwidth for your website and web-based...
Read more
Introduction to Python Pandas
July 28, 2020

This tutorial introduces you to basic Python Pandas concepts and commands. A selection of clear-cut images...
Read more
How to Use Comments in Python
November 25, 2019

The ability to use comments while writing code is an important skill valued among developers. These comments...
Read more