Kotlin Installation Guide
Prerequisites
Before installing Kotlin, make sure your computer meets the basic system requirements. Kotlin runs on multiple operating systems including Windows, macOS, Linux, and other Unix-like systems. Regardless of your operating system, ensure you have enough disk space to install development tools and run compiled programs.
For Windows users, Windows 10 or higher is recommended for the best experience. macOS users need macOS 10.14 or higher. Linux users can use any major distribution such as Ubuntu, Fedora, or Debian.
Installation via IDE
IntelliJ IDEA
IntelliJ IDEA is JetBrains' flagship integrated development environment and provides the most complete support for Kotlin. If your primary goal is Kotlin development, IntelliJ IDEA is the best choice.
IntelliJ IDEA comes in two versions: Community (free and open source) and Ultimate (for enterprise users with advanced features). The Community edition is free and provides sufficient features for Kotlin development. The Ultimate edition offers additional enterprise-level features such as advanced database tools and enterprise security features.
To install, visit the JetBrains official website (https://www.jetbrains.com/idea/download/) and download the appropriate installer for your operating system. After downloading, run the installer and follow the wizard to complete the installation. During installation, you can choose the installation path and create desktop shortcuts.
When you first launch IntelliJ IDEA, the IDE will automatically detect and prompt you to install the Kotlin plugin. If it doesn't auto-detect, you can manually search for Kotlin in the plugin marketplace and install it. After installation, restart the IDE to begin Kotlin development.
Android Studio
Android Studio is Google's official Android development IDE with built-in Kotlin support. If you primarily develop for Android, Android Studio is the ideal choice.
Visit the Android Developer website (https://developer.android.com/studio) to download Android Studio. After downloading, run the installer to complete the installation. Android Studio includes the Kotlin plugin by default, so no additional configuration is needed to start developing.
Android Studio releases multiple update versions each year, each containing the latest Kotlin support and Android development tools. It is recommended to keep your version updated for the best development experience and latest language features.
Installation via Command Line
In addition to using an IDE, you can also install the Kotlin command-line compiler. This approach is suitable for developers who prefer a lightweight development environment or need Kotlin for server-side development.
Manual Installation
First, visit the Kotlin GitHub Releases page (https://github.com/JetBrains/kotlin/releases) to download the latest compiler version. The download is a zip archive; extract it to a desired directory.
After extraction, add the kotlinc/bin directory to your system's PATH environment variable so you can use the kotlinc command from the command line. On Windows, add the path through System Properties > Environment Variables. On macOS and Linux, modify your ~/.bashrc or ~/.zshrc file and add a line like:
export PATH="$PATH:/path/to/kotlinc/bin"
After adding the path, open a new terminal window and enter kotlinc -version. If you see version information, the installation was successful.
Using SDKMAN!
SDKMAN! is a tool for managing Java development tool versions and works conveniently on Unix-like systems. If you use macOS, Linux, Cygwin, FreeBSD, or Solaris, SDKMAN! is the recommended way to install Kotlin.
First, install SDKMAN! by running this command in your terminal:
curl -s "https://get.sdkman.io" | bash
After installation, reload the environment configuration:
source ~/.sdkman/bin/sdkman-init.sh
Now you can use the sdk command to install Kotlin:
sdk install kotlin
During installation, you'll be prompted to confirm the version. After confirming, installation is complete. You can verify the installation with kotlinc -version.
Using Homebrew
On macOS, you can also use the Homebrew package manager to install Kotlin. First, ensure Homebrew is installed, then run:
brew update brew install kotlin
Homebrew will automatically download and install the latest Kotlin compiler. Verify the installation with kotlinc -version.
Using Snap
On Ubuntu 16.04 and later, you can use Snap to install Kotlin:
sudo snap install --classic kotlin
Snap installs Kotlin as a system-wide application, accessible from anywhere via command line.
Creating Your First Kotlin Program
Regardless of which installation method you choose, you can create and run your first Kotlin program with these steps.
First, create a file named hello.kt with the following content:
fun main() { println("Hello, World!") }
This is the simplest Kotlin program, defining a main function which is the entry point of a Kotlin program. The println function outputs text to the console.
If using the command-line compiler, compiling and running this program requires two steps. First, compile:
kotlinc hello.kt -include-runtime -d hello.jar
The -include-runtime parameter packages the Kotlin runtime library into the generated jar file, making it self-contained. The -d parameter specifies the output file path.
After compilation, run the program:
java -jar hello.jar
You should see "Hello, World!" output in the console.
If you don't want to generate a standalone jar file, you can also use kotlinc's interactive running feature:
kotlinc -script hello.kt
Or use the kotlin command to run compiled class files directly:
kotlin -classpath hello.jar HelloKt
Here, HelloKt is the main class name generated by the Kotlin compiler for the file named hello.kt.
Using REPL
Kotlin provides an interactive REPL (Read-Eval-Print Loop) environment where you can enter Kotlin code and see results immediately. This is useful for learning language features or quickly testing code snippets.
To start REPL, simply run kotlinc without any parameters:
kotlinc
This enters REPL mode. You can enter any valid Kotlin code and press Enter to see the result:
val x = 10 val y = 20 x + y 30
Enter :quit or press Ctrl+C to exit REPL.
Kotlin Scripts
Kotlin can also be used as a scripting language. Kotlin script files have the .kts extension, and top-level code in the file is treated as executable code.
For example, create a script named list_folders.kts:
import java.io.File
val path = if (args.contains("-d")) args[1 + args.indexOf("-d")] else "."
val folders = File(path).listFiles { file -> file.isDirectory() } folders?.forEach { folder -> println(folder) }
Run the script:
kotlinc -script list_folders.kts -- -d /path/to/folder
Configuring Build Tools
In real projects, Gradle or Maven are typically used as build tools. Both natively support Kotlin projects.
Gradle
Gradle is the most popular build tool for Kotlin development. The Kotlin team also provides Kotlin DSL, allowing build scripts to be written in Kotlin syntax.
To create a basic Kotlin Gradle project, add the Kotlin plugin to your project. Your build.gradle file needs these contents:
plugins { id 'org.jetbrains.kotlin.jvm' version '1.9.0' }
repositories { mavenCentral() }
dependencies { implementation kotlin-stdlib }
Or using Kotlin DSL:
plugins { kotlin("jvm") version "1.9.0" }
repositories { mavenCentral() }
dependencies { implementation(kotlin("stdlib")) }
Then create the source directory structure, typically src/main/kotlin. Add Kotlin source files and use gradle build to compile the project.
Maven
Maven is another popular build tool for Java projects that also supports Kotlin projects. Add Kotlin dependencies to your pom.xml file.
Verifying Installation
After installation, you can verify everything is working correctly.
First, verify the Kotlin compiler:
kotlinc -version
You should see output like "Kotlin compiler version 2.3.10".
Then verify the Kotlin standard library:
kotlin -e 'println("Hello Kotlin!")'
If everything works, the installation is successful and you can begin your Kotlin journey.
Next Steps
After installing Kotlin, it is recommended to continue learning by: first, reading the official Kotlin Tour to understand basic language features; then, choosing a development direction that interests you such as Android development, server-side development, or cross-platform development; finally, practicing with real projects to apply what you've learned.
Kotlin provides abundant learning resources including official documentation, online tutorials, and video courses. You can find links to these resources on the Kotlin website.
