Skip to content

Scala Environment Setup

To start developing with Scala, you need a Java Development Kit (JDK) and the Scala build tools installed on your system.

Step 1: Install Java Development Kit (JDK)

Section titled “Step 1: Install Java Development Kit (JDK)”

Scala runs on the Java Virtual Machine (JVM), so a JDK is required. Scala 3 typically requires JDK 8 or 11+, while many projects still use JDK 8 or 11 Long-Term Support (LTS) versions. Check the requirements for the specific Scala version and libraries you intend to use.

  1. Download: Get a JDK from vendors like Oracle (OpenJDK builds recommended, e.g., Adoptium/Temurin, Amazon Corretto, Oracle OpenJDK). Download an LTS version like JDK 11, 17, or 21.
  2. Install: Follow the installation instructions for your operating system (Windows, macOS, Linux).
  3. Verify: Open a terminal or command prompt and run java -version and javac -version. You should see output indicating the installed Java version.
$ java -version
openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment Temurin-17.0.5+8 (build 17.0.5+8)
OpenJDK 64-Bit Server VM Temurin-17.0.5+8 (build 17.0.5+8, mixed mode, sharing)
$ javac -version
javac 17.0.5

Ensure the JAVA_HOME environment variable is set correctly and the JDK’s bin directory is included in your system’s PATH.

While you can use the scalac compiler and scala runner directly, real-world Scala development relies heavily on build tools for managing dependencies, compiling, testing, and packaging applications. The most common way to get Scala is by installing a build tool.

sbt (Simple Build Tool) is the de facto standard build tool for Scala. Installing sbt will typically also provide the necessary Scala compiler and libraries for your projects.

  • Windows: Download the MSI installer or use package managers like Scoop (scoop install sbt) or Chocolatey (choco install sbt).
  • macOS: Use Homebrew: brew install sbt.
  • Linux: Use SDKMAN! (sdk install sbt), download DEB/RPM packages, or use the universal .tgz archive.

Official sbt installation instructions: https://www.scala-sbt.org/download.html (Note: Links cannot be rendered in the final JSON, but mentioning the source is helpful).

After installing sbt, verify it by running sbt --version in your terminal.

$ sbt --version
sbt version in this project: 1.9.7
sbt script version: 1.9.7

Alternative Installation: Coursier / Scala CLI

Section titled “Alternative Installation: Coursier / Scala CLI”

Coursier is the official Scala installation tool. It can install the scala, scalac commands, and other tools like sbt or scalafmt. The modern scala-cli tool, built on Coursier, is also gaining popularity for scripting, learning, and simple projects.

You can install Coursier and then use it to install Scala components:

# Example using Coursier installation script (check official docs for current command)
$ cs setup
$ scala --version
Scala compiler version 3.3.1 -- Copyright 2002-2023, LAMP/EPFL

Official Scala download page (includes Coursier/Scala CLI instructions): https://www.scala-lang.org/download/

While you can write Scala in any text editor, an IDE provides significant benefits like code completion, error highlighting, debugging, and build tool integration.

  • IntelliJ IDEA with Scala Plugin: Highly recommended, offers excellent Scala support (including Scala 3). The Community Edition is free.
  • Visual Studio Code (VS Code) with Metals: Metals is the official Scala language server, providing a good development experience in VS Code. Free.
  • Eclipse with Scala IDE: Another option, though less common than IntelliJ or VS Code for Scala development recently.

Choose an IDE, install it, and ensure you install the appropriate Scala plugin or extension (Scala Plugin for IntelliJ, Metals for VS Code).

Most IDEs can create new sbt projects for you. Alternatively, you can create one manually:

  1. Create a project directory: mkdir my-scala-app && cd my-scala-app
  2. Create a build.sbt file (the core sbt build definition):
    build.sbt
    ThisBuild / scalaVersion := "3.3.1" // Or your desired Scala 3 version
    // For Scala 2: ThisBuild / scalaVersion := "2.13.12"
    lazy val root = (project in file("."))
    .settings(
    name := "MyScalaApp"
    )
  3. Create source directory: mkdir -p src/main/scala
  4. Create your main Scala file: src/main/scala/Main.scala
    src/main/scala/Main.scala
    @main def hello(): Unit =
    println("Hello, Scala with sbt!")
  5. Run from the terminal in your project directory: sbt run
$ sbt run
[info] welcome to sbt 1.9.7 (Temurin Java 17.0.5)
...
[info] running Main
Hello, Scala with sbt!
[success] Total time: ...

You are now set up to compile, run, and manage Scala projects using sbt.