Cyberithub

How to Install Kotlin Programming Language on Ubuntu 20.04 LTS

Advertisements

In this article, I will take you through the steps to install Kotlin Programming Language on Ubuntu 20.04 LTS. Kotlin is known to be a cross platform, modern, concise and safe programming language which makes it a easy pick for creating powerful applications immediately. Kotlin is designed to work with the Java Ecosystem so any programmer or developer working on Java platform would adapt very quickly in this programming language. Kotlin provides a rich library set due to its large community base which contributes immensely. More on Kotlin official Page

How to Install Kotlin Programming Language on Ubuntu 20.04 LTS

How to Install Kotlin Programming Language on Ubuntu 20.04 LTS

Also Read: How to Install and Use Docker on Debian 11 [Easy Steps]

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 LTS System.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get and snap utility available in Your System.

 

Step 2: Update Your System

In the first step, Install all the latest available updates from Ubuntu Repo by using apt update or apt-get update command.

root@localhost:~# apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Ign:4 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 InRelease
Get:5 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:6 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 Release
Get:7 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,341 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [560 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [275 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [279 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [14.4 kB]
Get:13 http://in.archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [569 kB]

 

Step 3: Install OpenJDK 11

Kotlin requires Java to be installed in your System in order to work. If you are using any IDE like IntelliJ Idea or Android Studio then you need not worry about this. They provide full Kotlin support with all the required components. But if you are going to use through command line then need to have JDK package installed by using apt install openjdk-11-jdk -y command as shown below. With the current Kotlin version OpenJDK 11 is fully supported.

root@localhost:~# apt install openjdk-11-jdk -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
ca-certificates-java fonts-dejavu-extra java-common libatk-wrapper-java libatk-wrapper-java-jni libice-dev libpthread-stubs0-dev libsm-dev libx11-6
libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-11-jdk-headless openjdk-11-jre openjdk-11-jre-headless x11proto-core-dev x11proto-dev
xorg-sgml-doctools xtrans-dev
Suggested packages:
default-jre libice-doc libsm-doc libx11-doc libxcb-doc libxt-doc openjdk-11-demo openjdk-11-source visualvm fonts-ipafont-gothic fonts-ipafont-mincho
fonts-wqy-microhei | fonts-wqy-zenhei
The following NEW packages will be installed:
ca-certificates-java fonts-dejavu-extra java-common libatk-wrapper-java libatk-wrapper-java-jni libice-dev libpthread-stubs0-dev libsm-dev libx11-dev
libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-11-jdk openjdk-11-jdk-headless openjdk-11-jre openjdk-11-jre-headless x11proto-core-dev
x11proto-dev xorg-sgml-doctools xtrans-dev
The following packages will be upgraded:
libx11-6
1 upgraded, 21 newly installed, 0 to remove and 316 not upgraded.

 

Step 4: Install Kotlin

Kotlin can easily be installed using snap. You just have to use snap install --classic kotlin command to install kotlin compiler along with other tools.

root@localhost:~# snap install --classic kotlin
kotlin 1.5.31 from jetbrains* installed

 

Step 5: Check Kotlin version

You can verify the currently installed version of Kotlin by using kotlin --version command. As you can see from the below output, it is 1.5.31.

root@localhost:~# kotlin -version
Kotlin version 1.5.31-release-548 (JRE 1.8.0_292-8u292-b10-0ubuntu1~16.04.1-b10)

 

Step 6: Write Your First Program

After installing Kotlin successfully, it is now time to write first Hello World Program. Here we are creating a file called hello.kt using our favorite nano editor and then using simple main() function to display Hello, World! on the output. Applications written on Kotlin programming language can be compiled using kotlinc compiler.

root@localhost:~# nano hello.kt
fun main() {
println("Hello, World!")
}

Compile Your Application

So to compile hello.kt program, you need to use kotlinc hello.kt -include-runtime -d hello.jar command as shown below. If you notice below given -include-runtime option, it is being given to include all the necessary library files which makes the resulting .jar file self-contained and runnable. The other option you can see is -d option. It indicates the output path for generated class files, which may be either a directory or a .jar file.

root@localhost:~# kotlinc hello.kt -include-runtime -d hello.jar

Run Your Application

Now to run the application, you just need to use java -jar hello.jar command.

root@localhost:~# java -jar hello.jar
Hello, World!

You can check all the other options available with kotlinc command using kotlinc -help as shown below.

root@localhost:~# kotlinc -help
Usage: kotlinc-jvm <options> <source files>
where possible options include:
-classpath (-cp) <path> List of directories and JAR/ZIP archives to search for user class files
-d <directory|jar> Destination for generated class files
-expression (-e) Evaluate the given string as a Kotlin script
-include-runtime Include Kotlin runtime into the resulting JAR
-java-parameters Generate metadata for Java 1.8 reflection on method parameters
-jdk-home <path> Include a custom JDK from the specified location into the classpath instead of the default JAVA_HOME
-jvm-target <version> Target version of the generated JVM bytecode (1.6 (DEPRECATED), 1.8, 9, 10, 11, 12, 13, 14, 15 or 16), default is 1.8
-module-name <name> Name of the generated .kotlin_module file
-no-jdk Don't automatically include the Java runtime into the classpath
-no-reflect Don't automatically include Kotlin reflection into the classpath
-no-stdlib Don't automatically include the Kotlin/JVM stdlib and Kotlin reflection into the classpath
-script-templates <fully qualified class name[,]>
Script definition template classes
-Werror Report an error if there are any warnings

 

Step 7: Uninstall Kotlin

Once you are done using Kotlin then you can use snap remove kotlin command to remove this snap from your System.

root@localhost:~# snap remove kotlin
kotlin removed

Leave a Comment