Building your first Heroku App with Flask

In this tutorial we will learn how to build a simple Hello World Flask App and deploy to Heroku


Make sure you have Python 2.x installed on the machine. Also, install the Heroku Toolbelt

Also install pip tool, pip is a PyPA recommended tool for installing Python packages. More information to install pip can be found at pip installation.

Install Virtual Environment

Create a folder flask-hw-sample and install a virtualenvironment in it. We are using virtualenv to create the environment and then actuvate it with the command source venv/bin/activiate.


virtualenv is a tool to create isolated Python environments. The problem being addressed is one of dependencies and versions, and indirectly permissions. If you want to install an application and with dependencies. If an application works, any change in its libraries or the versions of those libraries can break the application.

It is also useful f you can’t install packages into the global site-packages directory, for example, on a shared host.

In all these cases, virtualenv can help you. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

$ mkdir flask-hw-sample
$ cd flask-hw-sample
$ virtualenv venv
$ source venv/bin/activate

Install Dependencies

Our application need flask and guncorn libraries which can be installed using pip.

$ pip install flask gunicorn

Creating a Simple Flask App

  1. First Create a base Flask app with simple REST endpoint/ in a file in the folder created above.

    from flask import Flask
    app = Flask(__name__)
    def hello_world():
        return 'Hello World!'
    if __name__ == '__main__':
  2. Run the app using the following command

    $ python

    Your app should now be running on localhost:5000

Initialize git

Initialize the git repository as shown by commands below.(We are assuming you have git already installed on the machine)

$ git init
$ git add .
$ git commit -m "initial commit"

Create a Requirements File

Requirements file is needed for heroku to detect that this a flask app and also install appropriate python packages, flask and gunicorn in our case.

$ pip freeze > requirements.txt

Create a Procfile

Create a file name Procfile in the root of the app and add following content. This specifies that the app uses a web dyno with gunicorn as http server.

web: gunicorn app:app --log-file -

Deploying to Heroku

Deployement is being done in three commands listed below.

1. Create a heroku app, this create a git remote with name heroku 2 Do a git push to heroku remote created by the command above. 3. Open the app in a browser with command verb|$ heroku open|

$ heroku create rd-flask-hw
$ git push heroku master
$ heroku open


In this tutorial we learnt how to create a simple Python Flask app and deploy it on Heroku.