Initial Setup
Want to start programming in Kotlin? This initial setup guide gets Kotlin installed and working on your system in minutes. By the end, you’ll have Kotlin running and will execute your first program.
This tutorial provides 0-5% coverage - just enough to get Kotlin working on your machine. For deeper learning, continue to Quick Start (5-30% coverage).
Prerequisites
Before installing Kotlin, you need:
- A computer running Windows, macOS, or Linux
- Java Development Kit (JDK) 8 or higher installed
- Administrator/sudo access for installation
- A terminal/command prompt
- A text editor (IntelliJ IDEA recommended, or any editor)
- Basic command-line navigation skills
IMPORTANT: Kotlin runs on the JVM, so Java must be installed first. If you haven’t installed Java yet, see Java Initial Setup.
Learning Objectives
By the end of this tutorial, you will be able to:
- Install the Kotlin compiler on your operating system
- Verify that Kotlin is installed correctly and check the version
- Write your first Kotlin program (Hello, World!)
- Compile Kotlin source code to JVM bytecode
- Execute Kotlin programs on the JVM
Installation Methods
Kotlin can be installed via:
- Kotlin command-line compiler - Standalone compiler for terminal use
- SDKMAN! - Version manager for JVM languages (Linux/macOS)
- Homebrew - Package manager (macOS)
- IntelliJ IDEA - IDE with built-in Kotlin support (all platforms, recommended for serious development)
We’ll cover method 1 (command-line compiler) for all platforms, as it’s the most universal.
Platform-Specific Installation
Choose your operating system and follow the installation steps.
Windows Installation
Step 1: Install Scoop (Package Manager)
Scoop simplifies Kotlin installation on Windows.
Open PowerShell and run:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh | iexStep 2: Install Kotlin via Scoop
scoop install kotlinStep 3: Verify Installation
kotlin -versionExpected output:
Kotlin version 1.9.X-release-XXX (JRE 21.X.X)Alternative: Manual Installation
- Download Kotlin compiler from https://github.com/JetBrains/kotlin/releases/latest
- Extract
kotlin-compiler-1.9.X.ziptoC:\Program Files\kotlinc\ - Add
C:\Program Files\kotlinc\binto PATH environment variable - Restart terminal and verify:
kotlinc -version
Troubleshooting Windows:
- If
kotlincnot found, check PATH includes Kotlin bin directory - Ensure JDK is installed:
java -versionshould work - Restart terminal after PATH changes
macOS Installation
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 kotlinStep 3: Verify Installation
kotlin -version
kotlinc -versionExpected output:
Kotlin version 1.9.X-release-XXX (JRE 21.X.X)Alternative: Install via SDKMAN!
SDKMAN! manages multiple JVM tool versions.
Install SDKMAN!:
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"Install Kotlin:
sdk install kotlinVerify:
kotlin -versionTroubleshooting macOS:
- If
kotlinnot found, restart terminal to load Homebrew changes - Check Homebrew installation:
brew doctor - Ensure JDK installed:
java -version
Linux Installation
Step 1: Install SDKMAN!
SDKMAN! is the recommended way to install Kotlin on Linux.
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"Step 2: Install Kotlin
sdk install kotlinStep 3: Verify Installation
kotlin -version
kotlinc -versionExpected output:
Kotlin version 1.9.X-release-XXX (JRE 21.X.X)Alternative: Manual Installation
wget https://github.com/JetBrains/kotlin/releases/download/v1.9.X/kotlin-compiler-1.9.X.zip
unzip kotlin-compiler-1.9.X.zip -d ~/kotlin
echo 'export PATH=$PATH:~/kotlin/kotlinc/bin' >> ~/.bashrc
source ~/.bashrc
kotlinc -versionAlternative: Install via Snap (Ubuntu)
sudo snap install --classic kotlinTroubleshooting Linux:
- If
kotlincnot found, ensure PATH includes Kotlin bin directory - Check SDKMAN installation:
sdk version - Verify JDK:
java -versionshould show Java 8+
Your First Kotlin Program
Let’s write and run your first Kotlin program - the classic “Hello, World!”.
Create a Project Directory
mkdir -p ~/kotlin-projects/hello
cd ~/kotlin-projects/helloWrite the Program
Create a file named HelloWorld.kt:
fun main() {
println("Hello, World!")
}Code breakdown:
fun main(): Entry point - Kotlin starts execution here (noargsparameter needed if not used)println(...): Prints text to console with newline (similar to Java’sSystem.out.println())
Save the file as HelloWorld.kt in your project directory.
Compile the Program
Kotlin code compiles to JVM bytecode:
kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jarFlags explained:
-include-runtime: Includes Kotlin runtime library in JAR (makes it standalone)-d HelloWorld.jar: Output JAR file name
This creates HelloWorld.jar containing bytecode and runtime.
Run the Program
Execute the JAR with Java:
java -jar HelloWorld.jarOutput:
Hello, World!What happened:
kotlinccompiledHelloWorld.kt→HelloWorld.jar(bytecode + runtime)javalaunched JVM to execute JAR- JVM executed
main()function println()printed output
Using Kotlin REPL (Interactive Mode)
Kotlin has a Read-Eval-Print Loop (REPL) for testing code interactively.
Start the REPL:
kotlincYou’ll see the Kotlin prompt:
Welcome to Kotlin version 1.9.X
Type :help for help, :quit for quit
>>>Try some commands:
>>> println("Hello, World!")
Hello, World!
>>> val x = 42
>>> x * 2
84
>>> fun greet(name: String) = "Hello, $name!"
>>> greet("Kotlin")
Hello, Kotlin!
>>> :quitExit REPL: Type :quit or press Ctrl+D.
Script Mode (No Compilation)
Kotlin supports script mode for quick execution without explicit compilation:
Create hello.kts (note .kts extension for script):
println("Hello from Kotlin script!")
val numbers = listOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 }
println("Doubled: $doubled")Run directly:
kotlinc -script hello.ktsOutput:
Hello from Kotlin script!
Doubled: [2, 4, 6, 8, 10]Difference:
.ktfiles: Full programs, need compilation to JAR.ktsfiles: Scripts, run directly (slower but convenient for quick tasks)
More Detailed Example
Let’s write a program with user input. Create Greeting.kt:
fun main() {
// Get user's name
print("What's your name? ")
val name = readln()
// Get user's age
print("What's your age? ")
val age = readln().toInt()
// Display greeting
println("Hello, $name! Welcome to Kotlin.")
println("In 10 years, you'll be ${age + 10} years old!")
}Compile and run:
kotlinc Greeting.kt -include-runtime -d Greeting.jar
java -jar Greeting.jarInteraction:
What's your name? Alice
What's your age? 25
Hello, Alice! Welcome to Kotlin.
In 10 years, you'll be 35 years old!Code breakdown:
readln(): Reads user input (returns String)toInt(): Converts String to Int$name: String interpolation - embeds variable in string${age + 10}: Expression interpolation - evaluates expression in string
Kotlin vs Java differences:
- No semicolons required (optional in Kotlin)
- Type inference:
val name = readln()(compiler infersStringtype) - String templates:
"Hello, $name"instead of Java’s"Hello, " + name
Understanding Kotlin Compilation
Kotlin compiles to JVM bytecode (same as Java):
HelloWorld.kt → kotlinc → HelloWorld.jar (JVM bytecode) → java → OutputWhy Kotlin uses JVM:
- Java interoperability: Call Java libraries from Kotlin and vice versa
- Mature platform: Leverage JVM’s performance optimizations and ecosystem
- Cross-platform: JVM bytecode runs on any OS with Java installed
Kotlin also compiles to:
- Kotlin/JS: JavaScript for browser/Node.js
- Kotlin/Native: Native binaries (no JVM needed)
This tutorial focuses on Kotlin/JVM.
Summary
What you’ve accomplished:
- Installed Kotlin compiler on your operating system
- Verified Kotlin installation with version checks
- Wrote and compiled your first Kotlin programs
- Executed Kotlin bytecode on the JVM
- Used Kotlin REPL for interactive experimentation
- Ran Kotlin scripts without explicit compilation
Key commands learned:
kotlinc -version- Check Kotlin compiler versionkotlin -version- Check Kotlin runtime versionkotlinc <file>.kt -include-runtime -d <output>.jar- Compile to JARjava -jar <output>.jar- Run compiled Kotlin programkotlinc- Start Kotlin REPLkotlinc -script <file>.kts- Run Kotlin script
Skills gained:
- Platform-specific Kotlin installation
- Compiling and running Kotlin programs
- Using Kotlin REPL and script mode
- Understanding Kotlin/JVM architecture
Next Steps
Ready to learn Kotlin syntax and concepts?
- Quick Start (5-30% coverage) - Touch all core Kotlin concepts in a fast-paced tour
Want comprehensive fundamentals?
Prefer code-first learning?
- By-Example Tutorial - Learn through heavily annotated examples
Want to understand Kotlin’s design philosophy?
- Overview - Why Kotlin exists and when to use it
Troubleshooting Common Issues
“kotlinc: command not found”
Problem: Terminal doesn’t recognize Kotlin commands.
Solution:
- Verify Kotlin is installed: Check installation directory
- Add Kotlin to PATH:
- Windows: Add
C:\Program Files\kotlinc\binto PATH - macOS/Linux: Add
export PATH=$PATH:~/kotlin/kotlinc/binto~/.bashrcor~/.zshrc
- Windows: Add
- Restart terminal after PATH changes
- For SDKMAN: Run
source "$HOME/.sdkman/bin/sdkman-init.sh"
“Error: Could not find or load main class”
Problem: JVM can’t find main class in JAR.
Solution:
- Ensure
-include-runtimeflag used during compilation - Verify JAR file exists:
ls HelloWorld.jar - Check
main()function is defined correctly (must be at top level or in object)
Java not found
Problem: Kotlin requires Java, but it’s not installed.
Solution:
- Install JDK 8 or higher first: See Java Initial Setup
- Verify Java installation:
java -version - Set JAVA_HOME environment variable if needed
REPL won’t start
Problem: kotlinc command hangs or fails.
Solution:
- Verify Kotlin installation:
kotlinc -version - Check JDK is accessible:
java -version - Try updating Kotlin:
sdk upgrade kotlin(if using SDKMAN)
Script execution fails
Problem: kotlinc -script gives errors.
Solution:
- Verify file has
.ktsextension (not.kt) - Check script syntax: Start with simple
println("test") - Ensure Kotlin compiler supports scripts (should be default)
Further Resources
Official Kotlin Documentation:
- Kotlin Language Site - Official website
- Kotlin Docs - Comprehensive documentation
- Kotlin Playground - Try Kotlin in browser (no installation)
Development Tools:
- IntelliJ IDEA - Best IDE for Kotlin (Community Edition free)
- Android Studio - For Android development with Kotlin
- VS Code with Kotlin extension
Community:
- Kotlin Slack - Official Slack community
- /r/Kotlin - Reddit community
- Kotlin Forum - Official discussion forum
- Stack Overflow - Kotlin - Q&A