Deploying Scala Apps on Heroku
Last updated October 16, 2024
Table of Contents
This article describes how to take an existing Scala app and deploy it to Heroku.
If you are new to Heroku, you might want to start with the Getting Started with Scala on Heroku tutorial.
Prerequisites
Before you begin, make sure you have installed Scala, sbt, and the Heroku CLI. This article assumes that you are starting with an existing Scala app that is set up to build with sbt.
This article does not apply to apps that use the Play framework. Play framework support is described in the Play framework support reference.
Overview
The way Heroku recognizes a Scala application is described in Heroku Scala Support.
Your application should already include a project/build.properties
file that defines the sbt.version it is designed to use. It will look something like this:
sbt.version=0.13.9
For more information, see Build behavior.
Verify that the sbt-native-packager plugin is set up correctly
To package your app for deployment, you can use the sbt-native-packager sbt community plugin. If you are using the Play framework version 2.3 or higher, then this plugin will be automatically configured for you.
If you are not using Play framework, you must configure the plugin manually. The project/plugins.sbt
file will specify which version of the sbt-native-packager to use. Add the following to your project/plugins.sbt
file:
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.0.3")
In your build.sbt
enable the plugin you want. For example the JavaAppPackaging
.
enablePlugins(JavaAppPackaging)
For more detailed information, see the sbt documentation, and the sbt-native-packager documentation.
Specify a JDK
Optionally, you can specify a JDK. For more information, see Specifying a Java version.
The Procfile
A Procfile is a text file in the root directory of your application, that defines process types and explicitly declares what command should be executed to start your app. If you are using the sbt-native-packager
and the java_application
package archetype, you will have a start script in target/universal/stage/bin
. Your Procfile
will look something like this, except instead of hello
, you’d use the name of your app:
web: target/universal/stage/bin/hello
This declares a single process type, web
, and the command needed to run it. The name, web, is important here. It declares that this process type will be attached to the HTTP routing stack of Heroku, and receive web traffic when deployed.
The command in a web process type must bind to the port number specified in the PORT
environment variable. If it does not, the dyno will not start.
How to keep build artifacts out of git
Prevent build artifacts from going into revision control by creating a .gitignore
file. Here’s a typical .gitignore
file:
target/
project/boot
Build your app and run it locally
To build your app locally do this:
$ sbt compile stage
$ heroku local --port 5001
Your app should now be running on http://localhost:5001/.
Deploy your application to Heroku
After you commit your changes to git, you can deploy your app to Heroku.
$ git add .
$ git commit -m "Added a Procfile and packaging."
$ heroku login
...
$ heroku create
Creating warm-frost-1289... done, stack is heroku-18
http://warm-frost-1289.herokuapp.com/ | git@heroku.com:warm-frost-1289.git
Git remote heroku added
$ git push heroku master
...
-----> Scala app detected
To open the app in your browser, type heroku open
.
Console
Heroku allows you to run one-off dynos - scripts and applications that only need to be executed when needed - using the heroku run
command. The console has your application code available. Use this to launch a REPL process attached to your local terminal for experimenting in your app’s environment:
$ heroku run sbt console
Running sbt console attached to terminal... up, run.1
...
scala>
For more information, see Running a Remote sbt Console for a Scala or Play Application.
Troubleshooting
Major releases of sbt may not be compatible with each other. Most issues will be related to mismatched versions of sbt.