Share your love
Install and Use Flask on Ubuntu 20.04

In this article from the Orcacore website, we want to teach you how to install and use Flask on Ubuntu 20.04.
Flask is a web framework, it’s a Python module that lets you develop web applications easily. It has a small and easy-to-extend core: it’s a microframework that doesn’t include an ORM (Object Relational Manager) or such features.
It does have many cool features like URL routing and a template engine. It is a WSGI web app framework.
Table of Contents
How To Install and Use Flask on Ubuntu 20.04
The simple way to install Flask on Ubuntu 20.04 is to use the APT package manager. But in this article, you will learn to install Flask inside a Python virtual environment. If you install Flask into the global environment, then you can install only one Flask version on your computer.
Before you start, you need some requirements. Let’s see what we need.
You need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do these, you can follow our article about the Initial Server Setup with Ubuntu 20.04.
1. Install Flask with Python Environment
Ubuntu 20.04 comes with Python 3.8. You can verify that Python is installed on your server by using the following command:
python3 -V
Your output should be similar to this:
Output
Python 3.8.10
To create a virtual environment, you need to install the Python-venv package with the following command:
sudo apt install python3-venv -y
Now navigate to a directory where you want to store the Python 3 virtual environments. It can be your home directory or any other directory where your user has read and write permissions.
Then, create a new directory for Flask on Ubuntu 20.04 and switch to it with the following command:
sudo mkdir flask_app && cd flask_app
Now, inside the directory, run the following command to create the virtual environment:
sudo python3 -m venv venv
You can use any name you want for the virtual environment. Here we use venv.
To start using the virtual environment, you need to activate it with the following script:
sudo source venv/bin/activate
When you have activated the virtual environment, your shell’s prompt will change to the name of the virtual environment:
(venv) $
Now you can install Flask on Ubuntu 20.04 with the Python package manager(pip):
(venv) $ pip install Flask
Note: Within the virtual environment, you can use the command pip
instead of pip3
and python
instead of python3
.
Verify the installation with the following command:
(venv) $ python -m flask --version
In your output, you will see:

Your Flask version may differ from the version shown in this example.
Now that you have Flask installed on your Ubuntu 20.04, let’s see how to use it. We show you how to create a minimal application.
2. How To Use Flask on Ubuntu 20.04?
Here, you need to create a simple Hello World application that will simply print “Hello World!”.
(venv) $ vi ~/flask_app/hello.py
Paste the following code in the file:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
When you are done, save and close the file.
Now you need to tell the shell the application to work with by setting the FLASK_APP environment variable:
(venv) $ export FLASK_APP=hello.py
Then run the application with the flask command on Ubuntu 20.04:
(venv) $ flask run
This command will launch the development built-in server.
Your output should be similar to this:

Note: If you installed Flask on a virtual machine and want to access the Flask development server, you can make the server publicly available by appending --host=0.0.0.0
to the flask run
command.
In your web browser, type http://127.0.0.1:5000, and you will be presented with the “Hello World!” message.
In your terminal, press CTRL+C to stop the development server.
When you are done, type deactivate to return to your normal shell:
(venv) $ deactivate
Conclusion
At this point, you have learned to create a Python virtual environment and install Flask on Ubuntu 20.04. Also, you learned to create a sample Flask development environment.
Hope you enjoy it. You may also like to read the following articles:
Install and Configure ProFTPD Over TLS/SSL on Ubuntu 24.04
Install Pritunl VPN on Ubuntu 24.04