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.
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.
There are different methods to install Flask on Ubuntu.
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.
Virtual environments allow you to create an isolated environment for different Python projects. This way, you can have multiple different Flask environments on a single computer and install a specific version of a module on a per-project basis without worrying that it will affect your other Flask installations. If you install Flask into the global environment, then you can install only one Flask version on your computer.
In this article, we’ll discuss how to install Flask on Ubuntu 20.04 inside a Python virtual environment.
Installing Flask on Ubuntu 20.04
Ubuntu 20.04 ships with Python 3.8. You can verify that Python is installed on your system by typing:
The output should look something like below:
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
Once the module is installed, we are ready to create a virtual environment for the Flask application.
Navigate to the 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.
Create a new directory for the Flask application and switch into it:
mkdir flask_app && cd flask_app
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.
To start using the virtual environment, you need to activate it with the
Once activated, the virtual environment’s bin directory will be added at the beginning of the
$PATH variable. Your shell’s prompt will also change and show the name of the virtual environment you’re currently using. In this example that is
Now that the virtual environment is activated, use the Python package manager
pip to install Flask:
pip install Flask
To verify the installation, run the following command, which prints the Flask version:
python -m flask --version
At the time of writing this article, the latest official Flask version is 1.1.2
Python 3.8.5 Flask 1.1.2 Werkzeug 1.0.1
Your Flask version may differ from the version shown in this example.
Creating a Minimal Application
We will create a simple hello world application that will simply print “Hello World!”.
Open your text editor or Python IDE and create the following file:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!'
Here is what the code does:
- That first line imports the Flask class.
- The second line creates a new instance of the Flask class.
route()decorator is used to register the
hello_worldfunction for the
/route. When this route is requested, the function is called, and the message “Hello World!” is returned to the client.
Save the file as
hello.py and go back to your terminal window.
We’ll use the
flask command to run the application, but before that, we need to tell the shell the application to work with by setting the
FLASK_APP environment variable:
The command above will launch the development builtin server.
The output will look something like the following:
* Serving Flask app "hello.py" * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
http://127.0.0.1:5000 in your web browser , and you will be presented with the “Hello World!” message.
To stop the development server type,
CTRL-C in your terminal.
Once you are done with your work, deactivate the environment by typing
deactivate, and you will return to your normal shell.
We’ve shown you how to create a Python virtual environment and install Flask on your Ubuntu 20.04 machine. To create additional Flask development environments, repeat the same procedure.
If you are new to Flask, visit the Flask documentation page and learn how to develop your first Flask app.
Feel free to leave a comment below.