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:
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:
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:
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:
Step 4: Installing Flask
Using pip, install Flask in the activated environment:
Run the command that gives the Flask version to confirm the installation:
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:
You can exit the python editor by using the following command:
Alternatively, open a nano editor.
Now, enter the following code:
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:
Run the Flask script by using:
After finishing your work, just type deactivate to exit the environment and return to your default shell.
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