Scala 3 Installation Instructions

There is no one best way to install Scala. The optimal development environment depends on your preferences and needs. Here are four choices. If you hate choices and just want to get started, use IntelliJ.

IntelliJ

IntelliJ is a traditional, comfortable, batteries-included development environment. The downside, as with all such environments, is that the app is resource-hungry and the user interface is rather complex.

Download and install the free Community Edition of IntelliJ from https://www.jetbrains.com/idea/download

Install the Scala plugin.

Open an existing SBT project (such as one of the book code chapters) or start a new Scala project, choosing SBT and the latest Scala 3 version.

To get a Scala REPL, select Tools → Scala REPL from the menu.

To run a worksheet, right-click on it and select Evaluate Worksheet

.png

To run a @main annotated function, open the file containing it, click on the green triangle next to it, and select Run or Debug.

.png

Visual Studio Code

Visual Studio Code is a popular development environment that relies on plugins to work with many languages and frameworks. The Scala support is provided by the Metals plugin.

Install Visual Studio Code: https://code.visualstudio.com/Download. If you don't like the Microsoft license, use VSCodium instead.

Install the Metals plugin into Visual Studio Code.

Open a folder containing Scala programs in the src/main/scala subfolder. Metals should start. The first time can be quite slow.

When you open a worksheet (with extension .worksheet.sc), you should automatically see the outputs.

.png

When you open a Scala file containing a @main annotated function, you will see links run | debug above it. Click to run or debug the program.

.png

Caution: The Run → Start Debugging, Run → Run Without Debugging menu commands do not work for Scala program.

Caution: Currently, you cannot run or debug programs that use console input.

Caution: When you run or debug a program in the book code, you may need to set the selector in the bottom panel to the program name in order to see the output.

Coursier

Coursier manages Scala command line tools. You use your own preferred editor. The Scala Center recommends this approach to get started quickly. You don't need to know how to install Java or update your PATH environment variable. However, you need to be comfortable with the command line.

Install Coursier. The installer may offer to install Java, if necessary, and to update your PATH. Agree to that.

Then close the terminal window and open a new one. Run

scala

You should see a Scala 3 version number. You should be able to type in Scala commands following section 1.1 of the book.

You can load a worksheet—a file with Scala expressions. For example, unzip the book code and run scala3 from the scala-impatient-3e-code folder:

:load ch1/src/main/scala/section2.worksheet.sc

If you load a program with a @main annotated function, the function definition is loaded but not executed. You still need to invoke the function.

:load ch2/src/main/scala/Section4.scala
hello()

To compile a program, use the scalac command:

scalac ch2/src/main/scala/Section4.scala

The result is a class whose name equals that of the @main function, not the name of the Scala file. In this example, the function is called demo4. You run it like this:

scala demo4

SBT

SBT is a build tool for Scala projects, comparable to Maven or Gradle. Like Coursier, it is a command line tool.

When you install Coursier, it automatically installs SBT as well.

The S in SBT stands for “simple”, but actually, SBT is pretty complex. (For a simpler Scala build tool, check out Mill.) Nevertheless, it can be helpful to use SBT because IntelliJ and VS Code recognize the structure of SBT projects.

Make a Scala 3 project:

sbt new scala/scala3.g8 --name=myproject
cd myproject

To get a Scala REPL, run

sbt console

inside a directory with an SBT project.

The ch1, ch2, ... directories inside the book code distribution are SBT projects. You can change into any of them and run SBT instructions. The first time, a large amount of library code will be downloaded.

In the console, you can load a worksheet. For example, in the ch1 bookcode directory:

:load section2.worksheet.sc

Scala programs are expected to be in the src/main/scala subdirectory. The sbt new action provides a sample Main.scala that you can modify or replace.

To compile and run a program, run

sbt runMain

If there is more than one @main annotated function, you will be asked to choose one. Or you can specify it. For example, from the ch2 book code directory:

sbt "runMain demo4"

The quotes are necessary.

Each time when launched, sbt scans your project. To save time, you can enter sbt once and then issue commands at the SBT prompt:

sbt
compile
runMain demo4
console

Note that now there are no quotes around runMain demo4.