Installing Flask on Ubuntu 22.04 or 20.04 LTS Linux

Introduction: A step-by-step guide for installing Flask Python web framework on Ubuntu Linux systems.

Flask is for web developers as it provides a micro web framework based on Python. It is popular because of its simplicity, flexibility and does not require particular tools or libraries. Furthermore, this web framework has no database abstraction layer. So, if you are into web development and want to use it then this tutorial is going to give you the instruction to set up and use it.

Prerequisites:

To follow this tutorial you need a Ubuntu system with sudo rights, Python, and access to an internet connection.

Step 1: Check the Version of Python

Almost all the latest Ubuntu operating systems including the 22.04 and 20.04 come with pre-installed Python version 3. So. let’s check Python and its package manager PIP is on our system…

To check the version:

python3 --version

For PIP

pip -V

If there is no output for the above two commands then they are not on your system. In that case, use the given commands:

To install python3:

sudo apt install python3

To install PIP:

sudo apt install python3-pip

If you don’t want to install Python packages globally then can create a Virtual environment which is a best practice for isolating Python packages and dependencies. So, those who want to set up a virtual environment can follow these steps otherwise move to the next one:

Install the Virtualenv package:

sudo apt-get install python3-venv

Use the given command and create a new Python virtual environment:  

python3 -m venv testenv

Once the virtual environment is created, to activate it run:

source testenv/bin/activate

Note: testenv is our environment name you can give something else if you want:

Step 3: Install Flask on Ubuntu 22.04 | 20.04

Next, whether you are using a virtual environment or not, the command of the PIP package manager to install Flask will be the same for both.

pip install flask

If you are not using VEnv then run:

pip install flask --user

Wait for a few seconds, PIP will download and install all the required dependencies to use Flask on Ubuntu or any other Linux system.

Installing Flask on Ubuntu Linux

Step 4: Verify the Flask Installation

By following the previous steps you would already have the Flask on your Ubuntu Linux but to confirm it, we can check its version, here is the command for that:

flask --version

Step 5: Example to use Flask

Let’s create a simple Python code file that will import Flash to print a simple Hello message in our browser. For that on your Ubuntu use any text editor to create a python file.

For example, let’s create a file named – flasktest.py

nano flasktest.py

First, we import the Flask module using “from flask import Flask” and after that, we will add the rest code to print the “hello” message.

Here is the code, just paste it into your file.

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, Flask!'

if __name__ == '__main__':
    app.run()

Save the file by pressing Ctrl+X, typing Y, and hitting the Enter key.

Now, use the Python command to run the created file:

python3 flasktest.py
Example to use Flask Ubuntu 22.04

You will see a local address- http://127.0.0.1:5000 on your command terminal because the Flask will start a local development server.

Local web server started

Now, open the address in your browser and you will see a message – “Hello, Flask!”.

Python Flash web browser example code

This was one of the quickest ways available to install Flash on Ubuntu Linux system. By following this guide you will be able to not only use Flask but also how to create a virtual environment to install Python libraries in an isolated environment.

Other Articles:

Leave a Comment

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