Getting Started on Heroku with Scala and Play
Introduction
Complete this tutorial to deploy a sample Scala app to Cedar, the legacy generation of the Heroku platform. To deploy the app to the Fir generation, only available to Heroku Private Spaces, follow this guide instead.
The tutorial assumes that you have:
- A verified Heroku Account
- An Eco dynos plan subscription (recommended)
- OpenJDK 17 (or newer) installed locally
- sbt installed
- Postgres installed locally
Using dynos and databases to complete this tutorial counts towards your usage. We recommend using our low-cost plans to complete this tutorial. Eligible students can apply for platform credits through our new Heroku for GitHub Students program.
Set Up
Install the Heroku Command Line Interface (CLI). Use the CLI to manage and scale your app, provision add-ons, view your logs, and run your app locally.
The Heroku CLI requires Git, the popular version control system. If you don’t already have Git installed, complete the following before proceeding:
Download and run the installer for your platform:
Download the appropriate installer for your Windows installation:
You can find more installation options for the Heroku CLI here.
After installation, you can use the heroku command from your command shell.
To log in to the Heroku CLI, use the heroku login command:
$ heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/***
heroku: Waiting for login...
Logging in... done
Logged in as me@example.com
This command opens your web browser to the Heroku login page. If your browser is already logged in to Heroku, click the Log In button on the page.
This authentication is required for the heroku and git commands to work correctly.
If you have any problems installing or using the Heroku CLI, see the main Heroku CLI article for advice and troubleshooting steps.
If you’re behind a firewall that uses a proxy to connect with external HTTP/HTTPS services, set the HTTP_PROXY or HTTPS_PROXY environment variables in your local development environment before running the heroku command.
Clone the Sample App
If you’re new to Heroku, it’s recommended that you complete this tutorial using the Heroku-provided sample application.
To deploy an existing application, follow this article instead.
Clone the sample application to get a local version of the code. Execute these commands in your local command shell or terminal:
$ git clone https://github.com/heroku/scala-getting-started
$ cd scala-getting-started
You now have a functioning Git repository that contains a simple application. It includes a build.sbt file, which is used by sbt, a Scala build tool.
Create Your App
Using a dyno and a database to complete this tutorial counts towards your usage. Delete your app, and database as soon as you’re done to control costs.
Apps use Eco dynos if you’re subscribed to Eco by default. Otherwise, it defaults to Basic dynos. The Eco dynos plan is shared across all Eco dynos in your account. It’s recommended if you plan on deploying many small apps to Heroku. Learn more here. Eligible students can apply for platform credits through our Heroku for GitHub Students program.
Create an app on Heroku to prepare the platform to receive your source code:
$ heroku create
Creating app... done, warm-dawn-59024
https://warm-dawn-59024-e31f471a73b6.herokuapp.com/ | https://git.heroku.com/warm-dawn-59024.git
When you create an app, a Git remote named heroku is also created and added to your local repository configuration. Git remotes are versions of your repository that live on other servers. You can deploy your app by pushing code to that special Heroku-hosted remote associated with your app.
Heroku generates a random name for your app, in this case, warm-dawn-59024. You can specify your own app name.
Provision a Database
The sample app requires a database. Provision a Heroku Postgres database, an add-on available through the Elements Marketplace. Add-ons are cloud services that provide out-of-the-box additional services for your application, such as logging, monitoring, databases, and more.
An essential-0 Postgres size costs $5 a month, prorated to the minute. At the end of this tutorial, we prompt you to delete your database to minimize costs.
$ heroku addons:create heroku-postgresql:essential-0
Creating heroku-postgresql:essential-0 on warm-dawn-59024... ~$0.007/hour (max $5/month)
Database should be available soon
postgresql-cylindrical-18516 is being created in the background. The app will restart when complete...
Run heroku addons:info postgresql-cylindrical-18516 to check creation progress.
Run heroku addons:docs heroku-postgresql to view documentation.
You can wait for the database to provision by running this command:
$ heroku pg:wait
After that command exits, your Heroku app can access the Postgres database. The DATABASE_URL environment variable stores your credentials. For applications that use the JVM an additional environment variable JDBC_DATABASE_URL is available. It contains a JDBC compatible connection string. You can see all the add-ons provisioned with the addons command:
$ heroku addons
Add-on Plan Price Max Price State
─────────────────────────────────────────────────────────────────────────────────────────────────────
heroku-postgresql (postgresql-cylindrical-18516) essential-0 ~$0.007/hour $5/month created
└─ as DATABASE
The table above shows add-ons and the attachments to the current app (warm-dawn-59024) or other apps.
Deploy the App
Using a dyno to complete this tutorial counts towards your usage. Delete your app and database as soon as you’re done to control costs.
Deploy your code. This command pushes the main branch of the sample repo to your heroku remote, which then deploys to Heroku:
$ git push heroku main
remote: Updated 27 paths from 6231a3a
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-24 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Scala app detected
remote: -----> Installing Azul Zulu OpenJDK 25.0.3
remote: -----> Downloading sbt launcher 1.11.7...
remote: -----> Setting up sbt launcher...
remote: -----> Running: sbt compile stage
remote: WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
remote: WARNING: sun.misc.Unsafe::objectFieldOffset has been called by scala.runtime.LazyVals$ (file:/tmp/codon/tmp/cache/sbt-launcher/sbt-launch-1.11.7.jar)
remote: WARNING: Please consider reporting this to the maintainers of class scala.runtime.LazyVals$
remote: WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
remote: [info] [launcher] getting org.scala-sbt sbt 1.11.7 (this may take some time)...
remote: [info] [launcher] getting Scala 2.12.20 (for sbt)...
remote: WARNING: A restricted method in java.lang.System has been called
remote: WARNING: java.lang.System::load has been called by com.sun.jna.Native in an unnamed module (file:/tmp/codon/tmp/cache/sbt_boot/scala-2.12.20/org.scala-sbt/sbt/1.11.7/jna-5.12.0.jar)
remote: WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
remote: WARNING: Restricted methods will be blocked in a future release unless native access is enabled
remote:
remote: [info] welcome to sbt 1.11.7 (Azul Systems, Inc. Java 25.0.3)
remote: [info] loading global plugins from /tmp/codon/tmp/cache/sbt_global/plugins
remote: [info] compiling 1 Scala source to /tmp/codon/tmp/cache/sbt_global/plugins/target/scala-2.12/sbt-1.0/classes ...
remote: [info] Non-compiled module 'compiler-bridge_2.12' for Scala 2.12.20. Compiling...
remote: [info] Compilation completed in 7.033s.
remote: [info] done compiling
remote: [info] loading settings for project build_304f2153-build from plugins.sbt...
remote: [info] loading project definition from /tmp/build_304f2153/project
remote: [info] loading settings for project root from build.sbt...
remote: [info] __ __
remote: [info] \ \ ____ / /____ _ __ __
remote: [info] \ \ / __ \ / // __ `// / / /
remote: [info] / / / /_/ // // /_/ // /_/ /
remote: [info] /_/ / .___//_/ \__,_/ \__, /
remote: [info] /_/ /____/
remote: [info]
remote: [info] Version 3.0.9 running Java 25.0.3
remote: [info]
remote: [info] Play is run entirely by the community. Please consider contributing and/or donating:
remote: [info] https://www.playframework.com/sponsors
remote: [info]
remote: [info] compiling 8 Scala sources and 1 Java source to /tmp/build_304f2153/target/scala-2.13/classes ...
remote: [info] done compiling
remote: [success] Total time: 9 s, completed May 5, 2026, 10:05:21 PM
remote: [info] Wrote /tmp/build_304f2153/target/scala-2.13/scala-getting-started_2.13-1.0-SNAPSHOT.pom
remote: [success] Total time: 1 s, completed May 5, 2026, 10:05:22 PM
remote: -----> Collecting sbt plugin information
remote: -----> Collecting dependency information
remote: -----> Copying sbt and dependencies into slug for runtime use
remote: -----> Dropping compilation artifacts from the slug
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 129.2M
remote: -----> Launching...
remote: Released v4
remote: https://warm-dawn-59024-e31f471a73b6.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/warm-dawn-59024.git
* [new branch] main -> main
Visit the app at the URL shown in the logs. As a shortcut, you can also open the website as follows:
$ heroku open
Understanding the Procfile
Use a Procfile, a text file in the root directory of your application, to explicitly declare what command to execute to start your app.
The Procfile in the example app looks like this:
web: target/universal/stage/bin/scala-getting-started -Dhttp.port=${PORT}
This Procfile 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 is attached to Heroku’s HTTP routing stack and receives web traffic when deployed.
A Procfile can contain additional process types. For example, you can declare a background worker process that processes items off a queue.
View Logs
Heroku treats logs as streams of time-ordered events, aggregated from the output streams of all your app and Heroku components. Heroku provides a single stream for all events.
View information about your running app by using one of the logging commands, heroku logs --tail:
$ heroku logs --tail
2026-05-05T22:02:57.277475+00:00 app[api]: Initial release by user developer@example.com
2026-05-05T22:02:57.277475+00:00 app[api]: Release v1 created by user developer@example.com
2026-05-05T22:02:57.421933+00:00 app[api]: Enable Logplex by user developer@example.com
2026-05-05T22:02:57.421933+00:00 app[api]: Release v2 created by user developer@example.com
2026-05-05T22:04:31.794611+00:00 app[api]: Release v3 created by user heroku-postgresql@addons.heroku.com
2026-05-05T22:04:31.794611+00:00 app[api]: @ref:postgresql-cylindrical-18516 completed provisioning, setting DATABASE_URL. by user heroku-postgresql@addons.heroku.com
2026-05-05T22:04:36.000000+00:00 app[api]: Build started by user developer@example.com
2026-05-05T22:05:45.611121+00:00 heroku[web.1]: Starting process with command `target/universal/stage/bin/scala-getting-started -Dhttp.port=36494`
2026-05-05T22:05:46.534356+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2026-05-05T22:05:46.587860+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -XX:MaxRAM=536870912 -Xmx300m -Xss512k -XX:CICompilerCount=2
2026-05-05T22:05:47.274159+00:00 app[web.1]: 2026-05-05 22:05:47 INFO p.a.http.HttpErrorHandlerExceptions Registering exception handler: guice-provision-exception-handler
2026-05-05T22:05:47.494733+00:00 app[web.1]: WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
2026-05-05T22:05:47.494734+00:00 app[web.1]: WARNING: sun.misc.Unsafe::objectFieldOffset has been called by com.google.common.util.concurrent.AbstractFuture$UnsafeAtomicHelper (file:/app/target/universal/stage/lib/com.google.guava.guava-32.1.3-jre.jar)
2026-05-05T22:05:47.494735+00:00 app[web.1]: WARNING: Please consider reporting this to the maintainers of class com.google.common.util.concurrent.AbstractFuture$UnsafeAtomicHelper
2026-05-05T22:05:47.494735+00:00 app[web.1]: WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
2026-05-05T22:05:47.740661+00:00 app[web.1]: 2026-05-05 22:05:47 INFO play.api.db.DefaultDBApi Database [default] initialized
2026-05-05T22:05:47.744670+00:00 app[web.1]: 2026-05-05 22:05:47 INFO play.api.db.HikariCPConnectionPool Creating Pool for datasource 'default'
2026-05-05T22:05:48.391864+00:00 app[web.1]: 2026-05-05 22:05:48 INFO play.api.Play Application started (Prod) (no global state)
2026-05-05T22:05:48.789371+00:00 app[web.1]: 2026-05-05 22:05:48 INFO play.core.server.PekkoHttpServer Listening for HTTP on /[0:0:0:0:0:0:0:0]:36494
2026-05-05T22:05:49.026708+00:00 heroku[web.1]: State changed from starting to up
2026-05-05T22:06:04.260180+00:00 heroku[router]: at=info method=GET path="/" host=warm-dawn-59024-e31f471a73b6.herokuapp.com request_id=891d4830-0f53-856b-1b21-8e422838e87a fwd="123.456.789.0" dyno=web.1 connect=0ms service=143ms status=200 bytes=10156 protocol=http1.1 tls=false
2026-05-05T22:05:40.975657+00:00 app[api]: Scaled to web@1:Basic by user developer@example.com
2026-05-05T22:05:40.958435+00:00 app[api]: Release v4 created by user developer@example.com
2026-05-05T22:05:40.958435+00:00 app[api]: Deploy e5e5479e by user developer@example.com
2026-05-05T22:06:13.614174+00:00 heroku[router]: at=info method=GET path="/" host=warm-dawn-59024-e31f471a73b6.herokuapp.com request_id=93d2ff1b-7714-2f0a-a75e-8da654d2ec9f fwd="123.456.789.0" dyno=web.1 connect=0ms service=6ms status=200 bytes=10156 protocol=http1.1 tls=false
To generate more log messages, refresh the app in your browser.
To stop streaming the logs, press Control+C.
Scale the App
After deploying the sample app, it automatically runs on a single web dyno. Think of a dyno as a lightweight container that runs the command specified in the Procfile.
You can check how many dynos are running by using the ps command:
$ heroku ps
=== web (Basic): target/universal/stage/bin/scala-getting-started -Dhttp.port=${PORT} (1)
web.1: up 2026/05/05 17:05:49 -0500 (~ 26s ago)
Scaling an app on Heroku is equivalent to changing the number of running dynos. Scale the number of web dynos to zero:
$ heroku ps:scale web=0
$ heroku ps:wait
Access the app again by hitting refresh in your browser, or heroku open to open it in a web tab. You get an error message because you no longer have web dynos available to serve requests.
Scale it up again:
$ heroku ps:scale web=1
$ heroku ps:wait
By default, apps use Eco dynos if you’re subscribed to Eco. Otherwise, it defaults to Basic dynos. The Eco dynos plan is shared across all Eco dynos in your account and is recommended if you plan on deploying many small apps to Heroku. Eco dynos sleep if they don’t receive any traffic for half an hour. This sleep behavior causes a few seconds delay for the first request upon waking. Eco dynos consume from a monthly, account-level quota of eco dyno hours. As long as you haven’t exhausted the quota, your apps can continue to run.
To avoid dyno sleeping, upgrade to a Basic or higher dyno type as described in the Dyno Types article. Upgrading to at least Standard dynos allows you to scale up to multiple dynos per process type.
Declare App Dependencies
Heroku recognizes an app as a Scala app by the existence of a build.sbt file in the root directory.
The demo app you deployed already has a build.sbt:
name := """scala-getting-started"""
organization := "com.heroku"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.13.18"
libraryDependencies += guice
libraryDependencies += jdbc
...
The build.sbt file specifies the dependencies to install with your application.
Run sbt stage in your local directory to install the dependencies, preparing your system for running the app locally:
$ sbt stage
[info] welcome to sbt 1.11.7 (Oracle Corporation Java 17.0.2)
[info] loading settings for project scala-getting-started-build from plugins.sbt...
[info] loading project definition from ./scala-getting-started/project
[info] loading settings for project root from build.sbt...
[info] __ __
Push Local Changes
In this step, you propagate a local change to the application to Heroku.
Modify build.sbt to include an additional dependency for the jscience to libraryDependencies.
At the end of build.sbt add:
libraryDependencies += "org.jscience" % "jscience" % "4.3.1"
Add the import statements for the library.
In file app/controllers/Application.scala, on line 2 add:
import org.jscience.physics.amount.Amount
import org.jscience.physics.model.RelativisticModel
import javax.measure.unit.SI
Add a new convert method.
In file app/controllers/Application.scala, on line 13 add:
def convert(): Action[AnyContent] = Action { implicit request: Request[AnyContent] =>
RelativisticModel.select()
val energy = Amount.valueOf("12 GeV")
Ok("E=mc^2: " + energy + " = " + energy.to(SI.KILOGRAM))
}
Add a new route to the new action.
In file conf/routes, on line 7 add:
GET /convert controllers.Application.convert()
Now test locally:
$ sbt stage
$ heroku local --port=5006
Visit your application’s /convert path at http://localhost:5006/convert. If your changes worked, you see scientific conversions:
E=mc^2: 12 GeV = (2.139194076302506E-26 ± 1.4E-42) kg
Now deploy this local change to Heroku.
Almost every deploy to Heroku follows this same pattern. First, add the modified files to the local Git repository:
$ git add .
Now commit the changes to the repository:
$ git commit -m "Add convert endpoint"
[main 3bdb360] Add convert endpoint
4 files changed, 33 insertions(+)
create mode 100644 install.txt
Now deploy as before:
$ git push heroku main
Finally, check that everything is working:
$ heroku open /convert
Provision a Logging Add-on
Beyond databases, add-ons provide many additional services for your application. In this step, you provision a free add-on to store your app’s logs.
By default, Heroku stores 1500 lines of logs from your application, but the full log stream is available as a service. Several add-on providers have logging services that provide things such as log persistence, search, and email and SMS alerts.
In this step, you provision one of these logging add-ons, Papertrail.
Provision the Papertrail logging add-on:
$ heroku addons:create papertrail
Creating papertrail on warm-dawn-59024... free
Provisioning has been successful
Created papertrail-rigid-62615
Run heroku addons:docs papertrail to view documentation.
The add-on is now deployed and configured for your app. You can list add-ons for your app with this command:
$ heroku addons
Add-on Plan Price Max Price State
─────────────────────────────────────────────────────────────────────────────────────────────────────
heroku-postgresql (postgresql-cylindrical-18516) essential-0 ~$0.007/hour $5/month created
└─ as DATABASE
papertrail (papertrail-rigid-62615) choklad free free created
└─ as PAPERTRAIL
The table above shows add-ons and the attachments to the current app (warm-dawn-59024) or other apps.
To see this particular add-on in action, visit your application’s Heroku URL a few times. Each visit generates more log messages, which get routed to the Papertrail add-on. Visit the Papertrail console to see the log messages:
$ heroku addons:open papertrail
Your browser opens up a Papertrail web console, showing the latest log events. The interface lets you search and set up alerts.
Start a One-off Dyno
You can run a command, typically scripts and applications that are part of your app, in a one-off dyno using the heroku run command. You can also run a one-off command like java -version:
$ heroku run "java -version"
Running java -version on warm-dawn-59024... starting, run.5692
Running java -version on warm-dawn-59024... connecting, run.5692
Running java -version on warm-dawn-59024... up, run.5692
openjdk version "25.0.3" 2026-04-21 LTS
OpenJDK Runtime Environment Zulu25.34+17-CA (build 25.0.3+9-LTS)
OpenJDK 64-Bit Server VM Zulu25.34+17-CA (build 25.0.3+9-LTS, mixed mode, sharing)
If you receive an error, Error connecting to process, configure your firewall.
Let’s try another example. Run the bash command to open up a shell on a one-off dyno. You can then execute commands there.
$ heroku run bash
Running bash on warm-dawn-59024... starting, run.6485
Running bash on warm-dawn-59024... connecting, run.6485
Running bash on warm-dawn-59024... up, run.6485
~ $ ls -lah
total 88K
drwx------ 13 u8691 dyno 4.0K May 5 22:07 .
drwxr-xr-x 11 root root 4.0K Apr 22 14:59 ..
-rw------- 1 u8691 dyno 14 May 5 22:04 .env
drwx------ 3 u8691 dyno 4.0K May 5 22:04 .g8
drwx------ 2 u8691 dyno 4.0K May 5 22:04 .github
-rw------- 1 u8691 dyno 96 May 5 22:04 .gitignore
drwx------ 7 u8691 dyno 4.0K May 5 22:05 .heroku
drwx------ 8 u8691 dyno 4.0K May 5 22:04 .jdk
drwx------ 2 u8691 dyno 4.0K May 5 22:05 .profile.d
drwx------ 8 u8691 dyno 4.0K May 5 22:05 .sbt_home
-rw------- 1 u8691 dyno 1.1K May 5 22:04 LICENSE
-rw------- 1 u8691 dyno 74 May 5 22:04 Procfile
-rw------- 1 u8691 dyno 78 May 5 22:04 Procfile.windows
-rw------- 1 u8691 dyno 2.3K May 5 22:04 README.md
drwx------ 4 u8691 dyno 4.0K May 5 22:04 app
-rw------- 1 u8691 dyno 157 May 5 22:04 app.json
-rw------- 1 u8691 dyno 310 May 5 22:04 build.sbt
drwx------ 2 u8691 dyno 4.0K May 5 22:04 conf
drwx------ 4 u8691 dyno 4.0K May 5 22:05 project
drwx------ 4 u8691 dyno 4.0K May 5 22:04 public
-rw------- 1 u8691 dyno 103 May 5 22:04 system.properties
drwx------ 6 u8691 dyno 4.0K May 5 22:05 target
~ $
~ $ exit
Type exit to exit the shell.
Define Config Vars
Heroku lets you externalize configuration by storing data such as encryption keys or external resource addresses in config vars.
At runtime, we expose config vars as environment variables to the application.
For example, modify Application.scala so that the method obtains an energy value from the ENERGY environment variable:
def convert(): Action[AnyContent] = Action { implicit request: Request[AnyContent] =>
RelativisticModel.select()
Ok(sys.env.get("ENERGY")
.map(Amount.valueOf)
.map(energy => "E=mc^2: " + energy + " = " + energy.to(SI.KILOGRAM))
.getOrElse("ENERGY environment variable is not set!"))
}
The heroku local command automatically sets up the environment based on the contents of the .env file in your local directory. In the top level directory of your sample project, there’s already a .env file that contains:
ENERGY=20 GeV
Rebuild the app with sbt stage. Then run the app with heroku local --port=5006 and visit http://localhost:5006/convert to see the conversion value for 20 GeV.
To set the config var on Heroku, execute the following:
$ heroku config:set ENERGY="20 GeV"
Setting ENERGY and restarting warm-dawn-59024... done, v5
ENERGY: 20 GeV
View the app’s config vars using heroku config:
$ heroku config
ENERGY: 20 GeV
...
To see this change in action, deploy your changed application to Heroku.
Use a Database
Listing the config vars for your app displays the URL that your app uses to connect to the database, DATABASE_URL:
$ heroku config
DATABASE_URL: postgres://xx:yyy@host:5432/d8slm9t7b5mjnd
HEROKU_POSTGRESQL_BROWN_URL: postgres://xx:yyy@host:5432/d8slm9t7b5mjnd
...
Heroku also provides a pg command that shows a lot more information:
$ heroku pg
=== DATABASE_URL
Plan: essential-0
Status: Available
Connections: unknown/20
PG Version: 17.9
Created: 2026-05-05 22:03
Data Size: unknown usage / 1 GB (In compliance)
Tables: 0/4000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
Continuous Protection: On
Add-on: postgresql-cylindrical-18516
The example app you deployed already has database functionality. You can visit the page by appending /database to your app’s URL.
Database Output
* Read from DB: 2024-11-27 13:07:53.002632
* Read from DB: 2024-11-27 13:07:54.965283
* Read from DB: 2024-11-27 13:07:55.620596
If you have Postgres installed locally, you can also interact directly with the database. For example, here’s how to connect to the database using psql and execute a query:
$ heroku pg:psql -c "SELECT * FROM ticks"
--> Connecting to postgresql-cylindrical-18516
tick
----------------------------
2026-05-05 22:07:33.442592
(1 row)
Read more about Heroku PostgreSQL.
Delete Your App
Remove the app from your account. We only charge you for the resources you used.
This action permanently deletes your application and any add-ons attached to it.
$ heroku apps:destroy
You can confirm that your app is gone with this command:
$ heroku apps --all
Next Steps
You now know how to configure and deploy a Scala app, view logs, and start a console.
To learn more, see: