How to Install Flask on Ubuntu 20.04?

Python programming is very popular among Ubuntu users and that is why the system includes it in its repositories. In addition to this, it is possible to install almost any Python development tool on Ubuntu. This also includes the programming of various web applications. That is why, if you want to use Python for your web applications then you have to know how to install Flask on Ubuntu and that is precisely what you will learn with this post.

What is Flask?

On the Flask website, we find the following definition:

Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks.

So with Flask, we can start creating web applications with Python and take care only of the code and design of our app.

Install Flask on Ubuntu 20.04

To install Flask it is best to install it in a Python virtual environment. Although this may sound strange, the reality is that it is quite simple to do.

First, make sure your system is completely up to date:

sudo apt update
sudo apt upgrade

After that, you need to install the python3-venv package whose main function is to allow you to create Python virtual environments.

Then, you have to create the folder where your project will be hosted. Also, you can choose the location you like. In this case, I will create a folder called example.

mkdir example

And access the folder:

cd example

Now it is time to create the virtual environment by running

python3 -m venv venv

This creates a folder called venv that contains a binary that will allow us to activate the virtual environment. To do this, just run:

source venv/bin/activate

At this point, you will notice how the prompt changes, adding at the beginning something similar to this

(venv)

Now that we are in the virtual environment, we can use PIP the package manager for Python to install Flask.

pip install Flask

You can verify the installation of Flask, showing the installed version.

python -m flask --version

Sample Output:

Python 3.8.5
Flask 1.1.2
Werkzeug 1.0.1

Checking the Flask installation on Ubuntu 20.04

The best way to know if the installation has been successful is to create a new file and add some code and run it. So do it with a text editor or your favorite IDE.

And add the following content:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World! welcome to imaginelinux.com'


export FLASK_APP=hello.py

Of course, this is a pretty basic test file and only includes the ‘Hello World’ but it should be enough.

To start the code execution you have to run

flask run

And you will get a screen output similar to this:

 * Serving Flask app "hello.py"
 * 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)

It tells you that you have to access from 127.0.0.1.1:5000 in your web browser. However, if you have installed Flask on a server or virtual machine you will have to use the --host option.

flask run --host=0.0.0.0.0

Now open your browser and go to http://127.0.0.1:5000 if you have installed Flask on a local machine or http://your-ip-server:5000 if you have installed it on a virtual machine or server.

If everything went well, you should see a screen similar to this one:

When you are done working, disable the virtual environment by running:

deactivate

This way you will have successfully installed Flask on Ubuntu 20.04.

Conclusion

In Ubuntu 20.04 we have at our disposal many tools for programming and knowing how to install them is an initial step to create applications in the language we want. In this post, you have learned how to install Flask on Ubuntu 20.04 which is a microframework for web application development using Python. The process is quick and easy and is within the reach of any user.

Scroll to Top