Heroku is a cloud platform as a service supporting several programming languages where a user can deploy, manage and scale their applications. It is widely used to deploy server-based web applications, APIs, discord bots, and more. So today in this tutorial, we’ll be deploying a simple flask app to Heroku from start, and learn how it works.
Ways to deploy apps on Heroku
- Using Heroku CLI (command line interface)
- Using GitHub
In this specific tutorial, we’ll be deploying our app through GitHub.
Steps to deploy on Heroku:
Step 1: Create a free account on Heroku. Just go to this link and fill in your credentials to get a new account working.
Step 2: Create a new app on Heroku from the dashboard. Create a new app and choose the name according to your app, and select the region of the server.
Note: The name must be available to be taken, or you can modify it a bit.
Step 3: Create a new repository on GitHub. Go to git and create a new repository against your account. It can be public or private too.
Step 4: Run your app locally once and check if everything’s alright. In this case, its a flask app, which can easily be run by
python app.py
Step 5: Add gunicorn package and update your requirements.txt file
Gunicorn is a Python HTTP server that makes your python applications more efficient by allowing multiple python processes to run concurrently, Hence, it is highly advised to install it as well. Doing that is very easy through pip. Also, a requirements.txt file is important for Heroku to identify what packages you require. So, create it through the command given below. Note: make sure you’re working inside a python virtual environment.
pip install gunicorn pip freeze > requirements.txt
6. Add Procfile
Procfile is nothing but just a text file that is used by Heroku to know which process is to be run on startup. The file should be present in the root directory and its name should be Procfile without any extension. For flaskthe app, the content is
web: gunicorn app:app
You can find more about it here (https://devcenter.heroku.com/articles/procfile#procfile-format)
7. Push the code to GitHub
git add . git remote add origin <your repository link> git commit -m "commit message" git push origin master
8. Connect your Heroku app to GitHub, from app dashboard
From this option, you’ll need to authorize your GitHub if you’re doing it for the first time. Then, just search for your repository, that you want to deploy and select it.
9. Select and click on deploy branch, and see it being deployed!
This step is pretty straightforward.