How to Install Kotlin on Linux
How to Install Kotlin on Linux

Kotlin is a cross-platform programming language that is supported by various operating systems, including Linux. You can develop Kotlin applications on any operating system, including Linux, and run them on different platforms.

How to Install Kotlin on Linux

Kotlin is a cross-platform programming language that is supported by various operating systems, including Linux. You can develop Kotlin applications on any operating system, including Linux, and run them on different platforms.

Moreover, many different Kotlin development tools are available on Linux, such as IntelliJ IDEA, Eclipse, NetBeans, and Android Studio. You can use the command line to work with Kotlin applications.

Let’s find out how to install Kotlin on Linux.

Getting ready to install Kotlin on Ubuntu

Ubuntu is a distribution based on Debian Linux. Before you start installing Kotlin, you must have:

1) An Ubuntu 20.04 LTS system must be up and running.

2) Your account must have administrator rights to run commands like sudo root.

3) The apt or apt-get must be installed on your system.

Moreover, it is mandatory to update the system before initiating the installation process. To do so, you can utilize the apt update or apt-get update command. Those commands help install all the latest updates available from the Ubuntu repository. You can execute the following command to accomplish this:

$sudo apt update

To ensure the optimal performance of Kotlin, it is crucial to have OpenJDK 11 properly set up on your system. If you happen to utilize an Integrated Development Environment (IDE) like IntelliJ IDEA or Android Studio, you can breathe easy as these IDEs come bundled with Kotlin support, containing all the essential elements. However, if your plan involves utilizing the command line interface, it becomes imperative to install the JDK package. Execute the subsequent command to fulfil this requirement:

$sudo apt install openjdk-11-jdk -y

After that, you can check the Java version.

$java --version

Once you have completed all the prerequisites, you can proceed with installing Kotlin on your Ubuntu system.

Installation options

There are several options for installing Kotlin in Ubuntu 20.04.

Option 1: With the Snap package

The easiest way to install Kotlin on Ubuntu is to use the official Kotlin Snap package. To install it, run the next command:

$sudo snap install --classic kotlin

In that case, if you haven’t installed the Snap yet, you can install it using the next command:

$sudo apt install snapd

And after the installation is complete, you need to repeat the previous command:

$sudo snap install --classic kotlin

Checking the installed version of Kotlin:

$kotlin -version

This command will help make sure everything is installed correctly

Option 2: Manual Installing

If you prefer not to utilize Snap packages, there is an alternative method to install Kotlin on your Ubuntu or any other Linux distribution. This is possible with the help of the special utility – SDKMAN. To use SDKMAN, you need to install it on your PC. If it doesn’t exist, you can execute this code:

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

After executing the command, be sure to restart your PC as without doing so, you won’t be able to complete the next step.

After installing SDKMAN, you need to update it:

$source "$HOME/.sdkman/bin/sdkman-init.sh"

Where “HOME” is an environment variable that points to the home directory of the current user on the system.

Now you may install Kotlin and check the version:

$sdk install kotlin 
$kotlin -version

Checking the installation of the Kotlin compiler

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

$kotlin

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

Create an executable .jar file from a .kt file by using kotlinc

Once you have Kotlin installed, you can try writing your first “MyFirstTest” program. To do this, create a file with the .kt extension using any text editor. For example: “MyFirstTest.kt”. For it, you can write code that displayed the phrase “My first test program” on the screen:

fun main() {
    print("My first test program")
}

Kotlin programs can be compiled using the kotlinc compiler. To do this, you need to go to the folder where the created file is located.

And now, run the following command:

$kotlinc MyFirstTest.kt -include-runtime -d outPutMyFirstTest.jar

The –include-runtime option, includes all the necessary libraries, making the resulting .jar file standalone and ready to run. Another option you might notice is using the -d option, which specifies the path to save the generated class files, which can be either a directory or a .jar file.

Once the Kotlin file is successfully compiled into a JAR file, you can run it using the Java Virtual Machine (JVM) with the following command:

$java -jar outPutMyFirstTest.jar

Result: My first test program

Running a .kt file

A .kt file can’t just be run because it’s just a Kotlin source code file. To run the .kt file, you can follow the instructions from the previous topic “Compiling a .kt file to a .jar file and running it with Java”.

Using third-party Jar library with Kotlin

You must manually add the jar file to the build command. Suppose the jar file is stored in the “lib” folder in the same directory as the “MyFirstTest.kt” file. Now use the following template:

$kotlinc MyFirstTest.kt -cp MyFirstTest.jar

Where <path-to-jar-file> is the path to the .jar file with your third-party library. 

Now you know how to install Kotlin programming language on Ubuntu and will be up to the task. Also, you can remove Kotlin if you need to.

$snap remove kotlin

FAQ

How to install Kotlin in the terminal?

Manually install Kotlin using SDKMAN, but make sure you have already installed SDKMAN. Run the following commands to install Kotlin and check its version:

$sdk install kotlin 

$kotlin -version 

The second approach, you can install Kotlin from the Snap package, but make sure Snap is installed first. Now install Kotlin and check the version using the subsequent commands:

$sudo snap install –classic kotlin 

$kotlin -version 

Can I run Kotlin on Linux?

Kotlin is a cross-platform programming language that is supported by various operating systems, including Linux. You can develop Kotlin applications on any operating system, including Linux, and run them on these platforms.

How do I run Kotlin in the Linux terminal?

In the terminal, run the following command to execute the Kotlin program:

$kotlin -classpath outPutMyFirstTest.jar MyFirstTestKt 

This command runs the Kotlin program using the Kotlin runtime, specifying the classpath and the name of the Kotlin file (MyFirstTestKt is the file that contains the main function).

Note: Ensure you have Java installed on your Linux system since Kotlin runs on the Java Virtual Machine (JVM). If you don’t have Java installed, you can install it using your package manager or download it from the official website.

How to run Kotlin in the Ubuntu terminal?

This is the same as for Linux. If you haven’t yet had the .jar file:

Create a simple Kotlin application that displays ”My first test program”.

Compile the application with the kotlinc compiler using the command:

$kotlinc MyFirstTest.kt -include-runtime -d outPutMyFirstTest.jar 

Launch the application.

$java -jar outPutMyFirstTest.jar