How to install Flask on Ubuntu 20.04?

Overview

One of the most well-known Python web application frameworks is Flask. This microframework was created to be simple and quick to implement. Flask gains greater capabilities through extension with libraries and tools to support more complicated projects.

In this tutorial, we'll install Flask in a simulated testing environment and make a basic Flask application.

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • Ubuntu 20.04 equipped system

  • A regular root user

  • Internet connection

  • Knowledge of Command Line Interface (CLI)

Get Started

Step 1: Virtual Environment Installation

To prevent conflicts across libraries, we will be installing Flask in a virtual environment.

  • In order to configure virtualenv on Ubuntu as well as other compatible distributions, start by launching the terminal and using the following apt command:

apt install python3-venv

When prompted to continue with the installation, press 'Y.'

Step 2: Creating an Environment

  • Set up a distinct directory for your project, then enter the directory:

mkdir newenv
cd newenv

The virtual Flask environment should be created inside the directory. In your project directory, a brand new folder bearing the environment's name appears once you create it.

  • Use the venv script and give your Python 3 virtual environment a name to establish a virtual environment:

python3 -m venv newenv

Step 3: Activating the Environment

Install Flask after activating the virtual environment. After activation, the identity of the enabled environment appears in the CLI.

  • Launch Linux's virtual environment by using the following command:

. newenv/bin/activate

Step 4: Installing Flask

  • Using pip, install Flask in the activated environment:

pip install Flask
  • Run the command that gives the Flask version to confirm the installation:

python -m flask --version

Step 5: Testing the Environment

In order to test the newly built development environment, design a basic Flask application. Create a file called hello.py and place it within the Flask project folder.

  • You can use the python editor using the following command:

python
  • You can exit the python editor by using the following command:

exit()
  • Alternatively, open a nano editor.

nano
  • Now, enter the following code:

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

Return to your console after saving the file as hello.py.

  • The application will be run using the flask command, but first, we must instruct the shell which application to use by setting the environment variable FLASK APP:

export FLASK_APP=hello.py
  • Run the Flask script by using:

flask run
  • After finishing your work, just type deactivate to exit the environment and return to your default shell.

deactivate

Conclusion

On your Ubuntu 20.04 system, we've shown you how to install Flask and set up a Python virtual environment. You can repeat the process to generate more Flask development environments.

Last updated