Вы находитесь на странице: 1из 3

HomeBrew or MacPort

If you are on macOS and already using either brew or port, see either Listing 1-5 or 1-6
for the terminal commands to get Kotlin.
Listing 1-5. Install Kotlin Using HomeBrew
$ brew update
$ brew install kotlin
Chapter 1 GettinG into Kotlin
11
Listing 1-6. Install Kotlin Using MacPorts
$ sudo port install kotlin
Using a Zipped Installer
If you go to the Kotlin website, http://kotlinglang.org then “learn” ➤ “tutorials” ➤
“getting started” ➤ “working with the command line compiler”, you’ll find a web page 2
that might look like the one shown in Figure 1-3. The zipped installer can be downloaded
by following the link “GitHub releases” (also shown in Figure 1-3).
The link should take you to the GitHub page of JetBrains/Kotlin 3 (Figure 1-4). At the
time of writing, Kotlin was on version 1.2.10; it might be a different version by the time
you are reading this, but just download the latest stable version.
2Working with the command line compiler: https://kotlinlang.org/docs/tutorials/
command-line.html
3JetBrains/Kotlin GitHub page: https://github.com/JetBrains/kotlin/releases/tag/v1.2.10

Figure 1-3. Kotlin command line compiler page


Chapter 1 GettinG into Kotlin
12
When the download finishes, unzip the installer file and put it somewhere in your
system—preferably, a directory where you have read, write, and execute privileges. The
file should unzip to a folder named “kotlinc”. Next thing to do is to add the kotlinc/bin
folder to the system path variable. The following sections will demonstrate how to do
this on macOS, Linux, and Windows.
macOS and Linux
Copy the downloaded zipped file to your home directory and unzip it there. Listing 1-7
shows the command.
Listing 1-7. Unzip Kotlin Installer
$ cd ~
$ unzip ~/kotlin-compiler-1.2.10.zip
Note the unzip command is available in macoS by default, but for linux
systems, you might have to get it from the repositories first. listing 1-8 shows the
command on how to pull it from the repositories.
Figure 1-4. GitHub page for the installer zipped file
Chapter 1 GettinG into Kotlin
13
Listing 1-8. Getting the Unzip Tool
$ sudo apt get update
$ sudo apt-get install unzip
The installer file should unzip to a folder named “kotlinc”, as shown in Figure 1-5.
Figure 1-5. Unzipping the Kotlin installer
Before we can use the command line tools, we need to add the “kotlinc/bin” folder
to the system path variable as shown in Listing 1-9.
Listing 1-9. Adding kotlinc/bin to the System Path
$ export PATH=~/kotlinc/bin:$PATH
Press ENTER and the kotlinc command should now work. You can add the line
shown in Listing 1-9 to your login script so that the Kotlin tools are available every time
you open a terminal window.
Chapter 1 GettinG into Kotlin
14
Windows 10
Copy the Kotlin installer zipped file to your home directory and unzip it there. Use your
favorite archive tool for unzipping. It should unzip to the following folder: C:\Users\
yourname\kotlinc. Inside the kotlinc folder is the bin folder, which contains the various
script and batch files that we need to use for compilation. This bin folder is what we need
to add the Windows system path.
To add the kotlinc\bin folder to the system path, click the Windows Start button
➤ Control Panel ➤ System. Once the System dialog opens, click Advanced ➤
Environment Variables. There are two boxes for variables; the upper box reads “User
variables” and the lower box reads “System variables”. The system PATH will be in the
“System variables” box. Append kotlinc\bin the PATH variable. Close the system dialog
box to save your changes.
Using SDKMAN
SDKMAN can be used on macOS, Linux, Cygwin (Windows), FreeBSD, and other UNIX
systems. If you have this already as part of your toolchain, you can use it to get the Kotlin
compiler. If you don’t have SDKMAN yet, it is simple to install. See Listing 1-10 to install
SDKMAN.
Important Before you can install SDKMan from the command line, you will need
to get the curl tool. if you don’t have it yet, use your platforms package manager
to get curl.
Listing 1-10. Installing SDKMAN From the Command Line
$ curl -s "https://get.sdkman.io" | bash
Follow the on-screen instructions to complete the installation. You will need to close
the current terminal window and launch another one because the SDKMAN installer
made changes to the login script. In order for those changes to take effect, you will need to
open a new terminal window. When that’s done, we can now install kotlin. See Listing 1-11
for the installation command.
Chapter 1 GettinG into Kotlin
15
Listing 1-11. Installing Kotlin via SDKMAN
$ sdk install kotlin

Coding With the Command Line Tools


Whichever way you chose to install the command line tools, by now you should already
have a working Kotlin compiler. To try it out, get a terminal window and enter the
command kotlinc. This will change your terminal prompt to a triple chevron (greater
than sign); see Listing 1-12.
Listing 1-12. Kotlin REPL
$ kotlinc
Welcome to Kotlin version 1.2.10 (JRE 9.0.1+11)
Type :help for help, :quit for quit
>>>
This is the Kotlin REPL—short for Read, Eval, Print, Loop. It executes Kotlin
commands interactively and shows you the results immediately. If you have used the
console feature of modern browsers to enter JavaScript commands before, this is very
similar to that. The REPL is a good way to learn the language interactively. It’s also very
useful during development because it allows you to try out expressions and statements
without having to go through the full write-compile-run cycle. You might want to try out
a couple of expressions and statements (see Listing 1-13).
Listing 1-13. Simple Expressions
>>> 5 * 3
15
>>> println("Hello there")
Hello there
for (i in 1 . . 3) {
. . .println(i)
. . .}
123
>>>

Вам также может понравиться