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:
- 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
- Verify Java Installation
java -version
You should see output like:
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!
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
Step 2: Install Kotlin
sdk install kotlin
Step 3: Verify Installation
kotlin -version
Method 2: Using Homebrew (macOS)
Step 1: Install Homebrew (if not already installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2: Install Kotlin
brew install kotlin
Step 3: Verify Installation
kotlin -version
Method 3: Manual Installation (Windows/macOS/Linux)
Step 1: Download Kotlin Compiler
- Visit Kotlin GitHub Releases
- Download the latest
kotlin-compiler-X.X.X.zipfile
Step 2: Extract the Archive
- Windows: Extract to
C:\kotlin(or any preferred location) - macOS/Linux: Extract to
/usr/local/kotlinor~/kotlin
Step 3: Set Environment Variables
Windows
- Open System Properties → Environment Variables
- Under System Variables, find Path and click Edit
- Click New and add:
C:\kotlin\bin(or your extraction path) - Click OK to save
macOS/Linux
Add to your ~/.bashrc, ~/.zshrc, or ~/.bash_profile:
export PATH=$PATH:/usr/local/kotlin/bin
Reload the configuration:
source ~/.bashrc # or ~/.zshrc
Step 4: Verify Installation
Open a new terminal and run:
kotlinc -version
Expected output:
info: kotlinc-jvm 1.9.x (JRE 1.8.x)
Writing Your First Kotlin Program
Step 1: Create a file named hello.kt
fun main() {
println("Hello, Kotlin!")
}
Step 2: Compile the Program
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
java -jar hello.jar
Output:
Hello, Kotlin!
Alternative: Using Kotlin Script (.kts)
Kotlin Script files can be run directly without compilation:
Step 1: Create hello.kts
println("Hello from Kotlin Script!")
Step 2: Run Directly
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 - Ensure you used
-include-runtimeflag when compiling - Verify JAR file was created successfully
- First compilation is always slower (JVM warmup)
- Use Kotlin daemon for faster subsequent compilations:
- 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
Error: "Could not find or load main class"
Slow Compilation
kotlinc -daemon hello.kt