80 pages of hands-on tutorial right into your email inbox. Learn how to use state of the art development environment and build a full-fledged command-line application. You will receive free updates to the tutorial. Start learning Kotlin today!
This guide is an add-on to the Ultimate Tutorial: Getting Started With Kotlin (get it here). It explains how to install all the dependencies Kotlin requires.
Looking for Linux? Here is the link to the guide.
Looking for Windows? Here is the link to the guide.
This tutorial assumes you can use a terminal application to execute commands to install the dependencies. Skip this section if you already have terminal installed and comfortable using it.
Mac OS X has the default Terminal App already pre-installed. You can also use your favorite terminal application.
Frankly, I prefer and recommend you use iTerm2. Maybe I am biased, but I have seen many developers prefer it too.
If you do not have iTerm2 installed, you should be able to download it from the official iTerm2 website. To check that iTerm2 or other terminal application of your choice works, run the following command:
$ echo "Hello, World."
$ is not part of the command – it is just a prompt that terminal application usually outputs. No need to type it. Same applies for any terminal command I ask you to run in this tutorial. If that worked, you should see Hello, World output.
This tutorial assumes you have homebrew installed. Skip this section if you already have homebrew installed.
To check if homebrew is installed run the following command, and it should produce the following output:
$ brew --version
Homebrew/homebrew-core (git revision 44a0; last commit 2017-09-19)
Your version, git revision and last commit is going to vary.
However, if you see something like that:
$ brew --version
zsh: command not found: brew
Then that means you do not have homebrew installed. Follow instructions at the official homebrew website to install it.
This tutorial assumes you have JDK installed. Skip this section if you already have JDK installed. Though, you might want to update the version of you JDK if it is too old. Then continue reading.
To check if JDK of proper version is installed run the following command and it should produce the following output:
$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
You java version and other numbers may vary.
If you do not have JDK installed, this command should trigger a dialog window with instructions how to install JDK by downloading it from some website. Ignore this and just press OK. Since you have homebrew installed, you should run in your terminal:
$ brew update
$ brew tap caskroom/versions
$ brew cask install java8
This command should ask you for your account’s password, that is correct. When it prompts for the password:
Enter your password:
Then you should enter your password. Don’t worry – it will not appear on the screen when you will be typing. Finally, when the command execution is complete, you should run java -version and check that its output is similar to what we had a few paragraphs before.
You might need to update your JDK version if it is too old. For that, you should execute the following two commands: brew update and brew cask reinstall java8
Both Kotlin programming language and IntelliJ IDEA are developed by the same company – JetBrains. As you might guess IntelliJ has some serious support for that language. Development environment and the programming language are both optimised to work well with each other.
Of course, you can use a plain-text editor and command-line Kotlin compiler. I highly recommend you use proper tools for the job. Don’t worry IntelliJ IDEA Community Edition is free to use and open-source. So you should have no trouble downloading, installing and using it indefinitely for anything you want, including commercial applications.
This tutorial assumes you are using IntelliJ IDEA Community Edition. If you have it installed and configured, you should skip this section. Otherwise, here we go:
First, download the community edition from the official JetBrains website.
Then, open the downloaded .dmg file and in the opened window drag the IntelliJ IDEA icon to Applications folder. It will take a few seconds to move it to the Applications folder (you will see the progress bar):
Next, start it from your Applications folder. There should be a popup dialog asking if you want to launch applications from a registered developer (JetBrains), answer OK.
IntelliJ will ask you if you want to import any settings from any previous installation, answer Do not import settings. IntelliJ will ask you what theme you want to use. This tutorial will use the Default, light theme.
Next, IntelliJ will ask you which keymap you want to use – choose I’ve never used IDEA.
Next, IntelliJ will ask you if you want to create a launcher script for opening files and projects from the command line – you do not need that right now. Just click Next button. IntelliJ will ask you what default plugins you want to use. Leave everything as is and proceed to the next step.
Finally, IntelliJ will ask you one last thing: what featured plugins you want to use. Leave everything as is and proceed to the next step.
At this point, you should see IntelliJ loading. As it is loaded, you should see the front screen, which allows you to create a project, import or open the existing one:
That should be it for dependencies. I highly recommend creating a simple project in Kotlin to see that everything works as expected. Let’s do that right now:
Click on the Create New Project button on the IntelliJ front screen:
After clicking on the button, you should see the Create New Project screen:
At this point choose Kotlin project group in the sidebar:
don’t choose Kotlin/JVM in the main area, as this will create a Java project with Kotlin support.
Now choose Kotlin/JVM in the main view and press Next button:
You should see the screen with all the different options to create your new project:
As you can see the Project Location is ~/IdeaProjects/untitled. Particularly the part ~/IdeaProjects. That is supposed to be a home directory for all the projects created in IntelliJ IDEA. I discourage you from using that. In our industry, most of the developers, whom I have met, prefer to use ~/workspace as the home directory for any of their projects, no matter if they are IntelliJ-based or not.
So change IdeaProjects to workspace and change your Project name from untitled to HelloKotlin. Also, make sure the Project Location says ~/workspace/HelloKotlin too. You do not need to change anything else on this screen at this point.
You should see now:
As we are done filling the details of our new project, let’s click on that Finish button. IntelliJ will ask you if it should create a new directory for your project. Click on the OK button.
Now you should see the workspace of your project:
As this is your first run of Kotlin project in IntelliJ, it will take a minute or so to index all the Kotlin and Java standard libraries.
As soon as it has completed indexing, you should see the sidebar with your project structure:
Now we should create our small hello world application. It will consist of a single file with the main function. The main function is the entry point to any application in Kotlin. When you run the application main function gets executed.
To create a new file, expand HelloKotlin module in the project sidebar. If you do not see the sidebar like on the picture, press ⌘1 (Command + 1). Then select src directory inside of it. src directory is where the code resides in the default project structure created by IntelliJ. Here is what you should see at that point:
Now to create a new file press ⌘N (Command + N) while src directory is selected; alternatively, you could right-click on src directory to bring up the context menu, and the first submenu is New.
Anyways, you should see now this context menu (or submenu):
That menu allows you to type to filter the items. Start typing kotlin to try it out:
Now press Enter to start the creation of new Kotlin File/Class. Alternatively, instead of using the filter, you could have just clicked with your mouse on Kotlin File/Class or use arrows to select that option.
After initiating the creation of new kotlin file or class, you will see an appropriate dialog, which allows you to choose the kind of kotlin file you want to create and its name:
Now type the name of the kotlin file: Application. Leave kind as it is - File. Finally, press Enter or click on the OK button:
After creating the file, it should automatically open in the main workspace area in the IntelliJ:
Now, the easiest way to create a main function is to type main and press ⇥ (TAB):
After you press ⇥ (TAB), the main function will be auto-generated by IntelliJ. This feature of IntelliJ is called live templates. It allows to type much less, comes with helpful built-in templates, and allows to configure your templates. That is what you should see when the main function is generated:
Now, to print “Hello, Kotlin!” on the screen add the following to your main function:
To run the application press ⌃⇧R while code editor with Application.kt open is active. The application should compile and run. Shortly, you should see the desired output “Hello, Kotlin!”:
Now you should be set to use Koltin with IntelliJ on Mac OS X.
Check out my free Ultimate Tutorial: Getting Started With Kotlin where you will build a real command-line application while learning all the features of Kotlin you will need to construct 80% of any application.