Before we begin

If you want to simply read a book in one go without trying to roll out an app of your own, you don’t need any preparations. However, even if this is the case, I would still recommend flicking through this section because it provides a good overview of what tools you can use to write Scala code and how to set them up.

Setting up your environment

I assume that you are already familiar with your operating system and its basic commands. I personally prefer using Ubuntu for Web development, and if you want to give it a try, you may consider installing VirtualBox and then installing Ubuntu on top of it. If you want to follow this path, here are my suggestions:

  • Download VirtualBox from https://www.virtualbox.org/ and install it
  • Create a new virtual machine, give it about 2GB of RAM and attach 16GB of HDD (dynamically allocated storage)
  • Download a lightweight Ubuntu distribution such as Xubuntu or Lubuntu. I wouldn’t bother with 64bit images, plain i386 ones will work just fine. As for versions, 16.04 definitely works in latest versions of VirtualBox without problems, earlier versions probably will too.
  • After installing Ubuntu, install Guest Additions so that you will be able to use higher resolutions on a virtual machine

Installing Java Development Kit

Scala compiles into Java bytecode, so you will need to install the Java Development Kit (JDK) to run Scala programs. Fortunately, there are many ways to obtain JDK, and all of them are relatively straightforward.

If you’re running a Linux distribution, then there’s a good chance that it has a prebuilt OpenJDK package in its repository. For example, on Ubuntu you can get the latest update for OpenJDK 8 using the following command:

$ sudo apt-get install openjdk-8-jdk

You can also download a prebuilt OpenJDK package from AdoptOpenJDK and then install it manually:

  • Go to https://adoptopenjdk.net/releases.html and download the latest JDK 8 (look for “OpenJDK 8 with HotSpot” and make sure that the full name looks something like “jdk8u172-b11”, which means “version 8, update 172”). For Linux you’ll probably end up getting a tar.gz file. Extract the contents of the archive anywhere (for example, to ~/DevTools/java8);
  • On Windows, just follow the instructions of the installer;
  • Create an environment variable JAVA_HOME and point it to the JDK directory;
  • Add the bin directory of JDK to your PATH so that the java command works from any directory on your machine;

On Linux, you can accomplish both goals if you add to your ~/.bashrc the following:

JAVA_HOME=/home/user/DevTools/jdk8u172-b11
PATH=$JAVA_HOME/bin:$PATH

After that, you can start a new terminal window and check that Java is indeed installed:

$ java -version
openjdk version "1.8.0-adoptopenjdk"
OpenJDK Runtime Environment (build 1.8.0-adoptopenjdk-2018_05_19_01_00-b00)
OpenJDK 64-Bit Server VM (build 25.71-b00, mixed mode)

Installing Scala

You don’t need the Scala distribution in order to write Play applications, but you may want to install it to try some ideas in the interpreter. Just follow these steps:

  • Get Scala binaries from http://www.scala-lang.org/download/
  • Extract the contents of the archive anywhere (for example, to ~/DevTools/scala)
  • Add the bin directory of the Scala distribution to your path:
JAVA_HOME=/home/user/DevTools/jdk8u172-b11
SCALA_HOME=/home/user/DevTools/scala-2.13.4
PATH=$JAVA_HOME/bin:$SCALA_HOME/bin:$PATH
  • Start the interpreter in the interactive mode by typing scala

Using REPL

When you type scala without arguments in the command line, Scala will start the interpreter in the interactive mode and greet you with a REPL prompt. REPL stands for Read-Eval-Print loop, and it is a great way to learn new features or try out some ideas. You can start by typing

scala> println("Hello Scala")

and then you can press Enter and observe the output.

Hello Scala

Since the println function prints a message in stdout, and the REPL session is hooked to stdout, you see the message in the console. Another option is to simply type

scala> "Hello World"

The REPL will respond by printing

res1: String = Hello World

in the console. The REPL evaluates an expression, determines its type and, if you haven’t assigned it to anything, assigns it to an automatically-generated value.

Feel free to come back to REPL later, when you learn more about the language, but for now here is the list of commands that you may find particularly useful:

command description
:quit exits the REPL
:reset resets the REPL to its initial state
:cp adds a specified library to the classpath
:paste enters the paste mode, which makes it possible to define multiple things at once
:history shows the list of previously typed expressions
:help shows the list of available commands

Using IntelliJ IDEA

IntelliJ IDEA accompanied with the official Scala plugin is the best way to write Scala code today. And the good news is that the Community edition, which is free and open-source, will work just fine.

When installing IntelliJ there are several not-so-obvious things worth mentioning.

First, Windows versions come with an embedded JRE, while Linux versions use an already installed JDK, so if you have Java 8 installed, you may see messages like this when IDEA starts:

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=350m; support\
 was removed in 8.0

You can safely ignore these warnings for now. By the way, you can use the idea.vmoptions file that resides in the bin directory to tweak, well, VM options. Two properties which are often tweaked are:

-Xms128m
-Xmx2048m

Here -Xms specifies the size of the initial memory pool, and -Xmx specifies the maximum. And if you want to learn more about them, start with this question on StackOverflow.

Sometimes IntelliJ goes crazy and starts highlighting valid parts of your code as errors. When it happens, there are two ways to fix the IDE. The simplest is to deactivate “Scala type-aware highlighting” and then activate it again. This helps in many cases.

Scala type-aware highlighting toggle
Scala type-aware highlighting toggle

A more radical solution is to click “File -> Invalidate Caches / Restart…”. IDEA will have to reindex everything after restart, but sometimes it could be the only way to regain its senses.

Using Atom Editor

Even though I strongly believe that using a full-blown IDE when working with Scala significantly increases your productivity, it’s also worth mentioning a slightly more lightweight alternative - the Atom Editor. Atom is created by GitHub, based on such technologies as V8 and React, and at the moment, it’s becoming more and more popular.

The official site is atom.io, and if you’re using Ubuntu, can simply grab the deb package.

There are many packages written for Atom, and for trying examples from this book you may want to install the following:

plugin description
language-scala adds syntax highlighting for Scala
linter-scalac compiles your code behind the scenes using scalac to show compiler errors right inside the editor
terminal-plus allows to use the terminal session without leaving the editor

Just go to “Packages -> Settings View -> Install Packages/Themes”.

Using Visual Studio Code

Visual Studio Code is a V8-based text editor from Microsoft. Just like Atom, it offers great many features for developers, particularly when it comes to the Web. Unlike the Visual Studio IDE, Code is available on most platforms, including Linux. In fact, after installing it from the official deb package, it will add its own official update repositry to keep up-to-date.

There are many useful extensions available, but related specifically to our project are the following:

extension description
scala-lang.scala adds syntax highlighting for Scala
dbaeumer.vscode-eslint checks correctness of EcmaScript on the fly and highlights dubious code
editorconfig.editorconfig adds support for EditorConfig that helps to configure different editors in the same way

Why use text editors?

Both Atom and Visual Studio Code are based on some relatively heavy technologies, so they are not as lightweight as, say, Notepad++ or emacs. Moreover, they don’t help you much when it comes to adding package imports, and in this respect, they are inferior to tools like IntelliJ IDEA, so why use them at all?

Well, there are several situations when you may actually choose Atom or Code over IntelliJ:

  • When you browse the contents of someone else’s project, often you want to get the overview of its directory structure and possibly quickly edit some files. In this case, you could simply type “atom .” or “code .” and start hacking in a couple of seconds. IntelliJ usually takes some time to load, and then it would require you to import the project, which may take a couple of minutes even on decent hardware.
  • Everything related to frontend is usually top quality in both V8-editors. At the same time, full support of JavaScript and CSS is considered a paid feature in IntelliJ. The Community version will paint HTML markup in different colors and even highlight JS files, but if you want support for JSX templates or CSS preprocessors, you should start looking at the Ultimate version or switch to Atom or Code.
  • Lastly, I found that when you’re only starting with something new, learning the basics without using sophisticated tools provides a better understanding of technology and therefore, gives you more confidence later.

I suggest you use text editors for the first parts of the book and also the frontend but switch to IntelliJ for while doing more serious work with Play. Please refer to Appendix B for additional instructions on importing Scala projects in IntelliJ.