All commands you should run under the ‘sudo’ command privileges.
Follow the below-mentioned steps to install the Flask on Ubuntu 20.04 system:
Ensure that Python is installed on your Ubuntu system. To check the installation, run the below-given command on the terminal:
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:
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:
Now, inside the specified directory ‘flask_application’, create a virtual environment by running the following command:
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:
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:
You can print the installed version of Flask using the following command:
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:
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:
@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:
To execute the above code run the following commands:
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:
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.
Microsoft Edge is now available for Ubuntu. In this guide, I’ll walk you through the…
Our latest Canonical website rebrand did not just bring the new Vanilla-based frontend, it also…
At Canonical, the work of our teams is strongly embedded in the open source principles…
Welcome to the Ubuntu Weekly Newsletter, Issue 873 for the week of December 29, 2024…
Have WiFi troubles on your Ubuntu 24.04 system? Don’t worry, you’re not alone. WiFi problems…
The following is a post from Mark Shuttleworth on the Ubuntu Discourse instance. For more…