Scala Setup & Toolchain

Scala needs a Java Development Kit underneath it, since it targets the JVM. On top of that you have a choice of tools: sbt, the traditional build tool for multi-file projects, and scala-cli, a fast, modern tool for single-file scripts and small projects. Both are installed by Coursier, Scala's package and tool manager.

Installation

Install a JDK first (17 or newer), then let Coursier's cs setup install the Scala compiler, sbt, scala-cli, and the REPL together in one step.

# macOS (Homebrew)
brew install coursier/formulas/coursier
cs setup

# Windows (winget)
winget install Coursier.cs
cs setup

# Linux
curl -fL https://github.com/coursier/coursier/releases/latest/download/cs-x86_64-pc-linux.gz | gzip -d > cs
chmod +x cs
./cs setup

# verify everything landed
scala -version        # the Scala runner / compiler front-end
sbt --version          # the traditional build tool
scala-cli --version    # the fast single-file tool

The REPL

The Scala REPL (Read-Eval-Print Loop) is the fastest way to try an idea. Launch it with scala and every line you type is evaluated immediately, with its type and value printed back.

$ scala
scala> val x = 21 * 2
val x: Int = 42

scala> def greet(name: String) = s"Hello, $name!"
def greet(name: String): String

scala> greet("world")
val res0: String = Hello, world!

scala> :quit

First Program with scala-cli

For a single file, scala-cli needs no project scaffolding at all — write the file, run it, done. This is the quickest way to work through the demos in this roadmap.

// hello.scala
@main def main(): Unit =
  println("Hello, world!")
scala-cli run hello.scala
# Compiling project (Scala 3.x, JVM)
# Compiled project (Scala 3.x, JVM)
# Hello, world!

First Project with sbt

Real projects — multiple files, dependencies, tests — use sbt. Scaffold one with sbt new, or create the minimal layout by hand:

hello/
├── build.sbt           # project manifest + dependencies
├── project/
│   └── build.properties # pins the sbt version
└── src/
    └── main/
        └── scala/
            └── Main.scala
// build.sbt
ThisBuild / scalaVersion := "3.4.2"

lazy val root = (project in file("."))
  .settings(
    name := "hello"
  )
cd hello
sbt run
# [info] compiling 1 Scala source
# [info] running Main
# Hello, world!

Everyday Commands

CommandWhat it does
sbt compileCompile without running.
sbt runCompile, then execute the @main entry point.
sbt testCompile and run the test suite — see Testing.
sbt consoleOpen a REPL with the project's classpath already loaded.
sbt ~compileWatch mode: recompile automatically on every save.
scala-cli run file.scalaCompile and run a single file, no project needed.
Practice move: download any file from Demo Examples and run it with scala-cli run <file>.scala — every demo in this roadmap is a single, self-contained file.