Flask is a free, open source micro web framework for Python designed to help developers build secure, scalable, and maintainable web applications. Bottle is based on tool
and used Jinja2
as a template engine.
Unlike Django, Flask doesn’t come with an ORM, form validation, or other functionality provided by third-party libraries by default. Flask was designed with extensions in mind, which are Python packages that add functionality to a Flask application.
There are different methods of installing Flask depending on your needs. It can be installed system-wide or in a virtual Python environment with pip.
Flask packages are also found in the official Ubuntu repositories and can be used with the
apt Package manager. This is the easiest way to install Flask on Ubuntu 18.04, but not as flexible as installing it in a virtual environment. In addition, the version contained in the repositories always lags behind the latest version of Flask.
The main purpose of Python virtual environments is to create an isolated environment for different Python projects. This way you can have several different Flask environments on a single computer and install a specific version of a module per project without having to worry about it affecting your other Flask installations. If you install Flask in the global environment, you can only install one version of Flask on your computer.
Install Flask on Ubuntu 18.04
The following sections provide information on installing Flask in a Python virtual environment on Ubuntu 18.04.
1. Install Python 3 and venv
Ubuntu 18.04 ships with Python 3.6 by default. You can check if Python 3 is installed on your system by typing:
The output should look like this:
Starting with Python 3.6, the recommended way to create a virtual environment is to use the
venv Module. To install the
python3-venv Package that the
venv Execute the following command:
sudo apt install python3-venv
Once the module is installed we can create a virtual environment for our Flask application.
2. Create a virtual environment
First, navigate to the directory where you want to save your Python 3 virtual environments. This can be your home directory or any other directory that your user has read and write permissions to.
Create a new directory for your Flask application and navigate into it:
In the directory, run the following command to create your new virtual environment:
python3 -m venv venv
The above command creates a directory called
venvwhich contains a copy of the Python binary, the Pip package manager, the standard Python library, and other supporting files. You can use any name for the virtual environment.
To use this virtual environment, you need to activate it by using the
After activation, the bin directory of the virtual environment will be at the beginning of the
Variable. Your shell’s prompt will also change to show the name of the virtual environment you are currently using. In our case it is
3. Install Flask
Now that the virtual environment is activated, you can install Flask with the Python package manager pip:
pip install Flask
Verify the installation with the following command that prints the Flask version:
python -m flask --version
At the time of this writing, the latest official Flask version is 1.0.2
Flask 1.0.2 Python 3.6.6 (default, Sep 12 2018, 18:26:19) [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
Your Flask version may differ from the version shown here.
4. Create a minimal flask application
In this tutorial, we’ll create a simple Hello World application that only displays the text “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!'
Let’s analyze the code line by line.
- In the first line we import the Flask class.
- Next, we’ll create an instance of the Flask class.
- Then we use that
route()Decorator to register
hello_worldFunction for the
/Route. When this route is requested,
hello_worldis called and the message “Hello World!” is returned to the customer.
Save file as
hello.py and go back to your terminal window.
5. Testing the development server
We’ll use that
flask Command to run the application, but before that we need to tell Flask how to load the application by
FLASK_APP Environment variable:
The above command starts the integrated development server.
The output looks something like this:
* 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 https://127.0.0.1:5000/ (Press CTRL+C to quit)
https://127.0.0.1:5000 in your web browser and you will be presented with the “Hello World!” Message.
How to stop the development server type
CTRL-C in your terminal.
6. Deactivate the virtual environment
When you’re done with your work, turn off the environment by pressing
deactivate and you go back to your normal shell.
You learned how to create a Python virtual environment and install Flask on your Ubuntu 18.04 computer. To create additional Flask development environments, repeat the steps outlined in this tutorial.
If you are new to Flask, check out the Bottle documentation
Page and learn how to develop your first Flask app.
Feel free to leave a comment below.