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:

$ 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:

(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.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.