DEV Community

Alex Baban
Alex Baban

Posted on • Updated on

How to install Kotlin/Native on Windows

Intro

By the end of this guide, you will have all what is needed to convert a hello.kt file containing this code:

fun main() {
  println("Hello, World!")
}

to hello.exe, a CLI (command-line interface) executable, which you can run and have "Hello, World!" printed in the terminal.

Note

If you want to build with Gradle, this is not the guide you're looking for. This guide shows you how to build by calling kotlinc-native from your terminal.

Prerequisites

Let's Go Kotlin/Native

  • download "the Kotlin/Native compiler" from the GitHub repository (https://github.com/JetBrains/kotlin-native/releases)
    • look for the "Latest release" and follow the link to "Download the binary distribution from..."
    • scroll to the bottom of the page and locate the "Assets" section
    • click on the kotlin-native-windows-...zip link
    • save the .zip file on your computer
  • place "the Kotlin/Native compiler" in the right location
    • in your "home directory" (which you can locate if you run echo %HOMEPATH% in a terminal (usually "C:\Users\YourName")) create a directory .konan
    • from the contents of kotlin-native-windows-...zip take the folder named something like kotlin-native-windows-... and copy it in the .konan directory
    • inside the kotlin-native-windows-... directory, there is a bin directory, add the path to this bin directory to your "Environment Variables..." > "System variables" > "Path" so that kotlinc-native can be called from any location
    • if you need help with what the "Path" environment variable is, here is a nice guide (https://docs.telerik.com/teststudio/features/test-runners/add-path-environment-variables)
  • test
    • open a terminal, run cd / then run kotlinc-native -version, if all went well so far the version of the compiler should show and you are (almost) ready to compile

First compilation

  • create a directory mkdir C:\apps\kotlin
  • create a file hello.kt inside C:\apps\kotlin
  • with your text editor, edit hello.kt and put this code in it:
fun main() {
  println("Hello, World!")
}
  • save hello.kt
  • in a terminal cd C:\apps\kotlin
  • run kotlinc-native hello.kt -o hello
    • because this is the first run of the compiler, the "native dependencies" will be downloaded, extracted and saved to that .konan folder from your "home directory" (this is a one-time operation which might take a few minutes)
  • when the compiler is done, you'll find a hello.exe in the same folder with the source file hello.kt
  • run hello.exe in the terminal to see the "Hello, World!" text

Summary

This guide showed you how to install and then create a "Hello, World" CLI executable with the Kotlin/Native compiler.

Now that the toolchain is installed on your system, go ahead and create your "real" executable.

The syntax to use the compiler is:

kotlinc-native <file-name>.kt -o <file-name>

You can reach me on Twitter at: @alexbaban if you have any questions.

Happy "Kotlin/Native" coding!

Top comments (0)