Getting Started with Kotlin

Kotlin is a modern programming language mainly used for Android application development.

You can run Kotlin on your computer using the following two methods:

  • Run Kotlin online
  • Install Kotlin on your computer

In this tutorial, you will learn both methods.

Run Kotlin Online

To run Kotlin code, you must install the Kotlin Compiler on your system. However, you can use a free online Kotlin compiler if you want to start immediately.

The online editor enables you to run Kotlin code directly in your browser—no installation is required.


Install Kotlin on Your Computer

For those who prefer to install Kotlin on your computer, this guide will walk you through the installation process on Windows, macOS, or Linux (Ubuntu).

To install Kotlin on your Windows, just follow these steps:

  1. Install VS Code
  2. Download the Kotlin Compiler Zip File
  3. Extract the Downloaded File
  4. Add the Compiler to Your PATH
  5. Verify the Installation

Here is a detailed explanation of each of the steps:

Step 1: Install VS Code

Go to the VS Code official website and download the Windows installer. Once the download is complete, run the installer and follow the installation process.

Click Finish to complete the installation process.

Step 2: Download the Kotlin Compiler Zip File

Navigate to the Kotlin Command Line documentation page and follow the link to the Kotlin Releases page on GitHub.

Kotlin Command-line Compiler
Kotlin Command-line Compiler

The most recent release is under the "Assets" section. To download it, download a file similar to kotlin-compiler-x.x.x.zip, where x.x.x represents the version number.

Most Recent Github Release
Most Recent Github Release

Step 3: Extract the Downloaded File

Once the download is complete, extract the zip file to your preferred location. The zip file contains the Kotlin compiler executables.

Extract the Downloaded Zip File
Extract the Downloaded Zip File

Once the downloaded file is extracted, you will see the following folder.

kotlinc Folder
kotlinc Folder

Step 4: Add the Compiler to Your PATH

To make Kotlin compiler commands available from any command prompt in Windows, add the compiler's directory to the system's PATH environment variable.

Follow these steps to add the PATH environment variable.

1. Copy the path in the Kotlin compiler's bin folder. This is typically found where you extract the Kotlin compiler. (Here, it is C:\Users\user\Downloads\kotlin-compiler-2.0.20\kotlinc\bin)

2. Right-click on This PC and select Properties.

3. Click on Advanced System Settings and then Environment Variables.

Edit Environment Variables
Edit Environment Variables

4. Under System Variables, find and select Path, then click Edit.

System Variable
System Variable

5. Click New and paste the path.

Paste the Path to Kotlin Compiler's bin Folder
Paste the Path to Kotlin Compiler's bin Folder

6. Click OK to close all dialogs and apply the changes.

Step 5: Verify the Installation

After installation, open a new Command Prompt window to ensure everything's set up correctly.

Run the following command.

kotlin -version

And it should now display the version you've installed.

Kotlin Version
Kotlin Version

Note: The version number might differ from the one above, depending on your installed version.

Now, you are all set to run Kotlin programs on your device.

To install Kotlin on your Mac, just follow these steps:

  1. Install VS Code
  2. Install Homebrew (if not installed)
  3. Install Kotlin
  4. Verify the installation.

Here is a detailed explanation of each of the steps:

Step 1: Install VS Code

Go to the VS Code official website and download the zipped file. Once the download is complete, open the zipped file.

In Finder, open a new window and navigate to the Applications folder. To install the VS Code application from the zip file, drag it into the Applications folder.

You can now launch VS Code directly from the Applications folder.

Step 2: Install Homebrew

Homebrew is a package manager that simplifies the installation of software on macOS. Open the Terminal app and type the following command to install Homebrew (if it's not already installed):

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Homebrew Installation on Mac
Homebrew Installation on Mac

When this installation is complete, we will use homebrew to install Kotlin.

Step 3: Install Kotlin

Now that Homebrew is installed, you can easily install Kotlin. In the Terminal, enter the following command:

brew install kotlin

This command installs the Kotlin compiler and all necessary tools. Once the installation is complete, you can run Kotlin code from the command line.

Kotlin Installation on Mac
Kotlin Installation on Mac

Step 4: Verify the installation

Once all the above processes are complete, exit the current terminal session and open a new one to verify installation.

To verify successful installation, run the following command:

kotlin -version
Kotlin Installation Verification for Mac
Kotlin Installation Verification for Mac

Note: The version number might differ from the one above, depending on your installed version.

Linux has various distributions, and the installation process differs slightly from each other. For now, we will focus on Ubuntu.

To install Kotlin, follow these steps:

  1. Install VS Code
  2. Install Curl
  3. Install SDKMAN
  4. Install Kotlin
  5. Verify Installation

Here is a detailed explanation of each of the steps:

Step 1: Install VS Code

Open the Terminal and type:

sudo apt update

This command updates your package lists to ensure you get the latest versions of your software.

Proceed to install VS Code with:

sudo snap install code --classic

Step 2: Install Curl

To download Kotlin, we'd need SDKMAN, and to install SDKMAN, we need Curl.

Download Curl using the following command:

sudo apt install curl
Install Curl on Ubuntu
Install Curl on Ubuntu

Step 3: Install SDKMAN

To install SDKMAN, run the following command on your terminal.

curl -s "https://get.sdkman.io" | bash
Install SDKMAN  on Ubuntu
Install SDKMAN on Ubuntu

Follow the on-screen instructions to complete the installation. Afterward, open a new terminal and run the following command.

source "$HOME/.sdkman/bin/sdkman-init.sh"
Run the script in the current shell Ubuntu
Run the script in the current shell Ubuntu

Step 4: Install Kotlin

To install the Kotlin compiler, run the following command in the terminal:

sdk install kotlin
Install Kotlin on Ubuntu
Install Kotlin on Ubuntu

Step 5: Verify Installation

Verify the installation of Kotlin by running the following command on your terminal.

Kotlin
Verify Kotlin Installation on Ubuntu
Verify Kotlin Installation on Ubuntu

Now, you can run the Kotlin program on your device.


Run Your First Kotlin Program

You must set up a few things to run your first Kotlin program.

  • Install the Code Runner and Kotlin extension in VS Code.
  • Create a new file
  • Write Your Kotlin Program
  • Run Your Program

First, open VS Code, click on the File in the top menu and then select New File.

Create a New File in VS Code
Create a New File in VS Code

Then, save this file with a .kt extension by clicking on File again, then Save As, and type your filename ending in .kt. (Here, we are saving it as hello.kt).

Install the Required Extension in VS Code

Before coding, ensure the Kotlin extension is installed in VS Code. Open VS Code and click on Extensions on the left sidebar. Search for the Kotlin extension and click on Install.

Install Kotlin VS Code Extension
Install Kotlin VS Code Extension

This extension provides Kotlin language support, debugging, and more, helping you develop Kotlin applications efficiently.

To run the Kotlin code directly from VS Code, we'll need the Code Runner Extension. So, follow similar steps to install the Code Runner Extension.

Install Code Runner Extension in VSCode
Install Code Runner Extension in VSCode

Now, write the following code into your file:

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

Then click the run button on the top right side of your screen.

Run Kotlin Program on Ubuntu with VS Code
Run Kotlin Program on Ubuntu with VS Code

You should see Hello, World written on the console.

Now that you have set everything up to run a Kotlin program on your computer, you'll learn how the basic program works in Kotlin in the next tutorial.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges