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. That plugin is under active development, and it still has a few rough edges.

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

You need SBT to make Scala projects that you can use with Visual Studio Code. Install Java and SBT as in the preceding section.

Install the “Metals” plugin into Visual Studio Code.

Make an SBT project and open it in VS Code, or open one of the chapter directories from the book code.

The first time you open an SBT project, you will be asked to import the build. Agree.

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 “swiss army knife” for Scala projects, comparable to Maven or Gradle. Like Coursier, it is a command line tool.

Java 11 or Java 8 is required. If you don't already have it, install it, for example from https://adoptopenjdk.net/. After the installation, run

javac -version

to see that you successfully installed the correct Java version. If you don't see the correct version, be sure that your PATH environment variable is properly set and try again.

Next, install SBT: https://www.scala-sbt.org/download.html

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.