How to install Kotlin in Linux

Kotlin is a statically-typed, cross-platform, high-level programming language that has been gaining popularity in recent times. It is designed to work seamlessly with Java and has a standard library that depends on Java. This makes it a great option for developers who are already familiar with Java and want to expand their skillset. In this tutorial, provided by LinuxForDevices, we will explore the steps for installing Kotlin on Ubuntu 20.04. With a solid background in Java programming, you can easily dive into the world of Kotlin and explore its vast potential. So, let’s get started!

How to Install Kotlin in Ubuntu 20.04

First, we will need to install Java on our system. For that, you can check out this tutorial. Or you can run the below command:

sudo apt install openjdk-14-jre-headless

Now check the java version by running the below command:

java --version

Finally, we can install Kotlin by running the below command in the terminal window:

sudo snap install --classic kotlin

If, however, you do not have snap installed, you can always go ahead and install snap first by using the command:

sudo apt install snapd

Then run the previous command once more to install Kotlin onto your system!

We installed the latest Java and JDK version
We installed the latest Java and JDK version

Now that you have kotlin installed on your computer let us write a demo application and try to run it on our machine. Open your favorite text editor and copy-paste the below code:

fun main()
{
	println("hello world");
}

Now save the file with the filename- “hello.kt” make sure to have an extension of .kt at the end. Now we need to compile the file for that. Give the following command:

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

Which will generate a jar file. Now run the jar file with the help of the following command, and you will see the output in the terminal window!

java -jar hello.jar
Compile and run the Kotlin File
Compile and run the Kotlin File

For the development approach, you can install the IntelliJ IDEA software if you want. For that, follow the link and go ahead and click on the Download button. After downloading the file, extract it by right-clicking on it and clicking on extract here. Now locate the bin folder and find the idea.sh file. Open up the terminal in the folder and run the below command:

./idea.sh

This will run the IDE. Now go to “Create New Project.” Under the project SDK menu, choose your preferred SDK. and select Kotlin. Now click next. Now you will have to name your project and select a place for storing your project under the project location menu. Now click on Finish.

Intellij Idea
Intellij IDEA

In the top left-hand section, you can see the Project folder; on the right, you have your project open. To create a new file, you must right-click on the src folder, go to new, and select Kotlin File/Class. Name the file and save it, and it’ll automatically open up. Now you can write your Kotlin code.

Remember, we need to have a main function, just as in java. And then, you can test it out by using the println function. Now to run it, go running and select the main kit. And in a moment, it’ll compile and run it in a terminal for you!

Conclusion

Kotlin is now very popular amongst Mobile app developers, Web development, and data scientists. Kotlin is fully compatible with Java; hence, if you already know Java, it is very easy to pick up a newer language. It also works on multiple devices and across multiple operating systems. There is also a big community, even though the language is comparatively new. We hope you learned something new from this article, and as always, thanks for reading!