How to install Flask on Ubuntu 22.04

To Install Flask On Ubuntu 22.04

Introduction

Flask is a free and open-source microweb 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.

Procedure

Step 1: Check the OS version by using the below command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:    22.04
Codename:    jammy

Step 2: Now check the version of the python by using the below command

root@linuxhelp:~# python3 --version
Python 3.10.12

Step 3: Check the version of the pip by using the below command

root@linuxhelp:~# pip -V
Command 'pip' not found, but can be installed with:
apt install python3-pip

Step 4: If pip is not installed then Install it by using the below command

root@linuxhelp:~# apt install python3-pip -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libflashrom1 libftdi1-2 libllvm13
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu build-essential dpkg-dev fakeroot g++ g++-11 gcc
  gcc-11 javascript-common libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl
  libasan6 libbinutils libc-dev-bin libc-devtools libc6 libc6-dbg libc6-dev libcc1-0 libcrypt-dev
  libctf-nobfd0 libctf0 libdpkg-perl libexpat1-dev libfakeroot libfile-fcntllock-perl libgcc-11-dev libitm1
  libjs-jquery libjs-sphinxdoc libjs-underscore liblsan0 libnsl-dev libpython3-dev libpython3.10-dev
Get:1 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libc6-dbg amd64 2.35-0ubuntu3.4 [13.9 MB]
Get:2 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libc6 amd64 2.35-0ubuntu3.4 [3,234 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 binutils-common amd64 2.38-4ubuntu2.3 [222 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libbinutils amd64 2.38-4ubuntu2.3 [662 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libctf-nobfd0 amd64 2.38-4ubuntu2.3 [107 kB]
Setting up build-essential (12.9ubuntu3) ...
Setting up libpython3-dev:amd64 (3.10.6-1~22.04) ...
Setting up python3-dev (3.10.6-1~22.04) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...

Step 5: After the installation, check the pip version again by using the below command

root@linuxhelp:~# pip -V
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

Step 6: Install the Virtualenv package by using the below command

root@linuxhelp:~# apt install python3-venv
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libflashrom1 libftdi1-2 libllvm13
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  python3-pip-whl python3-setuptools-whl python3.10-venv
The following NEW packages will be installed:
  python3-pip-whl python3-setuptools-whl python3-venv python3.10-venv
0 upgraded, 4 newly installed, 0 to remove and 97 not upgraded.
Need to get 2,474 kB of archives.
After this operation, 2,888 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-pip-whl all 22.0.2+dfsg-1ubuntu0.3 [1,679 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-setuptools-whl all 59.6.0-1.2ubuntu0.22.04.1 [788 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3.10-venv amd64 3.10.12-1~22.04.2 [5,724 B]
Get:4 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 python3-venv amd64 3.10.6-1~22.04 [1,038 B]
Fetched 2,474 kB in 3s (897 kB/s)        
Setting up python3-venv (3.10.6-1~22.04) ...

Step 7: Create a new python virtual environment by using the below command

root@linuxhelp:~# python3 -m venv testenv

Step 8: Once the virtual environment is created, run the below command to activate it.

root@linuxhelp:~# source testenv/bin/activate

Step 9: Now install Flask by using pip install command

(testenv) root@linuxhelp:~# pip install flask
Collecting flask
  Downloading flask-3.0.0-py3-none-any.whl (99 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 99.7/99.7 KB 1.1 MB/s eta 0:00:00
Collecting itsdangerous>=2.1.2
  Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting click>=8.1.3
  Downloading click-8.1.7-py3-none-any.whl (97 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.9/97.9 KB 7.5 MB/s eta 0:00:00
Collecting blinker>=1.6.2
  Downloading blinker-1.6.3-py3-none-any.whl (13 kB)
Collecting Werkzeug>=3.0.0
  Downloading werkzeug-3.0.1-py3-none-any.whl (226 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 226.7/226.7 KB 5.0 MB/s eta 0:00:00
Collecting Jinja2>=3.1.2
  Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.1/133.1 KB 7.0 MB/s eta 0:00:00
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Installing collected packages: MarkupSafe, itsdangerous, click, blinker, Werkzeug, Jinja2, flask
Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.3 Werkzeug-3.0.1 blinker-1.6.3 click-8.1.7 flask-3.0.0 itsdangerous-2.1.2

Step 10: After the installation check the version of the flask by using the below command

(testenv) root@linuxhelp:~# flask --version
Python 3.10.12
Flask 3.0.0
Werkzeug 3.0.1

Step 11: Now create a simple Python code file that will import Flash to print a simple Hello message by using the below command.

(testenv) root@linuxhelp:~# vim flasktest.py
Add the lines in the python file
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello, Flask!'
if __name__ == '__main__':
    app.run()

Step 12: Run the file by using the python command.

(testenv) root@linuxhelp:~# python3 flasktest.py
 * Serving Flask app 'flasktest'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
127.0.0.1 - - [01/Nov/2023 08:03:11] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [01/Nov/2023 08:03:12] "GET /favicon.ico HTTP/1.1" 404 –

Now go to the browser and search the following link http:/127.0.0.1:5000

You will see the above output in your browser

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to install Flask on Ubuntu 22.04. Your feedback is much welcome.

FAQ
Q
How to install Flask Sijax?
A
Install Flask Sijax by using the following command pip install flask-sijax
Q
What HTTP methods does Python Flask provide?
A
GET, POST, PUT, DELETE, HEAD
Q
Why do we use Flask(__name__) in Flask?
A
The __name__ parameter is a Python built-in variable that is set to the name of the current module.
Q
What is the default host port and port of Flask?
A
The default local host of the flask is 127.0.0.1, and the default port is 5000.
Q
What is Flask and why it is used?
A
Flask is a microweb framework that provides an API to build up web applications.