Flask is a free and open-source micro web framework for Python designed to help developers build secure, scalable, and maintainable web applications.
Flask is based on Werkzeug and uses Jinja2 as a template engine.
Also, it is known as a micro-framework because it doesn't require any particular third-party libraries and tools.
However, Flask can support extensions that are implemented in Flask itself to add features into a Flask application.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Software Installation task on Ubuntu Systems.
In this context, we shall look into method to install the Flask framework on your Ubuntu 20.04 machine.
In order to install Flask on Ubuntu 20.04, let’s update the system first with the following command:
$ sudo apt update
Once the update is complete, go ahead to the next steps.
By default, Ubuntu 20.04 comes with Python 3.8. You can verify this by the following command:
$ python3 -V
Next, you have to install python3-venv package to create a virtual environment for the Flask application:
$ sudo apt install python3-venv
After the package is installed, let's create a virtual environment for the Flask application.
It is recommended to create a new directory for the application and navigate into it:
$ mkdir flask-dir && cd flask-dir
Now, let’s run the following command in flask-dir to create the virtual environment:
$ python3 -m venv venv
The command creates a directory named venv in flask-dir directory.
In order use the virtual environment, you have to activate it as follows:
$ source venv/bin/activate
Once the virtual environment is activated, you can install Flask using the Python package manager pip:
(venv) $ pip install Flask
Verify that the Flask is successfully installed by running the following command:
(venv) $ python -m flask --version
Congratulations, now you can create some Flask applications on your Ubuntu 20.04.
Here, we will create a simple “Hello world” application with Flask.
Using your favorite editor to create a Python file named as: hello.py in flash-dir:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello world'
In your virtual environment, run the following commands:
(venv) $ export FLASK_APP=hello.py
(venv) $ flask run
You can use your web browser or curl command to hit http://127.0.0.1:5000, you will get the "Hello world" text output.
This article covers how to install Flask on Ubuntu 20.04. Flask is a powerful web framework for any developer. Unlike Django , by default Flask doesn’t include ORM, form validation, or any other functionalities provided by third-party libraries. Flask is built with extensions in mind, which are Python packages that add functionality to a Flask application.
Flask packages are included in the official Ubuntu repositories and can be installed using the apt package manager.
This is the simplest way to install Flask on Ubuntu 20.04, but not as flexible as installing in a virtual environment.
Also, the version included in the repositories may lag behind the latest version of Flask.
To install Flask on Ubuntu 20.04:
1. Ubuntu 20.04 ships with Python 3.8. You can verify that Python is installed on your system by typing:
$ python3 -V
2. The recommended way to create a virtual environment is by using the venv module, which is provided by the python3-venv package. Run the following command to install the package:
$ sudo apt install python3-venv
3. Create a new directory for the Flask application and switch into it:
$ mkdir flask_app && cd flask_app
4. Run the following command inside the directory to create the virtual environment:
$ python3 -m venv venv
The command will create a directory called venv, which contains a copy of the Python binary, the Pip package manager , the standard Python library, and other supporting files. You can use any name you want for the virtual environment.
5. To start using the virtual environment, you need to activate it with the activate script:
source venv/bin/activate
6. Now that the virtual environment is activated, use the Python package manager pip to install Flask:
$ pip install Flask
7. To verify the installation, run the following command, which prints the Flask version:
$ python -m flask --version