The following sections provide information about how to install Flask in a Python virtual environment on Ubuntu 18.04
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, depending on your needs. It can be installed system-wide or in a Python virtual environment using pip. Flask packages are also included in the official Ubuntu repositories and can be installed using the
apt package manager. This is the easiest method to install Flask on Ubuntu 18.04, but not as flexible as installing in a virtual environment. Also, the version included 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 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.
The following sections provide information about how to install Flask in a Python virtual environment on Ubuntu 18.04.
Ubuntu 18.04 ships with Python 3.6 by default. You can verify that Python 3 is installed on your system by typing:
The output should look like this:
Starting from Python 3.6, the recommended way to create a virtual environment is to use the
venv module. To install the
python3-venv package that provides the
venv module run the following command:
sudo apt install python3-venv
Once the module is installed we are ready to create a virtual environment for our Flask application.
Start by navigating to the directory where you would like to store your 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 your Flask application and navigate into it:
mkdir my_flask_app cd my_flask_app
Once inside the directory, run the following command to create your new virtual environment:
python3 -m venv venv
The command above creates 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 this virtual environment, you need to activate it by running the
Once activated, the virtual environment’s bin directory will be added at the beginning of the
$PATH variable. Also your shell’s prompt will change and it will show the name of the virtual environment you’re currently using. In our case that is
Now that the virtual environment is activated, you can use the Python package manager pip to install Flask:
pip install Flask
Verify the installation with the following command which will print the Flask version:
python -m flask --version
At the time of writing this article, 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.
In this guide, we will create a simple hello world application which will just display 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.
route()decorator to register the
hello_worldfunction for the
/route. When this route is requested,
hello_worldis 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 Flask how to load the application by specifying the
FLASK_APP environment variable:
export FLASK_APP=hello flask run
The command above will launch the development builtin server.
The output will look something like the following:
* Serving Flask app "hello" * 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.
You have learned how to create a Python virtual environment and install Flask on your Ubuntu 18.04 machine. To create additional Flask development environments repeat the steps we outlined in this tutorial.
Learn how build a Python application using the Flask microframework on Ubuntu 20.04. Learn how to serve Flask Applications with Gunicorn and Nginx on Ubuntu 20.04. Learn how to set up the Gunicorn application server to run the application and how to configure Nginx to act as a front
In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.
Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).
How to Install Pyenv on Ubuntu 18.04 - What is Pyenv? Pyenv is a fantastic tool for installing and managing multiple Python versions. It enables a developer to quickly gain access to newer
This is our second tutorial in Python Flask, in this tutorial we are going to have Introduction to Flask Templates, so for this Flask looks for the template