Kotlin

How to Install and Run Kotlin in Ubuntu

This article will explain how to install Kotlin programming language in Ubuntu. The walkthrough will mostly consist of instructions on installing, running and building Kotlin apps. No major Kotlin code samples will be shared in this article.

About Kotlin

Kotlin is a general purpose programming language developed by JetBrains, known as developers of many popular integrated development environment (IDE) software. The main highlights of Kotlin are its full interoperability with Java, statically typed syntax, strong emphasis on nullability checks to avoid NullPointerExceptions, and less code verbosity than Java. Google recently announced that Kotlin is now the preferred language for developing Android apps and full support for it was added to the Android Studio IDE.

Hello World in Kotlin

Below is a basic hello world example in Kotlin giving you some basic idea about its syntax.

fun main(args: Array<String>) {
 
  println("Hello World!")
}

All Kotlin files must end in the “.kt” extension. Building a Kotlin file converts a “.kt” file in a “.class” file using the following pattern: “hello.kt” is automatically converted into a “HelloKt.class” file.

Installing Kotlin in Ubuntu Using Snap Package

The simplest and easiest way to install Kotlin in Ubuntu is to use the official Kotlin snap package. To install Kotlin from snap store, run the following command:

$ sudo snap install --classic kotlin

Manually Installing Kotlin in Ubuntu

If you don’t like installing snap packages, you can manually install Kotlin in Ubuntu or any other Linux distribution using SDKMAN. Run the following two commands in succession to do so:

$ curl -s https://get.sdkman.io | bash
$ sdk install kotlin

Verifying the Installation of the Kotlin Compiler

You can run Kotlin REPL shell to confirm successful installation. Run the command below to view the shell:

$ kotlinc

You can run any valid Kotlin code in the interactive shell shown above.

Compiling Kt File into a Jar File and Running it Using Java

To build a Kotlin file into a jar file that can be run by Java runtime, use a command in the following format:

$ kotlinc hello.kt -include-runtime -d hello.jar

The “-include-runtime” switch ensures that Kotlin runtime library is bundled into the jar file. Once the build process finishes, you can run the file using the following command as the template:

$ java -jar hello.jar

Running Kt File Without Building a Jar File

You can also run a “.kt” file directly without using Java runtime. To do so, first compile “.kt” file in a “.class” file using the command below:

$ kotlinc hello.kt

Now you can run the file using the following command (without “.class” extension):

$ kotlin HelloKt

As stated earlier, “hello.kt” file is compiled into a “HelloKt.class” file when kotlin compiler is run.

Using a Third Party Jar Library with Kotlin

Importing a third party jar library in a Kotlin file is not enough to include it in the final generated build. You have to manually add it to the build command. Let’s assume a jar file is stored in the “lib” folder residing in the same directory as that of “hello.kt” file. Then you have to run a command using following template:

$ kotlinc hello.kt -cp libs/commons-text-1.7.jar -include-runtime -d hello.jar

You have to replace “libs/commons-text-1.7.jar” with the path to your own jar file.

Using Multiple Third Party Jar Libraries with Kotlin

The process of using multiple libraries is the same as above, with a small difference that you have to separate library paths with a : (colon). Unfortunately I couldn’t get wildcards to work in my testing and it seems support for it is missing, so for the time being, you may have to specify full path to each and every third party library in the build command itself.

$ kotlinc hello.kt -cp libs/commons-text-1.7.jar:libs/commons-lang3-3.9.jar
-include-runtime -d hello.jar

Conclusion

This marks the end of this article. Adoption of Kotlin took off after Google announced its inclusion in Android Studio as the preferred programming language. Even though Kotlin is mostly seen in Android apps today, there is no lack of third party libraries and projects to get you started. From UI libraries to web frameworks, many open source Kotlin projects are usable and are actively under development.

About the author

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.