# Kotlin command-line compiler Every Kotlin release ships with a standalone version of the compiler. You can download the latest version manually or via a package manager. Note: Installing the command-line compiler is not an essential step to use Kotlin. The common approach is to write Kotlin applications using IDEs or code editors with official Kotlin support, such as [IntelliJ IDEA](https://www.jetbrains.com/idea/) or [Android Studio](https://developer.android.com/studio). They provide full Kotlin support right out of the box. Learn how to [get started with Kotlin in an IDE](getting-started.html). ## Install the compiler ### Manual install To install the Kotlin compiler manually: 1. Download the latest version (`kotlin-compiler-2.4.21.zip`) from [GitHub Releases](https://github.com/JetBrains/kotlin/releases/tag/v2.4.21). 2. Unzip the standalone compiler into a directory and optionally add the `kotlinc/bin` directory to the system path. The `bin` directory contains the scripts needed to compile and run Kotlin on Windows, macOS, and Linux. Note: If you want to use the Kotlin command-line compiler on Windows, we recommend installing it manually. ### SDKMAN! An easier way to install Kotlin on UNIX-based systems, such as macOS, Linux, Cygwin, FreeBSD, and Solaris, is [SDKMAN!](https://sdkman.io). It also works in Bash and ZSH shells. [Learn how to install SDKMAN!](https://sdkman.io/install). To install the Kotlin compiler via SDKMAN!, run the following command in the terminal: ```BASH sdk install kotlin ``` ### Homebrew Alternatively, on macOS you can install the compiler via [Homebrew](https://brew.sh/): ```BASH brew update brew install kotlin ``` ### Snap package If you use [Snap](https://snapcraft.io/) on Ubuntu 16.04 or later, you can install the compiler from the command line: ```BASH sudo snap install --classic kotlin ``` ## Create and run an application 1. Create a simple console JVM application in Kotlin that displays `"Hello, World!"`. In a code editor, create a new file called `hello.kt` with the following code: ```KOTLIN fun main() { println("Hello, World!") } ``` 2. Compile the application using the Kotlin compiler: ```BASH kotlinc hello.kt -include-runtime -d hello.jar ``` * The `-d` option indicates the output path for generated class files, which may be either a directory or a .jar file. * The `-include-runtime` option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it. To see all available options, run: ```BASH kotlinc -help ``` 3. Run the application: ```BASH java -jar hello.jar ``` Note: To compile a Kotlin/Native application, use the [Kotlin/Native compiler](native-get-started.html#using-the-command-line-compiler). ## Compile a library If you're developing a library to be used by other Kotlin applications, you can build the .jar file without including the Kotlin runtime: ```BASH kotlinc hello.kt -d hello.jar ``` Since binaries compiled this way depend on the Kotlin runtime, you should ensure that it is present in the classpath whenever your compiled library is used. You can also use the `kotlinr` script to run binaries produced by the Kotlin compiler: ```BASH kotlinr -classpath hello.jar HelloKt ``` `HelloKt` is the main class name that the Kotlin compiler generates for the file named `hello.kt`. Note: To compile a Kotlin/Native library, use the [Kotlin/Native compiler](native-libraries.html#using-kotlin-native-compiler). ## Run the REPL Run the compiler with the [-Xrepl compiler option](compiler-reference.html#xrepl) to have an interactive shell. In this shell, you can type any valid Kotlin code and see the results. ## Run scripts You can use Kotlin as a scripting language. A Kotlin script is a Kotlin source file (`.kts`) with top-level executable code. ```KOTLIN import java.io.File // Get the passed in path, i.e. "-d some/path" or use the current path. 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) } ``` To run a script, pass the `-script` option to the compiler with the corresponding script file: ```BASH kotlinc -script list_folders.kts -- -d ``` Kotlin provides experimental support for script customization, such as adding external properties, providing static or dynamic dependencies, and so on. Customizations are defined by so-called script definitions – annotated kotlin classes with the appropriate support code. The script filename extension is used to select the appropriate definition. Learn more about [Kotlin custom scripting](custom-script-deps-tutorial.html). Properly prepared script definitions are detected and applied automatically when the appropriate jars are included in the compilation classpath. Alternatively, you can specify definitions manually by passing the `-script-templates` option to the compiler: ```BASH kotlinc -script-templates org.example.CustomScriptDefinition -script custom.script1.kts ``` For additional details, see the [KEEP-75](https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-support.md). ## What's next? [Create a console application based on Kotlin/JVM](jvm-get-started.html).