This article will let you know in detail how to deploy flask app on Heroku. Deploying applications on Heroku is very friendly and straightforward. You can easily deploy your applications by following this guide.
What is Heroku?
Heroku is a cloud-based service that provides a development environment for different languages such as Python, Java, Ruby, Node, Go, Scala and Clojure. Now As we are here to discuss the deployment of the Flask app, before starting the main process here is some introduction to Flask and Deployment.
What is Flask?
Flask is a popular, lightweight micro web framework written in python. It is based on the Werkzeug WSGI toolkit and Jinja2 template engine. Flask provides independent or full control to us for creating applications,
What is Deployment?
Deployment is a way through which we can share our applications with our clients, friends, and anyone on the internet. There are many platforms that offer deployment of our apps to their servers.
I have shared some insights about Flask and Deployment. Now we are diving into the main purpose of the article. Let’s get started!
- A running flask app on the local system
- A requirements.txt file
- A procfile
- Git Repository of the app
- Heroku CLI
- Heroku Account
About the above prerequisites, some of us might get confused about the requirements.txt file, procfile, and Heroku CLI, so let me know what these things are.
This file includes all the dependencies on which our app is running. To generate this file you can simply do “pip freeze” on the terminal where you are running your application, and then you will see something like this:
After that, just copy the libraries and the versions and paste them into a text file, and then name it as “requirements.txt”. Place the file inside your app folder.
Till now, we were using our app on a local server but while deployment we need a professional server. Here comes the role of Gunicorn which is a lightweight WSGI HTTP Server. Place “gunicorn” in the requirements.txt.
To tell Heroku which server it has to use for running our application, we need a Procfile.
Add this line to a text file “web: gunicorn run:app” and save it as Procfile in the same folder where we saved our requirements file.
You have to modify the procfile according to your application. In my case, ‘run’ is the python file that starts the server in my local system.
Here I also need to introduce another type of file that you might need which is runtime.txt
Suppose you have created a flask app that is based on python 3.7 but when you will try to deploy there will be an error because the requirements depend on python 3.7 but Heroku will be using the latest version of python. In the runtime.txt, you can specify your python version also to avoid dependency conflicts. In my case, I’ve added python-3.6.13 in the file.
Heroku CLI is a command line tool provided by Heroku for the deployment of our apps.
Methods to deploy Flask App on Heroku
There are three methods through which we can deploy our apps on Heroku.
- Using Heroku dashboard
- Using Heroku CLI
- Using Container Registry
By using a container registry we can deploy our docker-based app easily. In this article we will be implementing the Dashboard and CLI method below, After checking all the prerequisites Let’s dive into deployment.
1-Deploying The App Using Heroku Dashboard
This method is very simple and easy to use. Login to your Heroku account dashboard. You will see something like this:
Now click on Create new app. Enter the name of the app and click create app.
After that you will get the screen like this:
Select Github, Search your repo and connect it.
By enabling Automatic Deploys your application will be deployed again on every push to the repo. Now click on Deploy Branch.
It will start getting deployed. After some minutes and a successful deployment you will see this output.
After clicking on the view you should see this page.
This page shows there is no error in the application Congrats you have deployed your application easily using Heroku Dashboard.
But wait we are also covering the CLI method so increase your knowledge and don’t stop scrolling.
2-Deploying The App Using Heroku CLI
For using this method we need to install Heroku CLI in our local system. As I am using Linux so it can be installed using this command:
|curl https://cli-assets.heroku.com/install-ubuntu.sh | sh|
For Windows and Mac you can go through this link:
Verify the installation using the command “heroku –version”. After running this command you should see the output similar to this.
Now login to your Heroku account using Heroku CLI. Open the terminal in the folder where your app is and type “heroku login”. This will open the browser and you have to log in to your Heroku account.
As we have already pushed the code to GitHub with the procfile and requirements file. We have to create our app using the command “heroku create -a first-flaskapplication”. Here first-flaskapplication is the application name.
Now you just have to push the code to Heroku remote from your git repository and you can do this by typing “git push heroku master” on the terminal.
This output shows that our application is deployed successfully but to check if there is no error go to url :
You should see the not found web page. Congrats again we have deployed another application using the Heroku CLI method.
These are the things you should know while thinking to deploy your application to Heroku, you are in the process of deploying or you have deployed your application.
The one disadvantage of deploying the application to Heroku is that we cannot deploy large applications. After compression, if the app size is larger than 500 MB so there will be a problem and then Heroku will ask us to make the size smaller of the application.
Sometimes our applications depend on packages or libraries that cannot be installed just with the requirements file so there we need to add build packs to our applications. Some buildpacks can be detected and installed automatically from Heroku and for some, we have to do it manually. In my case, the application depends on FFmpeg and I have added FFmpeg buildpack on my application manually.
The steps to add any build pack to your application are:
Firstly if Heroku has official support for the buildpack you want to use that’s great but if Heroku does not have the support for the build pack, don’t worry just go to this link:
https://elements.heroku.com/buildpacks and search for your desired build pack. There will be many results for your buildpack choose one and open it on Github. Copy that Github link. You can add the buildpack to your application in both ways
- Using Heroku Dashboard
- Using Heroku CLI
Using Heroku Dashboard
To add buildpack go to your app settings and click on add buildpack paste that Github URL and save changes.
Using Heroku CLI
Login to Heroku using the CLI and type this:
|heroku buildpacks:add https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest -a first-flaskapplication|
After adding buildpacks you need to redeploy the code so the buildpacks can start working.
Sometimes after deploying our code and while running our server, it returns exceptions from the code and we need to check why our produces an exception. While running locally we can find all the logs in our terminal but how we can see them on Heroku?
On the application, page click on ‘more’ located at the top right and then go to view logs.
Sometimes you will need to see the directories of your application that is deployed. To do this simply go to and click on the run console. Enter bash and run.
Now you are in your application folder. Type ‘ls’ and you can see all the directories inside your app.
You can type ‘cd folder_name’ to go inside the folder.
In this article, we have learned how to deploy a Flask App on Heroku using two methods easily. I have tried to provide all the necessary information for the deployment including some other information related to console, buildpacks, logs, and slug size. We also found that we cannot deploy larger servers due to slug size. That’s a disadvantage and if you have an application that is larger than the slug size you can deploy it to some other services such as AWS. You can also follow this article https://tezeract.ai/deployment-on-ec2-instance/ which has detailed steps of deployment on AWS.
Hopefully, you guys found this article fruitful. Thank you