How to install Kotlin on Ubuntu 20.04

Kotlin is a modern programming language that makes developers happier. It’s an cross-platform, statically typed, general-purpose programming language with type inference.

Kotlin is designed to interoperate fully with Java, and the JVM version of Kotlin’s standard library depends on the Java Class Library but type inference allows its syntax to be more concise.

It is widely used to develop Android application. On 7 May 2019, Google announced that the Kotlin programming language is now its preferred language for Android app developers.

What is Type Inference?

Kotlin Programming language has a concept of type inference for compile-time type information, meaning some type information in the code may be omitted, to be inferred by the compiler.

There are two kinds of type inference supported by Kotlin:

  • Local type inference, for inferring types of expressions locally, in statement/expression scope.
  • Function signature type inference, for inferring types of function return values and/or parameters.

For more information on this topic you can refer the official documentation.

Support for multiplatform programming

Support for multiplatform programming is one of Kotlin’s key benefits.

It reduces time spent writing and maintaining the same code for different platforms while retaining the flexibility and benefits of native programming. This is how Kotlin Multiplatform works.

Image Credit: kotlinlang.org

Common Kotlin includes the language, core libraries, and basic tools. Code written in common Kotlin works everywhere on all platforms. With Kotlin Multiplatform libraries, you can reuse the multiplatform logic in common and platform-specific code.

Common code can rely on a set of libraries that cover everyday tasks such as HTTP, serialization, and managing coroutines.

To interop with platforms, use platform-specific versions of Kotlin.

Platform-specific versions of Kotlin (Kotlin/JVM, Kotlin/JS, Kotlin/Native) include extensions to the Kotlin language, and platform-specific libraries and tools. Through these platforms you can access the platform native code (JVM, JS, and Native) and leverage all native capabilities.

Key Features of Kotlin

  • Kotlin is Open-Source
  • Comes With Lazy-Loading Feature
  • Supports Full Java Interoperability
  • Clean and Compact Syntax
  • Kotlin is more concise than Java and you would need to write approximately 40% fewer lines of code when compared to Java.
  • It’s an is easy to learn programming language. If you have come from a Java background, you would find it easy to learn Kotlin.
  • Collection Filtering
  • Extension Functions
  • Reduces Crashes at Runtime
  • Avoiding major errors such as “NullPointException”
  • Standard Library Functions

Some of the most famous apps that are being developed using Kotling programming language are Netflix, Tinder, Pinterest, Airbnb, Uber.

Stats you should know about Kotlin in 2021

  • According to StackOverflow, Kotlin was the 4th most beloved language in 2020 with 62.9% votes.
  • Kotlin is among the top 3 languages that most businesses are planning to migrate their apps to in 2021.
  • In just three years, Kotlin is now used by 7.8% of the industry experts.
  • Now it is possible to build cross-platform apps using Kotlin.

Installing Java

Before installing Kotlin, we need to install OpenJDK. To do this follow these steps.

Type the following command to check whether Java is already installed on your system.

$ java -version

You can skip this step if Java is already installed, and if Java is not installed then follow the steps below.

In this tutorial I will install OpenJDK 11 and the installation is pretty straightforward.

First update the package index and then Install the OpenJDK 11 package by typing the following command.

$ sudo apt update && sudo apt install openjdk-11-jdk

Installing Kotlin as a Snap Package

The easiest way to install Kotlin Programming Language on Ubuntu 20.04 is by using the snap packaging system.

A snap package is a type of universal Linux package that you can enjoy irrespective of the distro. Its an self-contained software packages that include the binary all dependencies needed to run the application.

All you need is the snap service pre-configured, In the case of Ubuntu 20.04, it comes with snap pre-installed.

If snapd package is not already installed then you can install it by running following command.

$ sudo apt install snapd

Note: Snap packages can be installed from either the command-line or via the Ubuntu Software application.

This is actually the Snap version of the Kotlin. It can be used on any Linux distribution that has Snap support.

Open your terminal (Ctrl+Alt+T) and type the following command to install the Kotlin package.

$ sudo snap install kotlin --classic

The --classic option is required because the Kotlin snap requires full access to the system, like a traditionally packaged application.

Type the following command to update Kotlin package.

$ sudo snap refresh --list

Verifying the Installation

You can run the following command to verify the Kotlin installation.

$ kotlinc

Create and run an Kotlin application

Create a simple application in Kotlin that displays "Good Morning!". In your favorite editor, create a new file called welcome.kt with the following lines.

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

Then compile the application using the Kotlin compiler:

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

The -d option indicates the output path for generated class files, which may be either a directory or a .jar file.

The -include-runtime option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it.

Type the following command to see all available options:

$ kotlinc -help

Now run the application.

$ java -jar welcome.jar

Conclusion

I hope that now you have a good understanding of How to install Kotlin on Ubuntu 20.04 LTS Focal Fossa.

If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.

Source:

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment