Free Tier: Install Flask on an Ubuntu VM

In this tutorial, you use an Oracle Cloud Infrastructure Free Tier account to setup an Ubuntu 18.04 virtual machine (VM). Then, you set up a virtual environment for your host. Finally, you install Flask, a lightweight web application framework for Python. Key tasks include how to:

  • Setup ssh encryption keys to access your Ubuntu Server.
  • Setup an Oracle Cloud Infrastructure virtual cloud network (VCN) and related network services required for your host to connect to the internet.
  • Install your Ubuntu 18.04 VM and connect it to your VCN.
  • Set up a virtual environment and install Flask on your VM.
  • Create a Hello World Python application.
  • Test your application.

Here is a simplified diagram of the setup for your Linux VM.

A diagram of the components needed to run a Flask web application framework on Oracle Cloud Infrastructure

For additional information, see:

Before You Begin

To successfully perform this tutorial, you must have the following:

  • An Oracle Cloud Infrastructure Free Tier account. See Oracle Cloud Free Tier.
  • SSH support:
    • A MacOS or Linux computer with ssh support installed.
    • A Windows machine with Git Bash installed. See Git for Windows to download Git Bash.

1. Create SSH Encryption Keys

Create ssh encryption keys you can use to log into your VM.

  1. If you are using MacOS or Linux, open a terminal window in the directory where you want to store your keys, and issue the following OpenSSH command:
    ssh-keygen -t rsa -N "" -b 2048 -C <my-key> -f <my-key>

    The command generates some random text art used to generate the keys. When complete, you should have two files:

    • The private key file: <my-key>
    • The public key file: <my-key>.pub

    You use these files to connect to your Compute instance.

  2. On Windows, right click on the directory where you want to store your keys, select Git Bash Here, and then issue the following OpenSSH command:
    ssh-keygen -t rsa -N "" -b 2048 -C <my-key> -f <my-key>

    The command generates some random text art used to generate the keys. When complete, you should have two files:

    • The private key file: <my-key>
    • The public key file: <my-key>.pub

    You use these files to connect to your Compute instance.

You have generated the required encryption keys.

See Creating a Key Pair for more detailed information on generating ssh encryption keys.

2. Create your Virtual Cloud Network (VCN)

Setup a VCN to connect your Ubuntu instance to the internet. You configure all the components needed to create your virtual network.

  1. From the main landing page, select Set up a network with a wizard. Quick action menu from the main Free Tier landing page
  2. In the Start VCN Wizard workflow, select VCN with Internet Connectivity and then click Start VCN Wizard .
  3. In the configuration dialog, fill in the VCN Name for your VCN. Your Compartment is already set to its default value of <your-tenancy> (root).
  4. In the Configure VCN and Subnets section, keep the default values for the CIDR blocks:

    Notice the public and private subnets have different network addresses.
  5. For DNS RESOLUTION uncheck USE DNS HOSTNAMES IN THIS VCN.Picture shows the USE DNS HOSTNAMES IN THIS VCN option is unchecked.
  6. Click Next.

    The Create a VCN with Internet Connectivity configuration dialog is displayed (not shown here) confirming all the values you just entered.

  7. Click Create to create your VCN.

    The Creating Resources dialog is displayed (not shown here) showing all VCN components being created.

  8. Click View Virtual Cloud Network to view your new VCN.

    Your new VCN is displayed. Now you need to add a security rule to allow HTTP connections on port 5000, the default port for Flask applications.

  9. With your new VCN displayed, click your Public subnet link.

    The public subnet information is displayed with the Security Lists at the bottom of the page. There should be a link to the Default Security List for your VCN.

  10. Click on the Default Security List link.

    The default Ingress Rules for your VCN are displayed.

  11. Click Add Ingress Rules.

    An Add Ingress Rules dialog is displayed.

  12. Fill in the ingress rule with the following information. Once all the data is entered, click Add Ingress Rules

    Fill in the ingress rule as follows:

    • Stateless: Checked
    • Source Type: CIDR
    • Source CIDR:
    • IP Protocol: TCP
    • Source port range: (leave-blank)
    • Destination Port Range: 5000
    • Description: Access Flask applications

    Once you click Add Ingress Rule, HTTP connections are allowed to your public subnet.

You have successfully created a VCN that will make your Python applications created in the Flask framework, available from the internet.

3. Install your Ubuntu VM

Install your Ubuntu VM to host your Flask web application development framework.

  1. From the Oracle Cloud Infrastructure main menu, select Compute, then Instances.
  2. From the list of instances screen click Create Instance.

    The Create Compute Instance dialog is displayed. Notice the Show Shape, Network and Storage Options should be expanded to configure the virtual machine.

  3. Fill in the fields for the Create Compute Instance dialog with the following data:

    Initial Options

    • Name of your Instance: <name-for-the-instance>
    • Image or Operating System (Click Change Image): Canonical Ubuntu 18.04
    • Availability Domain: <Select-a-domain>
    • Instance Shape: VM.Standard.E2.1.Micro: Virtual Machine, 1 core OCPU, 1 GB Memory, 0.48 Gbps network bandwidth (the default)

    Configure Networking

    • VIRTUAL CLOUD NETWORK: <VCN-you-created>
    • SUBNET COMPARTMENT: (take default)
    • SUBNET: <public-subnet-you-created>
    • ASSIGN A PUBLIC IP ADDRESS: Selected/Checked

    Additional Options

    • Boot Volume: All options Unchecked
    • Add SSH Keys: Add the public key file (.pub) you created in part 1 of this tutorial.
  4. Click Create to create the instance.

    Provisioning the system may take several minutes.

You have successfully created an Ubuntu Linux VM to run your Flask application development framework.

4. Install and Set up Flask

Next, install and set up Flask to run on your Ubuntu Linux VM.

By default, Ubuntu 18.04 comes with Python 3.6 out-of-the-box . To setup Flask with Python 3.6, perform the following steps:

  1. From the main menu select Compute then Instances.
  2. Click the link to the instance you created in the previous step.

    From the Instance Details page look in the Instance Access section. Copy the public IP address the system created for you. You will use this IP address to connect to your instance.

  3. Open a Terminal or Command Prompt window.
  4. Change into the directory where you stored the ssh encryption keys you created in part 1.
  5. Connect to your VM with this ssh command
    ssh -i <your-private-key-file> ubuntu@<x.x.x.x>

    Since you identified your public key when you created the VM, this command should log you into your VM. You can now issue sudo commands to install and start your server.

  6. Update firewall settings.

    The Ubuntu firewall is disabled by default. However, it is still necessary to update your iptables configuration to allow HTTP traffic. Execute the following commands to update iptables.

    sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 5000 -j ACCEPT
    sudo netfilter-persistent save

    The commands add a rule to allow HTTP traffic through port 5000 and saves the changes to the iptables configuration files.

  7. Install pip3 for Python 3.
    sudo apt update
    sudo apt install python3-pip

    You may need to type "y" a few times to accept the packages that are installed to the VM.
  8. Next, install and activate a virtual environment plus a virtual environment wrapper.

    You can use a virtual environment to manage the dependencies for your project. Every project can be in its own virtual environment to host independent groups of Python libraries.

    The virtualenvwrapper is an extenstion to virtualenv. It provides a set of commands, which makes working with virtual environments much more pleasant. It also places all your virtual environments in one place. The virtualenvwrapper provides tab-completion on environment names.

    sudo apt install python3-venv
    sudo pip3 install virtualenvwrapper
  9. Set up your virtual environment wrapper in .bashrc.

    Update the file:

    sudo vi .bashrc

    In the file, append the following text and save the file:

    # set up Python env
    export WORKON_HOME=~/envs
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
    export VIRTUALENVWRAPPER_VIRTUALENV_ARGS=' -p /usr/bin/python3 '
    source /usr/local/bin/

    Activate the above commands in the current window.

    source ~/.bashrc
  10. Start a virtual environment.
    mkvirtualenv flask01
    You should see: (flask01) ubuntu@<ubuntu-instance-name>:~$
  11. Install Flask.
    sudo pip3 install Flask
  12. Create a "Hello, World!" application.

    Create the file:

    sudo vi

    In the file, input the following text and save the file:

    from flask import Flask
    app = Flask(__name__)
    def hello_world():
        return 'Hello, World!'
    if __name__ == "__main__":"", port=int("5000"), debug=True)
  13. Run the Python program
    flask run --host=
  14. You can now test your application framework.

    From a new terminal, connect to your Ubuntu VM with your SSH keys, and then in the command line enter curl localhost:5000. Or, you can connect your browser to the public IP address assigned to your VM: http://<x.x.x.x>:5000.

    You should see Hello World! on your VM, or in your browser.
Congratulations! You have successfully installed Flask on your Oracle Cloud Infrastructure VM.

What's Next

You have successfully installed Flask and created a Python application on Oracle Cloud Infrastructure using an Ubuntu VM.

To explore more information about development with Oracle products check out these sites: