Cyberithub

8 Simple and Easy Steps to Install Flask on Ubuntu 20.04

Advertisements

In this article, I will take you through 8 Simple and Easy Steps to Install Flask on Ubuntu 20.04. Flask is a python based web framework designed to work without any dependencies on external tools and libraries. Basically, it has its own set of tools and libraries which allows you to develop web applications easily. Flask is based on the Werkzeg WSGI toolkit and the Jinja2 template engine. More on Flask docs.

8 Simple and Easy Steps to Install Flask on Ubuntu 20.04

Steps to Install Flask on Ubuntu 20.04

Also Read: 5 Easy Steps to Install Chromium Browser on Ubuntu 20.04

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 Server.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get utility installed in your Server.

Step 2: Update Your System

It is highly recommended to first sync all the installed packages with the latest stable updates from ubuntu repo using apt update command. This will keep your server updated with the latest available releases.

root@localhost:~# apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:5 https://packages.microsoft.com/repos/edge stable InRelease
Get:6 http://ppa.launchpad.net/micahflee/ppa/ubuntu focal InRelease [17.5 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,026 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [283 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [779 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [330 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [2,468 B]

 

Step 3: Check Your Python Version

Python3 comes by default with Ubuntu 20.04. So to quickly check the python3 version, you can use python3 -V command as shown below.

root@localhost:~# python3 -V
Python 3.8.5

 

Step 4: Install Virtual Environment Tools

In the next step, you need to install Python based virtual environment tools for flask to run in a sandbox environment. You can simply use apt install python3-venv command to install the tool.

root@localhost:~# apt install python3-venv
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
libatomic1
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
libpython3.8 libpython3.8-minimal libpython3.8-stdlib python-pip-whl python3-distutils python3.8 python3.8-minimal python3.8-venv
Suggested packages:
python3.8-doc binutils binfmt-support
The following NEW packages will be installed:
python-pip-whl python3-distutils python3-venv python3.8-venv
The following packages will be upgraded:
libpython3.8 libpython3.8-minimal libpython3.8-stdlib python3.8 python3.8-minimal
5 upgraded, 4 newly installed, 0 to remove and 156 not upgraded.
Need to get 1,953 kB/8,234 kB of archives.
After this operation, 3,742 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

 

Step 5: Create Flask App Directory

You need to create and setup flask application directory where you will keep all your application files and other required tools. Let's create a directory flask-app and switch to it by using mkdir flash-app && cd flask-app command as shown below.

root@localhost:~# mkdir flask-app && cd flask-app
root@localhost:~/flask-app#

 

Step 6: Create a Virtual Environment

Under the flask-app directory, you need to create a virtual environment using python3 -m venv venv command. This command will create a directory called venv which will have the python binaries and libraries required to run the virtual environment.

root@localhost:~/flask-app# python3 -m venv venv

Then to start using this virtual environment, you need to activate it first by running source venv/bin/activate command.

root@localhost:~/flask-app# source venv/bin/activate
(venv) root@localhost:~/flask-app#

 

Step 7: Install Flask

Once virtual environment is created and activated, it is now time to install flask package by using pip install flask command as shown below.

(venv) root@localhost:~/flask-app# pip install flask
Collecting flask
Downloading Flask-2.0.1-py3-none-any.whl (94 kB)
|████████████████████████████████| 94 kB 215 kB/s
Collecting click>=7.1.2
Downloading click-8.0.1-py3-none-any.whl (97 kB)
|████████████████████████████████| 97 kB 2.9 MB/s
Collecting itsdangerous>=2.0
Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting Werkzeug>=2.0
Downloading Werkzeug-2.0.1-py3-none-any.whl (288 kB)
|████████████████████████████████| 288 kB 5.0 MB/s
Collecting Jinja2>=3.0
Downloading Jinja2-3.0.1-py3-none-any.whl (133 kB)
|████████████████████████████████| 133 kB 12.7 MB/s

 

Step 8: Create a Sample Application

Now that Flask has been installed successfully, let's test this by creating a small sample application. We will write below sample code in our flaskapp.py file. In this application we are first importing Flask Class using from flask import Flask. Then creating an instance of this class using app = Flask(__name__).

In the next line, we are registering sample_app() function for the ('/') route using @app.route('/'). Whenever a request comes for ('/') route, sample_app() will run and return the output 'Hi there!!'.

(venv) root@localhost:~/flask-app# vi flaskapp.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def sample_app():
return 'Hi there!!'

Now set this python file to FLASK_APP environment variable to tell the shell to look for the file set in this variable during flask run.

(venv) root@localhost:~/flask-app# export FLASK_APP=flaskapp.py

Then run the app by using flash run command. It will start the application in a built-in web server on localhost 127.0.0.1, listening for requests on default port 5000. If you open http://127.0.0.1:5000/ URL on your browser, you will see the output 'Hi there!!'

NOTE:

If you are having an active firewall, then you might need to allow Port 5000 from it to access the URL in your browser.
(venv) root@localhost:~/flask-app# flask run
* Serving Flask app 'flaskapp.py' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

You can Press Ctrl+C to exit from the above web server and return back to terminal. Once you are done with the app, you can again deactivate the virtual environment by using deactivate command as shown below.

(venv) root@localhost:~/flask-app# deactivate
root@localhost:~/flask-app#

Leave a Comment