How to Install Flask with Python 3 on Ubuntu 18.04

Flask is a very simple, but extremely flexible framework meant to provide your applications with functionality without being too restrictive about structure and design. You can use the general stack described in this article to serve the flask applications that you design.

In this article, we will be setting up a simple Python application using the Flask micro-framework on Ubuntu 18.04.

The bulk of this article will be about how to set up the Gunicorn application server to launch the application and Nginx to act as a front-end reverse proxy.


Before starting on this article, you should have a non-root user configured on your server. This user needs to have sudo privileges so that it can perform administrative functions.

Create non-root user with sudo privileges configured:
1- Logging in as Root
2- Creating a New User

# adduser bobby

3- Granting Administrative Privileges

To add these privileges to our new user, we need to add the new user to the sudo group. By default, on Ubuntu 18.04, users who belong to the sudo group are allowed to use the sudo command.

# usermod -aG sudo bobby

Install the Components from the Ubuntu Repositories

The first step will be to install all of the pieces that we need from the repositories. We will install pip, the Python 3 package manager, in order to install and manage our Python 3 components. We will also get the Python 3 development files needed to build some of the Gunicorn components. We'll install Nginx now as well.
Update your local package index and then install the packages. The specific packages you need will depend on the version of Python you are using for your project.
Installing Python 3, type:

$ sudo apt-get update
$ sudo apt-get install python3-pip python3-dev nginx

Create a Python 3 Virtual Environment

Next, we'll set up a virtual environment in order to isolate our Flask application from the other Python files on the system.
Start by installing the virtualenv package using pip.
If you are using Python 3, type:

sudo pip3 install virtualenv

Now, we can make a parent directory for our Flask project. Move into the directory after you create it:

mkdir ~/flaskproject
cd ~/flaskproject

We can create a virtual environment to store our Flask project's Python requirements by typing:

virtualenv flaskprojectenv

This will install a local copy of Python and pip into a directory called flaskprojectenv within your project directory.
Before we install applications within the virtual environment, we need to activate it. You can do so by typing:

source flaskprojectenv/bin/activate

Your prompt will change to indicate that you are now operating within the virtual environment. It will look something like this (flaskprojectenv)[email protected]:~/flaskproject$.

Set Up a Flask Application

Now that you are in your virtual environment, we can install Flask and Gunicorn and get started on designing our application:

Install Flask and Gunicorn

We can use the local instance of pip to install Flask and Gunicorn. Type the following commands to get these two components:

Note: Regardless of which version of Python you are using, when the virtual environment is activated, you should use the pip command (not pip3).

(flaskprojectenv) $ pip install gunicorn flask

Create a Sample App

Now that we have Flask available, we can create a simple application. Flask is a micro-framework. It does not include many of the tools that more full-featured frameworks might, and exists mainly as a module that you can import into your projects to assist you in initializing a web application.
While your application might be more complex, we'll create our Flask app in a single file, which we will call

(flaskprojectenv) $ nano ~/flaskproject/

Within this file, we'll place our application code. Basically, we need to import flask and instantiate a Flask object. We can use this to define the functions that should be run when a specific route is requested:

from flask import Flask
app = Flask(__name__)

def greeting():
    return "<h1 style='color:green'>Hello World!</h1>"

if __name__ == "__main__":'')

This basically defines what content to present when the root domain is accessed. Save and close the file when you're finished.

If you followed the initial server setup guide, you should have a UFW firewall enabled. In order to test our application, we need to allow access to port 5000.

Open up port 5000 by typing:

(flaskprojectenv) $ sudo ufw allow 5000

Now, you can test your Flask app by typing:

(flaskprojectenv) $ python

Visit your server's domain name or IP address followed by :5000 in your web browser:


You should see something like this:

When you are finished, hit CTRL-C in your terminal window a few times to stop the Flask development server.

Create the WSGI Entry Point

Next, we'll create a file that will serve as the entry point for our application. This will tell our Gunicorn server how to interact with the application.

We will call the file

(flaskprojectenv) $ nano ~/flaskproject/

The file is incredibly simple, we can simply import the Flask instance from our application and then run it:

from flaskproject import app

if __name__ == "__main__":

Save and close the file when you are finished.

Testing Gunicorn's Ability to Serve the Project

Before moving on, we should check that Gunicorn can correctly. We can do this by simply passing it the name of our entry point. This is constructed by the name of the module (minus the .py extension, as usual) plus the name of the callable within the application. In our case, this would be wsgi:app.

We'll also specify the interface and port to bind to so that it will be started on a publicly available interface:

(flaskprojectenv) $ cd ~/flaskproject
(flaskprojectenv) $ gunicorn --bind wsgi:app

Visit your server's domain name or IP address with :5000 appended to the end in your web browser again:


You should see your application's output again:

When you have confirmed that it's functioning properly, press CTRL-C in your terminal window.
We're now done with our virtual environment, so we can deactivate it:

(flaskprojectenv) $ deactivate

Any Python commands will now use the system's Python environment again.

Create a systemd Unit File

The next piece we need to take care of is the systemd service unit file. Creating a systemd unit file will allow Ubuntu's init system to automatically start Gunicorn and serve our Flask application whenever the server boots.

Create a unit file ending in .service within the /etc/systemd/system directory to begin:

$ sudo nano /etc/systemd/system/flaskproject.service

Inside, we'll start with the [Unit] section, which is used to specify metadata and dependencies. We'll put a description of our service here and tell the init system to only start this after the networking target has been reached

Next, we'll open up the [Service] section. We'll specify the user and group that we want the process to run under. We will give our regular user account ownership of the process since it owns all of the relevant files. We'll give group ownership to the www-data group so that Nginx can communicate easily with the Gunicorn processes.

We'll then map out the working directory and set the PATH environmental variable so that the init system knows where our the executables for the process are located (within our virtual environment). We'll then specify the commanded to start the service. Systemd requires that we give the full path to the Gunicorn executable, which is installed within our virtual environment.

We will tell it to start 3 worker processes (adjust this as necessary). We will also tell it to create and bind to a Unix socket file within our project directory called flaskproject.sock. We'll set a umask value of 007 so that the socket file is created giving access to the owner and group, while restricting other access. Finally, we need to pass in the WSGI entry point file name and the Python callable within.

Finally, we'll add an [Install] section. This will tell systemd what to link this service to if we enable it to start at boot. We want this service to start when the regular multi-user system is up and running:

Description=Gunicorn instance to serve flaskproject

ExecStart=/home/bobby/flaskproject/flaskprojectenv/bin/gunicorn --workers 3 --bind unix:flaskproject.sock -m 007 wsgi:app


With that, our systemd service file is complete. Save and close it now.

We can now start the Gunicorn service we created and enable it so that it starts at boot:

$ sudo systemctl start flaskproject
$ sudo systemctl enable flaskproject

Configuring Nginx to Proxy Requests

Our Gunicorn application server should now be up and running, waiting for requests on the socket file in the project directory. We need to configure Nginx to pass web requests to that socket by making some small additions to its configuration file.

Begin by creating a new server block configuration file in Nginx's sites-available directory. We'll simply call this flaskproject to keep in line with the rest of the article:

$ sudo nano /etc/nginx/sites-available/flaskproject

Open up a server block and tell Nginx to listen on the default port 80. We also need to tell it to use this block for requests for our server's domain name or IP address.

The only other thing that we need to add is a location block that matches every request. Within this block, we'll include the proxy_params file that specifies some general proxying parameters that need to be set. We'll then pass the requests to the socket we defined using the proxy_pass directive:

server {
    listen 80;
    server_name server_domain_or_IP;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/bobby/flaskproject/flaskproject.sock;

That's actually all we need to serve our application. Save and close the file when you're finished.

To enable the Nginx server block configuration we've just created, link the file to the sites-enabled directory:

$ sudo ln -s /etc/nginx/sites-available/flaskproject /etc/nginx/sites-enabled

With the file in that directory, we can test for syntax errors by typing:

$ sudo nginx -t

If this returns without indicating any issues, we can restart the Nginx process to read our new config:

$ sudo systemctl restart nginx

The last thing we need to do is adjust our firewall again. We no longer need access through port 5000, so we can remove that rule. We can then allow access to the Nginx server:

$ sudo ufw delete allow 5000
$ sudo ufw allow 'Nginx Full'

You should now be able to go to your server's domain name or IP address in your web browser:


You should see your application's output:

Note: After configuring Nginx, the next step should be securing traffic to the server using SSL/TLS. This is important because without it, all information, including passwords are sent over the network in plain text. The easiest way get an SSL certificate to secure your traffic is using Let's Encrypt.

Read Also

Conclusion: In this article, we've created a simple Flask application within a Python virtual environment. We create a WSGI entry point so that any WSGI-capable application server can interface with it, and then configured the Gunicorn app server to provide this function. Afterward, we created a systemd unit file to automatically launch the application server on boot. We created a Nginx server block that passes web client traffic to the application server, relaying external requests.

Ahmed Abdalhamid 1:03 am

About Ahmed Abdalhamid

Technical Support Engineer experienced in Linux servers administration of production environments. Exploring DevOps culture and tools. Interested in containerization and open source monitoring tools.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.