How-To: Install Kotlin on Ubuntu 22.04

0
265
Book background : Vecteezy Kotlin Logo: Wikipedia Commons

Prerequisites

Install via SDKMAN

01. Open a terminal and install unzip and zip programs needed by SDKMAN.

SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix based systems

$ sudo apt update$ sudo apt install unzip$ sudo apt install zip

02.Install SDKMAN, which will be used to install Kotlin.

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

03. Run the initialization script and follow the instructions on-screen to install SDKMAN. In this example, the script is located under the /home/geek/ directory, your command will be different.

$ source "/home/geek/.sdkman/bin/sdkman-init.sh"

04. Check if SDKMAN has been installed by checking its version.

$ sdk version

05. Install Kotlin

$ sdk install kotlin

06. Verify that Kotlin is installed. The following should output the version of Kotlin installed.

$ kotlin -version

If you are greeted with the following message, then it means that Java is not installed on your system. Follow the guide here on installing Java on Ubuntu.

If installed correctly, it will show the Kotlin version.

Running Kotlin

Run a Kotlin program that shows a message to the console/output.

01. Create a file named hello-geek.kt

$ cd ~
$ nano hello-geek.kt

Set the content as follows

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

02. Compile the application using Kotlin. This creates a JAR (Java Archive) file

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

03. Run the application using Java

$ java -jar hello-geek.jar

Kotlin is a great language, as an alternative to using Java. If you are using the JVM and you want to use a different language aside from Java, this is a good option.



For enquiries, product placements, sponsorships, and collaborations, connect with us at hello@firegulaman.com. We'd love to hear from you!