Site icon revealtheme.com

How to Deploy Flask App on Heroku

How To Deploy Flask App On Heroku

How To Deploy Flask App On Heroku

How to Deploy Flask App on Heroku

Deploying a Flask app on Heroku involves packaging your application with a Gunicorn web server, specifying dependencies in requirements.txt, and defining the start command in a Procfile. After initializing a Git repository and creating a Heroku app, simply push your codebase to Heroku’s Git remote, allowing their buildpacks to containerize and deploy your application automatically onto dynos.

Metric Details
Complexity (Initial Setup) Low to Moderate (Requires CLI familiarity, ~15-30 min)
Complexity (Maintenance) Low (Automated deployments, easy scaling)
Platform Support Heroku-20, Heroku-22 stacks (Ubuntu-based); Official Python Buildpack supporting Python 3.7+ (currently 3.9, 3.10, 3.11, 3.12 recommended).
Memory Footprint (Free Dyno) 512 MB RAM (shared, prone to cold starts/sleep after 30 mins inactivity)
Memory Footprint (Hobby Dyno) 512 MB RAM (dedicated, no cold starts, always on)
Typical Flask App Size ~50-150 MB (Python interpreter, Flask, Gunicorn, dependencies, app code)
Deployment Time 1-5 minutes (initial buildpack download & slug compilation), subsequent pushes ~30-90 seconds.

The “Senior Dev” Hook

In my experience, one of the most common pitfalls junior developers encounter when deploying a Flask application to Heroku is underestimating the importance of a correct Procfile and ensuring all dependencies are pinned in requirements.txt. I’ve personally wasted hours troubleshooting “H10 App Crashed” errors only to find out it was a missing Gunicorn entry or an unpinned dependency causing a version mismatch in Heroku’s build environment. Get those two right, and half your battle is won.

Under the Hood Logic: How Heroku Deploys Flask

When you push your code to Heroku, it doesn’t just run your Python script directly. Heroku operates on a “slug compiler” model. Here’s the sequence:

  1. Git Push Trigger: Your git push heroku main command initiates the deployment. Heroku’s Git remote receives your code.
  2. Buildpack Detection: Heroku inspects your repository for specific files. For Python, it looks for requirements.txt (or Pipfile/pyproject.toml with Poetry/Pipenv). Upon detection, the Heroku Python Buildpack is activated.
  3. Dependency Installation: The buildpack sets up a compatible Python runtime and then uses Pip to install all dependencies listed in your requirements.txt into a isolated virtual environment within the build slug.
  4. Slug Compilation: Your application code, along with its dependencies and the Python runtime, are bundled into a “slug” – a compressed, optimized package ready for execution.
  5. Dyno Formation: Once the slug is compiled, Heroku provisions one or more “dynos” – lightweight Linux containers. Each dyno is an isolated environment where your slug is unpacked.
  6. Procfile Execution: Heroku then looks for a Procfile in the root of your project. This file defines the commands Heroku should run to start your application’s processes. For a web application, it typically specifies how to start a web server like Gunicorn, pointing it to your Flask app.
  7. Application Startup: The command from your Procfile is executed, starting Gunicorn, which in turn serves your Flask application. Heroku handles routing incoming HTTP requests to your running web dynos.

This entire process ensures a consistent and reproducible deployment environment, abstracting away the underlying infrastructure details.

Step-by-Step Implementation: Deploying Your Flask Application

1. Set Up Your Local Flask Application

First, ensure you have Python installed locally. I recommend using a virtual environment for dependency isolation.


# Create a new project directory
mkdir my-flask-app
cd my-flask-app

# Create and activate a virtual environment
python3 -m venv venv
source venv/bin/activate

# Install Flask and Gunicorn
pip install Flask gunicorn

Next, create your main Flask application file. For this example, we’ll name it app.py.


# app.py
from flask import Flask, render_template_string
import os

app = Flask(__name__)

# Basic route
@app.route('/')
def hello():
    # Example to show environment variables working on Heroku
    # Heroku sets PORT, DATABASE_URL (if addon present), etc.
    message = os.environ.get('WELCOME_MESSAGE', 'Hello from Heroku Flask!')
    return render_template_string(f'''
        
        
        Flask on Heroku
        
            

{message}

This app is running on port: {os.environ.get('PORT', 'Not set')}

Python Version: {os.environ.get('PYTHON_VERSION', 'Unknown')}

''') if __name__ == '__main__': # This block is for local development only. # Heroku will use Gunicorn to run the app. app.run(debug=True, port=os.environ.get('PORT', 5000))

Now, generate your requirements.txt file to list all dependencies for Heroku’s buildpack.


pip freeze > requirements.txt

This command creates a file like this:


Flask==2.3.3
gunicorn==21.2.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
Werkzeug==2.3.7

Finally, create a Procfile in the root of your project. This tells Heroku how to start your web process.


# Procfile
web: gunicorn --bind 0.0.0.0:$PORT wsgi:app

Explanation:

To adhere to the wsgi:app pattern in the Procfile, create a new file named wsgi.py:


# wsgi.py
from app import app

if __name__ == "__main__":
    app.run()

This wsgi.py file simply imports your Flask application instance, making it available for Gunicorn.

2. Heroku Setup and Deployment

If you don’t have the Heroku CLI installed, install it now. Then log in:


heroku login
# Follow the browser prompt to log in.

Now, initialize a Git repository and commit your files:


git init
git add .
git commit -m "Initial Flask app for Heroku deployment"

Create a new Heroku application. You can specify a name or let Heroku generate one.


# Option 1: Let Heroku generate a name
heroku create

# Option 2: Specify a name (must be unique)
# heroku create my-unique-flask-app-name

Heroku will add a new Git remote named heroku to your repository.

Finally, deploy your application by pushing your code to the Heroku remote:


git push heroku main

Heroku will detect the Python buildpack, install dependencies, compile the slug, and start your dyno. Once complete, open your application in the browser:


heroku open

You should see your “Hello from Heroku Flask!” message.

3. (Optional) Adding a Database

Most Flask apps need a database. Heroku makes it easy with add-ons. Heroku Postgres is a popular choice.


heroku addons:create heroku-postgresql:hobby-dev

This adds a PostgreSQL database and sets the DATABASE_URL environment variable, which your Flask app can access via os.environ.get('DATABASE_URL'). You’d typically use an ORM like SQLAlchemy with Flask-SQLAlchemy to connect to it.

What Can Go Wrong (Troubleshooting)

Performance & Best Practices

While Heroku provides an excellent developer experience, it’s essential to understand its nuances for optimal performance and cost-effectiveness.

When NOT to use this approach (or at least, the free/hobby tier):

Alternative Methods & Comparison (Legacy vs. Modern):

Best Practices for Heroku Flask Deployments:

For more on this, Check out more Web Development Tutorials.

Author’s Final Verdict

In my opinion, for small to medium-sized Flask applications, prototypes, or educational projects, Heroku remains an unparalleled deployment platform due to its simplicity and robust developer experience. The “Git push to deploy” model significantly reduces friction. However, as your application grows in complexity, traffic, or requires specific resource guarantees, you’ll need to critically evaluate if the cost-benefit analysis favors scaling up on Heroku or migrating to a more specialized cloud platform like Google Cloud Run or AWS App Runner. For many developers, starting with Heroku is still the smartest, most efficient path to getting a Flask application live.

Exit mobile version