In this article, 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.
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 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 first.
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.
How To Install Flask on Ubuntu 20.04
Ubuntu 20.04 comes with Python 3.8. You can verify that Python is installed on your server by using the following command:
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
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 the Flask on Ubuntu 20.04 and switch to it with the following command:
mkdir flask_app && cd flask_app
Now inside the directory run the following command to create the virtual environment:
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:
When you have activated the virtual environment, your shell’s prompt will change to the name of the virtual environment:
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
python instead of
Verify the installation with the following command:
(venv) $ python -m flask --version
In your output you will see:
Output Python 3.8.10 Flask 2.0.2 Werkzeug 2.0.2
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.
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 at 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 flak command on Ubuntu 20.04:
(venv) $ flask run
This command will launch the development built-in server.
Your output should be similar to this:
Output * Serving Flask app 'hello.py' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
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 the deactivate to return to your normal shell:
(venv) $ deactivate
At this point, you learn to create a Python virtual environment and install Flask on Ubuntu 20.04. Also, you learn to create the Flask development environments.
Hope you enjoy it.