How to Install Flask on CentOS 7
5 min read
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 EPEL repositories and can be installed using the
yum package manager. This is the easiest method to install Flask on CentOS 7, 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.
Installing Flask on CentOS 7
In the following sections, we’ll provide details on how to install Flask in a Python virtual environment on CentOS 7.
1. Installing Python 3 and venv
We will install Python 3.6 from the Software Collections (SCL) repositories.
CentOS 7 ships with Python 2.7.5 which is a critical part of the CentOS base system. SCL will allow you to install newer versions of python 3.x alongside the default python v2.7.5 so that system tools such as yum will continue to work properly.
Enable SCL by installing the CentOS SCL release file which is included in the CentOS extras repository:
sudo yum install centos-release-scl
Once the repository is enabled install Python 3.6 with the following command:
sudo yum install rh-python36
Once Python 3.6 is installed we are ready to create a virtual environment for our Django application.
2. Creating a Virtual Environment
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.
To access Python 3.6 you need to launch a new shell instance using the
scl enable rh-python36 bash
Create a new directory for your Flask application and navigate into it:
Run the following command to create a new virtual environment:
python3 -m venv venv
The command above 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.
Activate the virtual environment using 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
3. Installing Flask
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.3 (default, Mar 20 2018, 13:50:41) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
Your Flask version may differ from the version shown here.
4. Creating a Minimal Flask Application
In this guide, we will create a simple hello world application that will 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.
- In the first line, we are importing the Flask class.
- Next, we creating an instance of the Flask class.
- Then we use the
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.
5. Testing the Development Server
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:
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.
6. Deactivating the Virtual Environment
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 CentOS 7 machine. To create additional Flask development environments repeat the steps we outlined in this tutorial.
If you are new to Flask, visit the Flask documentation page and learn how to develop your first Flask app.