How to Install Flask in Ubuntu

Flask is a popular web framework to build python-based websites and web applications. In this article, we will look at how to install Flask in Ubuntu. You can install Flask as a system-wide package or within a virtual environment. You can install it using Ubuntu’s apt package manager or using pip utility.

How to Install Flask in Ubuntu

Here are the steps to install flask in Ubuntu. We will be installing it system-wide. If you want to install Flask within a virtual environment then here are the steps to install virtualenv in python. After you have activated your virtual environment, you can proceed as follows. If you don’t want to use a virtual environment, you can directly proceed further.

1. Update system packages

Open terminal and run the following command to update system packages.

$ sudo apt-get update
$ sudo apt-get upgrade -y

Also read : How to remove .php from URL in NGINX

2. Install Flask

Run the following commands to first install pip, and then use pip tool to install Flask.

python 2.x

$ sudo apt-get install python-pip
$ sudo apt-get install python-flask

python 3.x

$ sudo apt-get install python-pip3
$ sudo apt-get install python3-flask

Also read : How to Convert List to String in Python

3. Create Simple Flask Application for testing

We will create a simple web application in flask that responds with “Hello World” message when user opens the home page URL. Create the following directory to store your flask application

$ sudo mkdir /var/www/application
$ cd /var/www/application

Create a file to hold the application.

$ sudo vi

Add the following lines to set up website home page

from flask import Flask
app = Flask(__name__)
def home():
    return "<h1>Hello World</h1>"

In the code, first we import Flask class. Then we create an instance of Flask class. Next we create a URL handler to handle home URL (/) using route decorator. When users request this URL, they will receive a “Hello World” response.

Also read : How to Run Scripts on Startup in Ubuntu

4. Test Flask Application

Run the following two commands to run your flask application.

$ export
$ flask run

This will run Flask’s built-in server at port 5000 by default. You will see the following output.

* Serving Flask app ""  
* Environment: production    WARNING: Do not use the development server in a production environment.    Use a production WSGI server instead.  
* Debug mode: off  
* Running on (Press CTRL+C to quit)

Please note this is a lightweight server meant for testing & development purposes. Don’t use it for production websites.

Open web browser and go to You will see “Hello World” message.

Press Ctrl+C to close the Flask server.

That’s it. As you can see it is very easy to install flask and create a simple application in it.

Also read : How to Use NGINX with Flask