Anonymous View

Ktor 3.5.0 Help

Heroku

In this tutorial, you will learn how to prepare and deploy a Ktor application to Heroku.

Prerequisites

Before starting this tutorial, ensure that the following prerequisites are met:

  • You have a Heroku account.

  • Heroku CLI is installed on your machine.

Create a sample application

Create a sample application as described in Create, open, and run a new Ktor project.

Prepare the application

Step 1: Configure a port

First, specify a port to listen for incoming requests. Since Heroku uses the PORT environment variable, you need to configure your application to use the value of this variable. Depending on the way used to configure a Ktor server, do one of the following:

  • If server configuration is specified in code, you can retrieve the environment variable value using System.getenv. Open the Application.kt file placed in the src/main/kotlin/com/example folder and change the port parameter value of the embeddedServer() function:

    fun main() { embeddedServer(Netty, port = System.getenv("PORT")?.toIntOrNull() ?: 8080) { // ... }.start(wait = true) }
  • If your server configuration is specified in an application.conf file, you can assign the environment variable to the port parameter by using the ${ENV} syntax. Open the application.conf file placed in src/main/resources and update it as shown below:

    ktor { deployment { port = 8080 port = ${?PORT} } }

Step 2: Add a stage task

Open the build.gradle.kts file and add a custom stage task. Heroku uses the stage task to make an executable file that runs on Heroku's platform:

tasks { create("stage").dependsOn("installDist") }

Step 3: Create a Procfile

Create a Procfile in a project root and add the following content to it:

web: ./build/install/ktor-get-started-sample/bin/ktor-get-started-sample

This file specifies a path to the application's executable generated by the stage task and allows Heroku to start the application. You might need to replace ktor-get-started-sample with your project name.

Step 4: Specify the Java version (optional)

By default, Heroku uses Java 25 to run your application. If your Ktor application is compiled with a different Java version, you need to specify it explicitly to avoid deployment failures.

To specify the Java version, create a system.properties file in your project's root folder with the following content:

java.runtime.version=21

Replace 21 with your desired Java version.

Deploy an application

To deploy your application to Heroku using Git, open a new terminal window and follow the steps below:

  1. Commit changes made in the previous section locally:

    git add . git commit -m "Prepare app for deploying"
  2. Login to Heroku CLI:

    heroku login
  3. Create a Heroku application using the heroku create command. Replace ktor-sample-heroku with the name of your application:

    heroku create ktor-sample-heroku

    This command does two things:

    • Creates a new Heroku application, which is available on the web dashboard.

    • Adds a new Git remote called heroku to a local repository.

  4. To deploy the application, push changes to heroku main:

    git push heroku main
  5. Wait until Heroku builds and publishes the application. When it completes, you should see the following output:

    ... remote: https://clear-https-nn2g64rnonqw24dmmuwwqzlsn5vxkltimvzg623vmfyhaltdn5w.q.proxy.gigablast.org/ deployed to Heroku remote: remote: Verifying deploy... done.
01 June 2026