This article serves as a guide for my students to deploy their Flask apps to Heroku. I will assume that you are using Terminal on Mac/Linux or Git Bash on Windows. There is an accompanying GitHub repo for this article.
- Install the Heroku CLI
- In terminal, run
heroku login -iand login
We will start off with a new project directory (folder) called
my_app. In this folder, create a new file,
app.py which will house our Flask app. The code for the Flask app should look something like this:
from flask import Flask, render_template
import gunicornapp = Flask(__name__)@app.route('/')
Your index.html file will go in the
templates/ folder in your project directory. Let’s create a basic Hello, World! page for our website.
- Navigate to your project directory
- Create a new file in the project directory and name it
Procfile— Note: the Procfile does not have a file extension
- open the Procfile with a text editor (ex: Visual Studio Code)
web: gunicorn app:appand save the file
Alternatively, if you are feeling savvy, we can do this with just one command:
echo "web: gunicorn app:app" > Procfile
This will tell Heroku that we would like to use gunicorn to run a function called app inside of the file
There are many ways to create a requirements.txt file. Since many students are often using Anaconda and are avoiding virtual environments, I recommend using the pipreqs module.
To use this module, run the following commands in your Terminal:
pip install pipreqspipreqs .
pipreqs will automatically find out which libraries you have imported and creates a
requirements.txt file in your current directory.
The file structure of your project directory,
my_app, should now look like this:
Deploying to Heroku
We’re going to deploy to Heroku from the command line. Deploying is very similar to pushing to GitHub, if you are comfortable with that process, this will be a breeze! Follow along and type the following in your Terminal:
- initialize your repository
2. Now that we’ve initialized this working directory as a git repository, we can create a new Heroku project. Run the following:
Heroku will initialize the project with a random name, if you would like to use a custom name, like my-app, you can write
heroku create my-app, but names must not be taken by another user already.
3. Finally, add your files to git, commit your changes and push to the Heroku repository
git add .git commit -m "ready for Heroku"git push heroku master
If this was successful you should see something similar to the following:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote: -----> Compressing...
remote: Done: 45M
remote: -----> Launching...
remote: Released v3
remote: https://fierce-reaches-15276.herokuapp.com/ deployed to Heroku
remote: Verifying deploy... done.
You can now visit your web page and verify that it says Hello, World! Make sure to use the url provided on the command line, similar to https://fierce-reaches-15276.herokuapp.com/ in this case.
If your app is not working, check the logs. Run the following in Terminal:
heroku logs --tail
We’ve deployed a Flask app to Heroku and learned the basic deployment requirements along the way. We must make sure we have a Procfile and requirements.txt file for a successful deployment. We can then use the Heroku CLI to create a new project with
heroku create and push our work to a Heroku repository, a process similar to a GitHub push.
Please feel free to visit the GitHub repository accompanying this article if you have any questions, or feel free to leave them below. Most importantly, have fun deploying your apps!