Initial Setup
Get Kotlin running quickly. This tutorial provides the quickest path to running your first Kotlin program. You’ll install the JDK, set up IntelliJ IDEA, create a Gradle project, and run a simple “Hello World” application.
What You’ll Achieve
By the end of this tutorial, you’ll have:
- β Java Development Kit (JDK 17 or later) installed on your system
- β IntelliJ IDEA Community Edition installed and configured
- β Kotlin 2.3.0 plugin enabled and working
- β Created your first Kotlin + Gradle project
- β Run your first Kotlin application successfully
Prerequisites
- Basic command line familiarity - You should know how to open a terminal/command prompt
- No programming experience required - This tutorial assumes you’re completely new to Kotlin
Learning Path
This tutorial follows a step-by-step path to get you running quickly:
graph TD
A["1. Install JDK"] --> B["2. Install IntelliJ IDEA"]
B --> C["3. Create Gradle Project"]
C --> D["4. Write Hello World"]
D --> E["5. Run & Verify"]
E --> F["Ready for Quick Start!"]
style A fill:#0173B2,stroke:#000000,color:#FFFFFF
style B fill:#DE8F05,stroke:#000000,color:#FFFFFF
style C fill:#0173B2,stroke:#000000,color:#FFFFFF
style D fill:#DE8F05,stroke:#000000,color:#FFFFFF
style E fill:#0173B2,stroke:#000000,color:#FFFFFF
style F fill:#029E73,stroke:#000000,color:#FFFFFF
Why Learn Kotlin?
Kotlin is a modern, pragmatic programming language that combines productivity with safety:
- Android development - Google’s preferred language for Android apps, with full IDE support
- Backend services - Server-side applications with Ktor, Spring Boot, and other JVM frameworks
- Multiplatform development - Share code between JVM, Android, iOS, and web (advanced use case)
- Conciseness - Write less code than Java while maintaining readability
- Null safety - Eliminate null pointer exceptions through the type system
- JVM interoperability - Use any Java library seamlessly, leverage the entire Java ecosystem
Kotlin runs on the Java Virtual Machine (JVM), giving you access to decades of mature libraries and tools while providing modern language features like null safety, extension functions, and coroutines.
Step 1: Install the Java Development Kit (JDK)
Kotlin compiles to JVM bytecode, so you need a JDK installed.
Choose Your Installation Method
We recommend OpenJDK 17 or later - the free, open-source Java implementation.
Option A: Download from Adoptium (Recommended)
Adoptium provides high-quality, free OpenJDK binaries for all platforms.
- Visit https://adoptium.net/
- Select “JDK 21 (LTS)” from the version dropdown (or JDK 17 minimum)
- Download the installer for your operating system
- Run the installer and follow the prompts
Option B: Use a Package Manager
macOS (Homebrew):
brew install openjdk@21After installation, link it to your system:
sudo ln -sfn /opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdkLinux (Ubuntu/Debian):
sudo apt update
sudo apt install openjdk-21-jdkLinux (Fedora/RHEL):
sudo dnf install java-21-openjdk-develWindows (Chocolatey):
choco install openjdk21Platform-Specific Installation Notes
Windows
- The installer will typically install to
C:\Program Files\Eclipse Adoptium\jdk-21.x.x\ - The installer should automatically set your
PATHenvironment variable - If not, you’ll need to manually add the
bindirectory to your PATH
macOS
- JDK is installed to
/Library/Java/JavaVirtualMachines/ - The
javacommand should be available immediately after installation - You may need to restart your terminal
Linux
- Package managers handle PATH configuration automatically
- JDK is typically installed to
/usr/lib/jvm/ - No additional configuration needed
Verify JDK Installation
Open a terminal (or command prompt on Windows) and run:
java -versionExpected output (version numbers may vary):
openjdk version "21.0.1" 2023-10-17 LTS
OpenJDK Runtime Environment Temurin-21.0.1+12 (build 21.0.1+12-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.1+12 (build 21.0.1+12-LTS, mixed mode, sharing)Troubleshooting JDK Installation
“Command not found” error?
- Windows: Add JDK
bindirectory to PATH (e.g.,C:\Program Files\Eclipse Adoptium\jdk-21.x.x\bin) - macOS/Linux: Your package manager should have configured PATH automatically. Try restarting your terminal
Wrong version displayed?
- You may have multiple Java versions installed
- Use
which java(macOS/Linux) orwhere java(Windows) to see which version is active - Uninstall older versions or update your PATH to prioritize JDK 17+
Step 2: Install IntelliJ IDEA Community Edition
IntelliJ IDEA is the recommended IDE for Kotlin development, with excellent language support and productivity features.
Download and Install
- Visit https://www.jetbrains.com/idea/download/
- Download IntelliJ IDEA Community Edition (free)
- Run the installer and follow the prompts
- Launch IntelliJ IDEA when installation completes
Initial Setup
When you first launch IntelliJ IDEA:
- Choose your UI theme (light or dark)
- Skip featured plugins for now (you can install them later)
- Click “New Project” on the welcome screen
Important: IntelliJ IDEA Community Edition includes the Kotlin plugin by default. You don’t need to install it separately.
Verify Kotlin Plugin
To confirm the Kotlin plugin is enabled:
- Open IntelliJ IDEA
- Go to File β Settings (Windows/Linux) or IntelliJ IDEA β Preferences (macOS)
- Navigate to Plugins
- Search for “Kotlin”
- Verify the Kotlin plugin shows as “Installed” and “Enabled”
If it’s not enabled, click the checkbox to enable it and restart IntelliJ IDEA.
Step 3: Create Your First Kotlin Project
We’ll create a Kotlin project using Gradle, the standard build tool for Kotlin.
Create New Project
- Click “New Project” from the welcome screen (or File β New β Project)
- Select “Kotlin” from the left sidebar
- Choose “Kotlin/JVM” as the project type
- Configure project settings:
- Name:
hello-kotlin - Location: Choose a directory (e.g., your Documents or projects folder)
- JDK: Select your installed JDK 17+ (IntelliJ should auto-detect it)
- Build System: Gradle Kotlin
- Gradle DSL: Kotlin
- Name:
- Click “Create”
IntelliJ IDEA will create the project structure and download Gradle and Kotlin dependencies. This may take a minute.
Understanding the Project Structure
After creation, you’ll see this structure:
hello-kotlin/
βββ build.gradle.kts # Gradle build configuration (Kotlin DSL)
βββ settings.gradle.kts # Gradle settings
βββ gradle/ # Gradle wrapper files
β βββ wrapper/
βββ gradlew # Gradle wrapper script (macOS/Linux)
βββ gradlew.bat # Gradle wrapper script (Windows)
βββ src/
βββ main/
βββ kotlin/ # Kotlin source files go hereKey files:
build.gradle.kts- Defines project dependencies, Kotlin version, and build configurationsrc/main/kotlin/- Where you write your Kotlin codegradlew/gradlew.bat- Gradle wrapper scripts that run Gradle (no separate installation needed)
Step 4: Write Your First Kotlin Program
Let’s create a simple “Hello World” program.
Create a Kotlin File
- Right-click on
src/main/kotlin/in the Project pane - Select New β Kotlin Class/File
- Choose “File” (not Class)
- Name it
Main(createsMain.kt)
Write the Code
In Main.kt, write:
fun main() {
println("Hello, Kotlin!")
}That’s it! Kotlin is significantly more concise than Java.
Understanding the Code (Brief)
fun main()- The entry point where Kotlin starts executionprintln()- Prints text to the console (no need forSystem.out)- No class required for a simple program (unlike Java)
- No semicolons needed
Note: You’ll learn these concepts in detail in the Quick Start and Beginner Guide tutorials. For advanced function patterns, see Intermediate Kotlin.
Comparison with Java
If you know Java, here’s the equivalent Java code:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Kotlin!");
}
}Kotlin eliminates:
- Class boilerplate for simple programs
public statickeywordsString[] argsparameter (added only if needed)System.outprefix- Semicolons
This conciseness is one of Kotlin’s key advantages.
Step 5: Run the Program
Using IntelliJ IDEA (Recommended)
- Click the green play button (βΆ) in the gutter next to
fun main() - Select “Run ‘MainKt’” from the menu
Expected output in the Run panel at the bottom:
Hello, Kotlin!
Process finished with exit code 0Using Gradle (Command Line)
Alternatively, you can run from the terminal:
macOS/Linux:
./gradlew runWindows:
.\gradlew.bat runNote: You may need to add a run task to build.gradle.kts first:
tasks.register<JavaExec>("run") {
mainClass.set("MainKt")
classpath = sourceSets["main"].runtimeClasspath
}For now, using IntelliJ IDEA’s run button is simpler.
Step 6: Understanding the Build Process
When you click “Run”, IntelliJ IDEA:
- Compiles your Kotlin code to JVM bytecode (
.classfiles) - Places compiled files in
build/classes/kotlin/main/ - Executes the bytecode using the JVM
You can see the compiled output:
- Navigate to
hello-kotlin/build/classes/kotlin/main/in your file explorer - Find
MainKt.class- the compiled bytecode
Kotlin compiles to the same bytecode format as Java, which is why Kotlin can use Java libraries seamlessly.
Verification Checklist
Confirm you can do all of the following:
- β
Run
java -versionand see Java 17 or later - β Launch IntelliJ IDEA successfully
- β Kotlin plugin is installed and enabled
- β Create a new Kotlin/JVM project with Gradle
- β
Write a simple
fun main()program - β Run the program and see output in the Run panel
If all checkboxes are checked, you’re ready to start learning Kotlin!
π― Quick Challenge
Now that you have Kotlin working, try these challenges to solidify your understanding:
Challenge 1: Modify the Hello World Program
- Edit
Main.ktand change the message to print your name instead of “Hello, Kotlin!” - Run it again and verify your name appears
Success criteria:
- Program compiles without errors
- Program runs without errors
- Your custom message displays
Challenge 2: Create a Second Function
Try adding another function to Main.kt:
fun greet(name: String) {
println("Welcome, $name!")
}
fun main() {
println("Hello, Kotlin!")
greet("Developer")
}- Add the
greetfunction abovemain() - Call it from
main()as shown - Run the program
- Verify both messages appear
Expected output:
Hello, Kotlin!
Welcome, Developer!Challenge 3: Simple Calculation
Create a new file Calculator.kt and write:
fun main() {
val x = 10
val y = 20
println("Sum: ${x + y}")
}- Create the file using New β Kotlin Class/File β File
- Copy the code above
- Run it (click the play button next to this new
main()) - Verify the output shows the correct sum (30)
What you’re learning:
valdeclares an immutable variable (likefinalin Java)${}is string interpolation (embeds expressions in strings)- You can have multiple files with
main()in the same project
These challenges confirm that your Kotlin setup is complete and working properly.
Understanding Gradle (Brief)
Gradle is Kotlin’s standard build tool. It handles:
- Dependency management - Downloading libraries your project needs
- Compilation - Converting Kotlin to bytecode
- Testing - Running automated tests
- Packaging - Creating executable JARs
Open build.gradle.kts to see the configuration:
plugins {
kotlin("jvm") version "2.3.0" // Kotlin version
}
repositories {
mavenCentral() // Where to download dependencies
}
dependencies {
testImplementation(kotlin("test")) // Testing library
}Key points:
kotlin("jvm")- Configures Kotlin for JVM (bytecode) compilation- Version
"2.3.0"- The Kotlin compiler version used mavenCentral()- The repository for downloading librariesdependencies {}- Where you add libraries your project needs
You’ll learn more about Gradle as you progress through the tutorials. For build configuration best practices, see Kotlin Best Practices. For common Gradle tasks, check the Kotlin Cookbook.
Next Steps
Congratulations! You have a working Kotlin development environment.
Continue Learning
Kotlin Quick Start - Learn 12 core Kotlin concepts to explore independently. Perfect if you want a fast overview of Kotlin syntax and features.
Complete Beginner’s Guide to Kotlin - Comprehensive foundation covering Kotlin fundamentals from zero. Choose this if you want in-depth coverage with practice exercises.
What’s Next?
The Quick Start tutorial teaches you:
- Variables (val vs var) and type inference
- Null safety system (?, ?., ?:, !!)
- Functions and lambdas
- Classes, data classes, and objects
- Collections and extension functions
- Coroutines basics
The Beginner tutorial provides comprehensive coverage:
- Complete type system and null safety
- Object-oriented programming (classes, inheritance, interfaces)
- Functional programming fundamentals
- Collections and sequences
- Error handling and testing
- Package organization
- Practice projects and exercises
Choose Quick Start for a fast overview, or jump to Beginner for thorough, comprehensive learning.
Additional Resources
Internal Resources:
- Kotlin Cheat Sheet - Quick reference for Kotlin syntax
- Kotlin Glossary - Technical terms explained
- Kotlin Learning Resources - External learning materials
- Kotlin Anti-Patterns - Common mistakes to avoid
Official Kotlin Documentation: https://kotlinlang.org/docs/home.html
Kotlin Language Reference: https://kotlinlang.org/spec/
Kotlin Playground (try code in browser): https://play.kotlinlang.org/
Kotlin by Example: https://play.kotlinlang.org/byExample/overview
Tutorial Complete! You’re now ready to start learning Kotlin programming.