Introduction: Why Deploying a Kotlin Application Requires a Special Approach
\nDeveloping 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.
\nIn 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\n1. Project Preparation and Building the JAR File
\nBefore 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\n1.1. Configuring build.gradle.kts
\nMake sure your build.gradle.kts file contains the plugin for building an executable JAR. For Spring Boot, it looks like this:
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\nFor pure Ktor (without Spring), use the application plugin:
plugins {\n kotlin("jvm") version "1.9.22"\n application\n}\n\napplication {\n mainClass.set("com.example.ApplicationKt")\n}\n\n\n1.2. Building the Project
\nRun the build command in the project root folder:
\n./gradlew clean build\nAfter 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.
src/main/resources/application.conf file.2. Configuring the VPS Server
\nFor 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\n2.1. Installing Java (JRE)
\nKotlin compiles to JVM bytecode, so the server must have Java Runtime Environment (JRE) version 17 or higher installed:
\nsudo apt update\nsudo apt install openjdk-17-jre -y\njava -version\n\n2.2. Creating a User for the Application
\nFor security reasons, it is not recommended to run the application as root. Let's create a separate user:
\nsudo useradd -m -s /bin/bash kotlinapp\nsudo passwd kotlinapp\n\n2.3. Uploading the JAR File to the Server
\nUse SCP to copy the file from your local machine to the VPS:
\nscp build/libs/app-0.0.1-SNAPSHOT.jar kotlinapp@your-server-ip:/home/kotlinapp/app.jar\n\n3. Running the Application and Configuring Systemd
\nTo 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\n3.1. Manual Launch (for Testing)
\nConnect to the server as the kotlinapp user and run:
cd /home/kotlinapp\njava -jar app.jar\nIf the application starts and responds on port 8080, proceed to the next step. Stop the process (Ctrl+C).
\n\n3.2. Creating a Systemd Unit File
\nCreate the service file:
\nsudo nano /etc/systemd/system/kotlin-app.service\nPaste the following content:
\n[Unit]\nDescription=Kotlin Application Service\nAfter=net