How to install Flask on Fedora Linux?


Flask is an open-source micro web framework that is written in Python. It can be used to develop secure, scalable, and easily maintainable web applications. It is classified as a micro web framework because it doesn’t include ORM, form validation, or any other functionality which is provided by third-party libraries.

However, you can add functionalities such as ORM, form validation, upload handling, etc by using extensions.

In this article, I will discuss how to install Flask on Fedora Linux.

Prerequisites

You should have root permissions to install a package on your Fedora system.

Installing Python in Fedora

By default, Fedora comes preinstalled with Python 3.x but if it is not in your system then use the given command to install it.

First, check update and upgrade packages on your system by using –

sudo dnf update -y

To install the latest version of python3 from the Fedora repository use –

sudo dnf install python3 -y

After installing Python3 you can check its version by using –

python3 -V

python version

Creating Python virtual environment

The Flask is available in Fedora’s official repository you can use the dnf package manager to download it this is one of the simplest ways but you may not get the latest software.

So the recommended method is to create a virtual environment and then install Flask in it. In this way, you can have multiple Flask environments on a single system.

In Python, a virtual environment can be created using the venv module which is available in python3-venv package, use the given command to install it –

sudo dnf install python3-virtualenv

Once it gets installed. Create a directory for the flask application and move to it by using –

mkdir MyFlaskApps && cd MyFlaskApps

Use the given command to create a virtual environment inside this directory –

python3 -m venv project_venv

Now use the given command to activate the created environment –

source project_venv/bin/activate

This will display the output as given in the image below.

Install Flask using pip

Once the virtual environment gets activated you will see that project_venv is added at the beginning of your shell prompt.

Now use the given command to install the flask on your Fedora system.

pip install Flask

flask installation

Note- Please note that you can use pip instead of pip3 and python instead of python3 in a Python virtual environment

You can verify the flask installation by using the given command.

python -m flask --version

You can see the output in the image below.

flask version

Running the first Flask application

Run the given command to open a text editor –

nano flask_app.py

Copy the given code and save it in a file with .py extension. This code shows a simple flask application.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def first_app():
            return 'Hello, this is my first Flask app'

The code description is given below.

  • The first line imports the Flask class in Python
  • The second line creates a new instance of the Flask class
  • Here route() is the decorator which will be used to register the first_app() function. Now when you request this route it will display the text Hello, this is my first Flask app.

Use the given commands to run this application.

export FLASK_APP=first_app.py
flask run

This will display the given output in your terminal.

flask run

Now open a browser and type http://127.0.0.1:5000/ and press enter.

This will display the given output in the browser.

hello world

To stop this press ctrl+c in the terminal.

After you are done with your work deactivate the virtual environment by typing the given command in your terminal.

deactivate

Conclusion

I hope you understand how to install and use Flask in Fedora Linux. For more information, you can check the Flask official documentation.

Now if you have a query or feedback then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.