Deploying a Kotlin Application on VPS: A Step-by-Step Guide from A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Deploying a Kotlin Application Requires a Special Approach

\n

Developing in Kotlin is only half the battle. The other half is competently deploying your application on a remote server. Without this stage, your code will remain just a local project, inaccessible to users.

\n

In this guide, we will cover the full deployment cycle of a Kotlin application on a VPS (Virtual Private Server). You will learn how to build the project into an executable JAR file, configure the server environment, run the application as a service, and ensure its stable operation. The article is intended for developers who already have basic knowledge of Kotlin and Linux.

\n\n

1. Project Preparation and Building the JAR File

\n

Before uploading the code to the server, you need to build the application into a single artifact. For Kotlin projects written using Spring Boot or Ktor, the standard format is a JAR file (Java Archive).

\n\n

1.1. Configuring build.gradle.kts

\n

Make sure your build.gradle.kts file contains the plugin for building an executable JAR. For Spring Boot, it looks like this:

\n
plugins {\n    kotlin("jvm") version "1.9.22"\n    id("org.springframework.boot") version "3.2.2"\n    id("io.spring.dependency-management") version "1.1.4"\n}\n\ndependencies {\n    implementation("org.springframework.boot:spring-boot-starter-web")\n    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")\n}\n
\n

For pure Ktor (without Spring), use the application plugin:

\n
plugins {\n    kotlin("jvm") version "1.9.22"\n    application\n}\n\napplication {\n    mainClass.set("com.example.ApplicationKt")\n}\n
\n\n

1.2. Building the Project

\n

Run the build command in the project root folder:

\n
./gradlew clean build
\n

After a successful build, the file app-0.0.1-SNAPSHOT.jar (the name depends on the configuration) will appear in the build/libs/ directory. Copy it to your local computer.

\n
Important: For Ktor applications, make sure the correct port (usually 8080) and operating mode are specified in the src/main/resources/application.conf file.
\n\n

2. Configuring the VPS Server

\n

For deployment, we will need a VPS with Ubuntu 22.04 LTS installed. It is assumed that you have SSH access and superuser (sudo) privileges.

\n\n

2.1. Installing Java (JRE)

\n

Kotlin compiles to JVM bytecode, so the server must have Java Runtime Environment (JRE) version 17 or higher installed:

\n
sudo apt update\nsudo apt install openjdk-17-jre -y\njava -version
\n\n

2.2. Creating a User for the Application

\n

For security reasons, it is not recommended to run the application as root. Let's create a separate user:

\n
sudo useradd -m -s /bin/bash kotlinapp\nsudo passwd kotlinapp
\n\n

2.3. Uploading the JAR File to the Server

\n

Use SCP to copy the file from your local machine to the VPS:

\n
scp build/libs/app-0.0.1-SNAPSHOT.jar kotlinapp@your-server-ip:/home/kotlinapp/app.jar
\n\n

3. Running the Application and Configuring Systemd

\n

To ensure the application starts automatically on system boot and does not crash when the SSH connection is lost, we will set it up as a Systemd service.

\n\n

3.1. Manual Launch (for Testing)

\n

Connect to the server as the kotlinapp user and run:

\n
cd /home/kotlinapp\njava -jar app.jar
\n

If the application starts and responds on port 8080, proceed to the next step. Stop the process (Ctrl+C).

\n\n

3.2. Creating a Systemd Unit File

\n

Create the service file:

\n
sudo nano /etc/systemd/system/kotlin-app.service
\n

Paste the following content:

\n
[Unit]\nDescription=Kotlin Application Service\nAfter=net                

Blogs

Book Recommendations