Introduction
Flask is one of the most well-known web frameworks used by many developers in the world. It is written in Python and designed to help developers build secure and scalable web applications.
Flask 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.
This article will show you the way to install the Flask framework on your Ubuntu 20.04 machine.
Installing Flask
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.
Creating a simple application
In this section, 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
Using your web browser or curl command to hit http://127.0.0.1:5000, you will get the “Hello world” text output as shown in the below screenshot.
Conclusion
Flask is a really powerful web framework for any developer. This tutorial went through all the detailed steps of installing Flask on Ubuntu 20.04.
Thank you and feel free to feedback in the below comment section.
System Engineer with 6 years of experience in software development, specializes in Embedded Linux, C/C++, Python, Go, and Shell Scripts. He has a solid background in Computer Networking, OpenStack, Kubernetes, Docker/Container, CI/CD, and Google Cloud as well. Now, he is Head of GDG Cloud Hanoi – a non-profit community of cloud developers who meet/share ideas on everything Google Cloud Platform related.