Deploying to Heroku: An introduction

Share this article

Deploying to Heroku: An introduction

Key Takeaways

  • Heroku is a managed server platform that simplifies the deployment of web applications. It automatically provisions server resources and allows for easy deployment via a git push to the app’s repository on Heroku. It also offers a free tier for low-traffic apps.
  • Heroku projects are managed by buildpacks, which are instructions for fetching dependencies, building, and running your project. Heroku supports buildpacks for several languages and can automatically detect the project type based on certain conventions and dependency files. For unsupported languages or different build tools, third-party buildpacks can be used.
  • Heroku uses a file called Procfile to determine what it should be running. After setting up the Procfile and adding it to the repository, the project can be deployed to Heroku using git push. Additional commands can be used to set persistent config values, scale up the process in case of a traffic surge, or roll back to a previous release.

Thanks to Matthew Wilkin for kindly helping to peer review this article.

In this article, you’ll learn about Heroku and how to deploy your web application to it.

Heroku Logo

If you’ve never heard of it, Heroku is a managed server platform for quickly deploying web applications. It automatically provisions server resources for you, and deployment is as easy as a git push to your app’s repository on Heroku. Best of all, you can deploy your app for free (provided it doesn’t get too much traffic), which makes getting started free and easy.

If you have a lot of traffic, Heroku can get a bit pricy; each node (or dyno, as they call them) will cost you $25 or more per month, and adding features like databases will increase that a bit. That said, it’s a heck of a lot cheaper than hiring a devops team to deliver the stability and ease-of-use that Heroku offers.

Before We Start

If you want to follow along from here, you’ll need to make sure you have a few things handy.

  1. Go download and install the Heroku Toolbelt. This is the command-line utility that we’ll be using to configure the project.
  2. Make sure your project is using Git. You should know what git is, but if you don’t, here’s some light reading. If you don’t have a project, just make sure git is installed.

If you already have something ready to go, skip the next section and go straight to Creating a Heroku Project.

Our Example Project

To assemble this example project, you’re going to need pip (which is good to have handy for any Python development anyhow). We’ll be using a Python project written using Flask, a web microframework for Python, but you can mostly follow along with any project (it should be obvious which parts are language-specific and how to adjust them).

If you have something ready, skip ahead to the next section. If you need a project, set up a project folder as follows (you don’t have to call it myproject):

/myproject
  /templates
    index.html
  app.py
  requirements.txt

And fill them out like so:

app.py:

import os
import flask


app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")


if __name__ == "__main__":
    app.run(port=os.environ.get('PORT', '5000'))

templates/index.html:

<!doctype HTML>
<html>
  <head>
    <title>My example project</title>
  </head>
  <body>
    <h1>This is my project.</h1>
    <!-- feel free to get a bit more creative here -->
  <body>
</html>

requirements.txt

Flask==0.10.1

then run:

pip install -r requirements.txt

After that, make sure everything is working by running python app.py and navigating to http://localhost:5000/. If all went well, you should see index.html rendered there.

Creating a Heroku Project

Here comes the easiest thing in the world. Open a terminal, cd to your project directory, and run the following commands (skip the git one if you already have git in your project):

$ git init
$ heroku create
Creating app... done, stack is cedar-14
https://calm-lake-56303.herokuapp.com/ | https://git.heroku.com/calm-lake-56303.git

With this command, Heroku:

  • generated a name for us (we could have chosen one by running heroku create myproject instead)
  • assigned us a url and a git repository
  • initialized the heroku remote repository in git for us.

We’re just about ready to deploy, but it’s good to know what’s about to happen first. Let’s learn a bit more about how Heroku can know what to do with your code — and how to make sure it’s satisfied.

An Aside on Buildpacks (or, how Heroku Recognizes Your Project)

Heroku projects are managed by buildpacks, which are in essence instructions for fetching dependencies, building and running your project. There are officially supported buildpacks for Node.js, Ruby, Java, Clojure, Scala, PHP, Python, and Go. You won’t need to tell Heroku what you’re using for these; instead, Heroku will guess what the project is, based on some conventions and heuristics regarding dependency management. For example, a requirements.txt file in your project directory, as exists in the example project above, indicates a Python project. Here are the dependency files that Heroku will use to automatically detect your platform for other platforms:

  • Node.js: package.json
  • Ruby: Gemfile
  • PHP: composer.json
  • Java: pom.xml
  • Scala: build.properties
  • Clojure: project.clj
  • Go: Godeps/Godeps.json

If you’re using a different language, or using a different build tool for one of these languages, you can also use a third-party buildpack. Heroku maintains an extensive directory of these, so check there before giving up on your preferred language. You can set the buildpack using the git repository URL; for example, to use Upworthy’s Clojure Boot buildpack, you could run this command in your project directory:

$ heroku buildpacks:set https://github.com/upworthy/heroku-buildpack-boot

You might even find an unlisted buildpack on Github — just check around to make sure it’s safe to use!

Setting up Your Procfile

Everything we need is in place, except for one component. Heroku uses a file called Procfile to tell it what it should be running. For your getting started project, you’ll probably just want to specify a web process, but you can also specify worker jobs.

Since we run our app with python app.py, we will put the following in our Procfile:

web: python app.py

(Later, you might want to use something more performant; you can use Gunicorn by adding it to your requirements.txt and replacing your Procfile contents with web: gunicorn app:app -b 0.0.0.0:$PORT).

Deploying Your Project

Add Procfile to your repository:

$ git add Procfile && git commit -m "added Procfile"

Then, use git push to deploy to Heroku

git push heroku master

🎉🎉🎉 Congratulations 🎉🎉🎉

Your app should be deployed on Heroku. Navigate to the URL that Heroku tells you, and you should see your home page.

That’s it. That’s the whole article. That’s your life with Heroku now; just push your commits and they get deployed immediately. How crazy is that?!

Some Bonus Commands

Just in case you’re not satisfied, here’s some assorted things you might want to do with the heroku command:

  • heroku config:set MY_ENV_VARIABLE=some_value: Set a persistent config value. Useful for things like database passwords and other configuration.
  • heroku ps:scale web=5: Sudden traffic surge? Scale your process up to 5 web dynos in an instant. Note that this will cost you $125 at $25 per dyno, so use this with caution. To scale back down after you fall off Reddit, run heroku ps:scale web=1.
  • Made a mistake? You can list your app’s releases with heroku releases. To roll back to a certain release, run heroku rollback <release identifier>. Or just run heroku release to undo the latest release.

You can also manage most of these from Heroku’s dashboard, if you prefer.

Frequently Asked Questions about Deploying to Heroku

What are the prerequisites for deploying an application to Heroku?

Before deploying an application to Heroku, you need to have a few things in place. Firstly, you need to have a Heroku account. If you don’t have one, you can sign up for free on the Heroku website. Secondly, you need to have the Heroku CLI (Command Line Interface) installed on your computer. This tool allows you to manage your Heroku apps from the command line. Lastly, your application needs to be stored in a Git repository, as Heroku uses Git for deployment.

How can I rollback a release on Heroku?

Rolling back a release on Heroku is quite straightforward. You can use the heroku rollback command to revert to a previous release. This command undoes the changes made by the last release and redeploys the previous release. If you want to rollback to a specific release, you can use the heroku rollback vNUMBER command, replacing ‘NUMBER’ with the version number of the release you want to rollback to.

How can I undo a push on Heroku?

If you’ve pushed changes to Heroku that you want to undo, you can use the git revert command. This command creates a new commit that undoes the changes made in the specified commit. After running this command, you can push the new commit to Heroku to undo the changes.

How can I fix problematic deployments on Heroku?

If you’re experiencing issues with a deployment on Heroku, there are a few steps you can take to troubleshoot and fix the problem. Firstly, you can check the logs for your application using the heroku logs command. This will show you any error messages that might indicate what’s going wrong. Secondly, you can rollback the release to a previous version if the problem started after a recent deployment.

How can I manage releases on Heroku?

Heroku provides a releases API that allows you to manage your application’s releases. You can use this API to list your application’s releases, view information about a specific release, create a new release, or rollback to a previous release. The releases API is accessible through the Heroku CLI, and you can find detailed documentation on how to use it on the Heroku Dev Center website.

How can I deploy a new version of my application without downtime?

Heroku’s preboot feature allows you to deploy a new version of your application without any downtime. When you enable preboot for your application, Heroku will start a new set of dynos with the new version of your application before stopping the old dynos. This ensures that your application remains available during the deployment process.

How can I automate deployments to Heroku?

Heroku supports several methods for automating deployments. One of the most common methods is to use a continuous integration (CI) service like Travis CI or CircleCI. These services can automatically deploy your application to Heroku whenever you push changes to your Git repository.

How can I scale my application on Heroku?

Scaling an application on Heroku involves increasing or decreasing the number of dynos – the containers that run your application. You can scale your application manually using the Heroku CLI or the Heroku Dashboard. Alternatively, you can use Heroku’s autoscaling feature, which automatically adjusts the number of dynos based on your application’s traffic.

How can I monitor my application’s performance on Heroku?

Heroku provides several tools for monitoring your application’s performance. The Heroku Dashboard provides a high-level overview of your application’s performance, including response times and throughput. For more detailed metrics, you can use Heroku’s Metrics API or add-ons like New Relic or Librato.

How can I secure my application on Heroku?

Heroku provides several features to help you secure your application. These include automated certificate management for HTTPS, the ability to restrict access to your application based on IP address, and two-factor authentication for your Heroku account. Additionally, Heroku’s platform is designed with security in mind, and includes features like isolated execution environments and regular security updates.

Adam BardAdam Bard
View Author

Adam makes a lot of websites. He presently develops full-time for Tapstream, freelances occasionally and posts unsaleable articles on his blog.

deployHerokupipRalphM
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week