Categories: Ubuntu

Ultimate Guide to Install Flask on Ubuntu

Flask is an open-source and free micro web-based python framework, designed to help programmers for building scalable, secure, and easily maintainable web applications. If you are a beginner, then, it’s quite easy and simple to start. We will tell you in this article how to install the python framework Flask on Ubuntu 20.04 system. The commands we have implemented can also run on Debian and old Ubuntu distributions.

Pre-requirements

All commands you should run under the ‘sudo’ command privileges.

Installation of Flask on Ubuntu 20.04

Follow the below-mentioned steps to install the Flask on Ubuntu 20.04 system:

Step 1: Installation of Python

Ensure that Python is installed on your Ubuntu system. To check the installation, run the below-given command on the terminal:

Sponsored
$ python3 -V

It is a recommended method to create a virtual environment in python using the venv module. You can create this environment with the help of the python3-venv package. Therefore, you will have to install this package by executing the below-mentioned command on your system:

$ sudo apt install python3-venv


Once the above package module is installed on your Ubuntu system, you can create a python virtual environment in which you will install and use the Flask application.

Create a directory for the Flask application and navigate it in this folder using the below-given command:

$ mkdir flask_application && cd flask_application

Step 2: Create a python virtual environment

Now, inside the specified directory ‘flask_application’, create a virtual environment by running the following command:

$ python3 -m venv venv

A new directory or virtual environment is created with the name ‘venv’ that consists of a copy of all Python supporting files. You can also create a different name with this virtual environment.

Now, activate the above virtual environment using the following command, and then you can use it:

$ source venv/bin/activate

Step 3: Install Flask using pip

Once the environment is activated, you will notice that the virtual environment name will be added at the beginning of the shell prompt. Now, install Flask using the Python package manager pip as follows:

(venv) $ pip install Flask

You can print the installed version of Flask using the following command:

Sponsored
(venv) $ python -m flask –version

At this time, the latest version of Flask 1.1.2 has been installed on your Ubuntu system, which you can also see in the below-given screenshot:

Create Minimal Application using Flask

Here, we will create a simple application that will print the text ‘First Flask application!’. Open the text editor and paste the following code into this file:

from flask import Flask
app = Flask(__name__)

@app.route(‘/’)
def my_app():
    return ‘First Flask application!’

Save the above file inside the Flask_application directory with the name ‘my_app.py’.
In the above code:

  • The first line will import the Flask class.
  • The second line will create a new Flask class instance.
  • The function my_app is registered through route() decorator. When you requested this route the ‘First Flask application!’ text will print on the terminal.

To execute the above code run the following commands:

(venv) $ export flask_application=my_app.py
(venv) $ flask run

The following output will print on the terminal:

Now, open the browser and type the ‘http://127.0.0.1:5000’ URL in the address bar. The ‘First Flask application!’ message will display in the browser.

To stop the shell output, press ‘Ctrl-C’. Once you have finished your work, type the following command to deactivate the virtual environment or exit from it:

(venv) $ deactivate

Conclusion

In the above article, we have explained the procedure of how to install the Flask on the Ubuntu 20.04 environment. We have also described how you can create a python virtual environment and install Flask in it. We have experienced different commands to run the application using Flask. For more details, please visit the Flask documentation from internet resources.

Ubuntu Server Admin

Recent Posts

Canonical Releases Ubuntu 25.04 Plucky Puffin

The latest interim release of Ubuntu introduces “devpacks” for popular frameworks like Spring, along with…

2 days ago

Ubuntu 25.04 (Plucky Puffin) Released

Ubuntu 25.04, codenamed “Plucky Puffin”, is here. This release continues Ubuntu’s proud tradition of integrating…

3 days ago

Extended Security Maintenance for Ubuntu 20.04 (Focal Fossa) begins May 29, 2025

Ubuntu released its 20.04 (Focal Fossa) release 5 years ago, on March 23, 2020. As…

3 days ago

Ubuntu 20.04 LTS End Of Life – activate ESM to keep your fleet of devices secure and operational

Focal Fossa will reach the End of Standard Support in May 2025, also known as…

4 days ago

Ubuntu MATE 25.04 Release Notes

Ubuntu MATE 25.04 is ready to soar! 🪽 Celebrating our 10th anniversary as an official…

5 days ago

Ubuntu Weekly Newsletter Issue 887

Welcome to the Ubuntu Weekly Newsletter, Issue 887 for the week of April 6 –…

6 days ago