Environment Setup - Kotlin Tutorial
Kotlin Course / Introduction / Environment Setup

Environment Setup

BLUF: Mastering Environment Setup is a fundamental step in modern Android and server-side development. This lesson covers the concise syntax and powerful features of Kotlin that make this concept easy to implement.
Modern Expressive Code: Environment Setup

Kotlin's safety features and expressive syntax reduce boilerplate. See how Environment Setup makes your code cleaner and more reliable in the tutorial below.

This guide covers setting up Kotlin for command-line development on Windows, macOS, and Linux. For IDE-based development (recommended for beginners), see the next section.

Prerequisites

Before installing Kotlin:

  1. Install JDK 8 or later
  • Kotlin runs on the JVM, so you need Java Development Kit (JDK) version 8 or higher
  • Download from Oracle or OpenJDK
  1. Verify Java Installation
Example

java -version

You should see output like:

Example

java version "1.8.0_xxx" or higher

Installing Kotlin Compiler

Method 1: Using SDKMAN! (Recommended for macOS/Linux)

SDKMAN! is a tool for managing parallel versions of multiple SDKs.

Step 1: Install SDKMAN!

Example

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"

Step 2: Install Kotlin

Example

sdk install kotlin

Step 3: Verify Installation

Example

kotlin -version

Method 2: Using Homebrew (macOS)

Step 1: Install Homebrew (if not already installed)

Example

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

Step 2: Install Kotlin

Example

brew install kotlin

Step 3: Verify Installation

Example

kotlin -version

Method 3: Manual Installation (Windows/macOS/Linux)

Step 1: Download Kotlin Compiler

Step 2: Extract the Archive

  • Windows: Extract to C:\kotlin (or any preferred location)
  • macOS/Linux: Extract to /usr/local/kotlin or ~/kotlin

Step 3: Set Environment Variables

Windows

  1. Open System PropertiesEnvironment Variables
  2. Under System Variables, find Path and click Edit
  3. Click New and add: C:\kotlin\bin (or your extraction path)
  4. Click OK to save
  5. macOS/Linux

Add to your ~/.bashrc, ~/.zshrc, or ~/.bash_profile:

Example

export PATH=$PATH:/usr/local/kotlin/bin

Reload the configuration:

Example

source ~/.bashrc  # or ~/.zshrc

Step 4: Verify Installation

Open a new terminal and run:

Example

kotlinc -version

Expected output:

Output

info: kotlinc-jvm 1.9.x (JRE 1.8.x)

Writing Your First Kotlin Program

Step 1: Create a file named hello.kt

Example

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

Step 2: Compile the Program

Example

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

Explanation:

  • kotlinc: Kotlin compiler
  • hello.kt: Source file
  • -include-runtime: Includes Kotlin runtime in the JAR
  • -d hello.jar: Output JAR file name

Step 3: Run the Program

Example

java -jar hello.jar

Output:

Output

Hello, Kotlin!

Alternative: Using Kotlin Script (.kts)

Kotlin Script files can be run directly without compilation:

Step 1: Create hello.kts

Example

println("Hello from Kotlin Script!")

Step 2: Run Directly

Example

kotlinc -script hello.kts

Common Commands

Command Description
kotlinc file.kt Compile Kotlin source file
kotlinc -include-runtime -d output.jar file.kt Compile to executable JAR
kotlinc -script file.kts Run Kotlin script
kotlin ClassName Run compiled Kotlin class
java -jar output.jar Run compiled JAR file

Troubleshooting

Error: "kotlinc is not recognized"

  • Verify Kotlin bin directory is in PATH
  • Restart terminal after setting environment variables
  • On Windows, check if path uses backslashes: C:\kotlin\bin
  • Error: "Could not find or load main class"

  • Ensure you used -include-runtime flag when compiling
  • Verify JAR file was created successfully
  • Slow Compilation

  • First compilation is always slower (JVM warmup)
  • Use Kotlin daemon for faster subsequent compilations:
Example

kotlinc -daemon hello.kt

Next Steps

  • For Beginners: Install IntelliJ IDEA or Android Studio for better development experience
  • Try Online: Use Kotlin Playground to experiment without setup
  • Learn Basics: Move on to variables, data types, and control flow

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience