Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 37 ยท Getting Started

Install Scala and Run Code

Install Scala 3 with Coursier, scala-cli and sbt, run your first file, use the REPL, and pick an editor such as IntelliJ IDEA or VS Code with Metals.

Before you can run Scala on your own machine you need a JDK (Java Development Kit) and the Scala tooling. The good news is that one installer, Coursier, sets up everything. This lesson walks through the setup, the three ways to run code (scala-cli, the REPL and sbt) and the editors worth using.

Remember that the Scala blocks on this site are read-only, so a local install is how you will run them. If you only want to follow along, you can skip the setup and come back later.

What you need

flowchart LR
  A["JDK 17 or newer"] --> B["Scala compiler (scalac)"]
  B --> C["scala-cli or sbt"]
  C --> D["Your program"]

The JDK runs everything; the compiler turns .scala files into JVM bytecode; a tool such as scala-cli or sbt drives the compiler.

  • A JDK. Scala 3 needs Java 8 or newer, but install a current LTS release (17 or 21) from any vendor such as Temurin.
  • Coursier (cs). A launcher that installs Scala tools and keeps them up to date.
  • scala-cli. A small, fast command that compiles and runs single files and small projects.
  • sbt. The standard build tool for bigger projects. Later lessons cover it.

Install with Coursier

Download the Coursier installer for your operating system from the official Scala website, then run its setup command. It installs the compiler, scala-cli, sbt and a JDK if you do not have one. After it finishes, open a new terminal and check:

Terminal
scala-cli version
sbt --version

If both print a version, you are ready. If the shell says the command is not found, the Coursier bin directory is not on your PATH yet; restarting the terminal usually fixes it.

Run a file with scala-cli

Create a file hello.scala:

SCALA
@main def hello(): Unit =
  println("Hello from Scala 3")

Run it:

Terminal
scala-cli run hello.scala

The first run downloads the compiler and takes a while. Later runs are fast. The output is:

TEXT
Hello from Scala 3

You can also put a directive at the top of the file to select the Scala version or to pull in a library:

SCALA
//> using scala 3.3
//> using dep com.lihaoyi::os-lib:0.9.1

@main def listFiles(): Unit =
  println(os.list(os.pwd).size)

//> using lines are read by scala-cli itself, which is why a single file can be a complete project. Version numbers change often, so look up the current one when you add a dependency.

The REPL

REPL stands for read-eval-print loop. Type an expression, see its value and type:

Terminal
scala-cli repl
TEXT
scala> 21 * 2
val res0: Int = 42

scala> val name = "Scala"
val name: String = Scala

scala> name.toUpperCase
val res1: String = SCALA

The REPL is the quickest way to try out a method of the standard library. Type :quit or press Ctrl+D to leave. Use :paste to enter several lines at once, for example a class definition.

Worksheets and scripts

Two more ways to run quick code:

  • Scripts. A file named something.sc can contain top-level statements with no @main. scala-cli run something.sc executes them in order.
  • Worksheets. IntelliJ IDEA and VS Code with Metals evaluate a .worksheet.sc file as you type and show results inline. Great for experiments.
SCALA
// calc.sc
val prices = List(4.5, 3.0, 12.25)
println(prices.sum)

Compiling to a program

scala-cli can also package your code:

Terminal
scala-cli compile hello.scala
scala-cli package hello.scala -o hello
./hello

For small tools that is enough. For a project with many files, tests and dependencies you will want sbt, which has its own lesson.

Choosing an editor

Editor Notes
IntelliJ IDEA + Scala plugin Powerful refactoring and debugger, many people's first choice
VS Code + Metals Lightweight; uses the Metals language server
Neovim, Emacs, Helix, Zed Work through Metals too

Metals is the language server behind most non-IntelliJ setups. It reads your build, so open the folder that contains the build file (a project.scala, build.sbt or scala-cli directives) and let it import.

Try the idea in a runnable language

You cannot execute scala-cli here, but you can try the "single file, no ceremony" idea in Java 25, which has a similar source launcher. This Java program uses a compact main method:

Java Single-file Java launcher (for comparison)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Scala 3 has the same wish for low ceremony: @main methods and top-level definitions let you write a program without a wrapper class.

Common problems

  • "Command not found". Restart the terminal so the new PATH is read.
  • Wrong Java version. Run java -version. Scala 3 tools prefer 17 or newer.
  • Slow first run. The tools download the compiler and libraries once and then cache them.
  • Corporate proxy. Set the https_proxy environment variable or Coursier's proxy options.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Coursier do?

    Coursier (cs) is the installer and launcher for the Scala ecosystem.
  2. 2. Which command starts the interactive read-eval-print loop?

    scala-cli repl opens the REPL where each line is evaluated and its value and type are printed.
  3. 3. What is a //> using line in a scala-cli file?

    scala-cli treats //> using lines as configuration, so a single file can declare its own dependencies.
  4. 4. Which tool is the standard build tool for larger Scala projects?

    sbt (Scala Build Tool) is the common choice for multi-file projects, tests and publishing.

Summary

  • You need a JDK and Scala tooling; Coursier installs both.
  • scala-cli run file.scala is the fastest way to run code; scala-cli repl is for experiments.
  • .sc files are scripts and worksheets; @main marks a program's entry point.
  • Use sbt when a project outgrows a single file.
  • IntelliJ IDEA and VS Code with Metals are the main editors.

Next: Your First Scala Program