Kotlin CLI¶
The Kotlin CLI is a command-line tool to build, run, test, package, and publish your projects without an IDE. It is useful both locally and in CI/CD pipelines.
Installation¶
curl -fsSL https://kotl.in/install.sh | sh
The installer script downloads the Kotlin CLI, places it in ~/.local/bin, and ensures it's on your PATH.
No sudo required.
You can review the installer script before running it.
powershell -ExecutionPolicy ByPass -c "irm 'https://kotl.in/install.ps1' | iex"
The installer script downloads the Kotlin CLI, places it in ~/.local/bin, and ensures it's on your PATH.
No sudo required.
You can review the installer script before running it.
Note
The first time you run the Kotlin CLI, it will take some time to download the Kotlin Toolchain distribution. Subsequent runs will be faster, as the downloaded files will be cached locally.
Alternative: use SDKMAN¶
If you already have SDKMAN installed, you can use it to install the CLI:
sdk selfupdate
sdk update
sdk install kotlintoolchain
Exploring Kotlin CLI commands¶
The root kotlin command and all subcommands support the -h (or --help) option to explore what is possible:
kotlin --help # shows the available commands and general options
kotlin build --help # shows the options for the 'build' command specifically
Here are the most commonly used commands:
kotlin newto create a new Kotlin projectkotlin buildto compile and link all code in the projectkotlin runto run your applicationkotlin testto run tests in the projectkotlin show (modules|settings|dependencies|checks|commands)to introspect the project's configurationkotlin cleanto remove the project's build output and cacheskotlin publishto publish modules to a repository
Try it out!
Create a new project using the kotlin new command and select the JVM console application template.
Then build and run the application using kotlin run.
Tab-completion¶
If you’re using bash, zsh, or fish, you can generate a completion script to source as part of your shell’s
configuration, to get tab completion for Kotlin CLI commands.
First, generate the completion script using the generate-completion command, specifying the shell you use:
kotlin generate-completion bash > ~/kotlin-completion.sh
kotlin generate-completion zsh > ~/kotlin-completion.sh
kotlin generate-completion fish > ~/kotlin-completion.sh
Then load the script in your shell (this can be added to .bashrc, .zshrc, or similar configuration files to load it
automatically):
source ~/kotlin-completion.sh
You should now have tab completion available for Kotlin CLI subcommands, options, and option values.
Updating the Kotlin Toolchain to a newer version¶
Run kotlin update to update the Kotlin CLI scripts and the toolchain distribution to the latest released version.
Use the --dev option if you want to try the bleeding edge dev build of the Kotlin Toolchain (no guarantees are made on these builds).
See kotlin update -h for more information about the available options.
Don't forget to regenerate your tab-completion script, if you have one.