Deploy Flask Application on Heroku

Posted on Oct 18, 2013

Heroku is one of the popular cloud hosting service. It can be used to deploy the applications of Python, Php, Ruby, Node.js and many more online. This blog will teach you how to deploy the simple python web applications on heroku step by step. I will use Flask web framework for this blog. It requires the use of some tools and some modules along with basic knowledge of git commands. So here we go:

Before procedding make sure that GIT is installed on your machine. If its not then you must setup and install it first. Once git is ready on the machine, Create an account on Heroku's official website. It's free and quick. After the setup, we need to install some modules on local machine. Firstly, we need to install heroku tool belt - command line support for heroku. Download and install heroku toolbelt from here. Next, we need to install few python modules using pip Download the latest version of python's setup tools from here . Also install pip from here.

We will use python's virtual environment for the deployment purposes. After installation of setup tools you will see a folder named venv in the downloaded directory. CD to venv folder and activate the venv to launch virtual environment using entering this command in cmd:

activate.bat

This will prompt a venv shell which is nothing but a virtual environment. In Venv shell Cd to your app directory and add a procfile to it. It\'s a text file which contains information about your app.sample Procfile for flask app:

web: gunicorn hello:app

Also add requirements.txt file in your repository using this command.

Pip freeze > requirements.txt

Now all prerequisites are completed and its time to git init in that folder. Add all your files for tracking and commit your files using.

git init
git add .
git commit -m "your commit message"

In the command prompt login into your heroku account using following commands. After successful authentication create a new app in the heroku app dashboard and finally push your app to cloud.

heroku login
heroku create
git push heroku master

Make sure you have added ssh keys to heroku account, otherwise you will get error.
Trobuleshooting for errors related to ssh keys:

First, Check for the existing ssh keys on your machine. In git bash cd to your .ssh folder (usually located in users/ in c drive). Check the folder to see if you have a file named either id_rsa.pub or id_dsa.pub If not then generate a new SSH key, use the command

Ssh-keygen-t rsa -c "youremal@example.com"

Now you will be prompted to to enter a passphrase. Its just like a password. After key successfully generated you need to add them to heroku account, use following command

Heroku keys:add

And we are done. I Hope that this blog will help you deploy your app on heroku cloud. Feel free to contact :)