Scala Setup: SBT, IntelliJ IDEA, and Your First Program
Getting Scala running correctly involves three components: the JDK, Scala itself, and SBT (Scala Build Tool). Once these are in place, IntelliJ IDEA with the Scala plugin provides the best development experience. This guide walks through every step.
Prerequisites: Install the JDK
Scala runs on the JVM, so you need a JDK installed first. Use JDK 11 or JDK 17 (LTS releases) for maximum compatibility:
macOS (Homebrew):
brew install openjdk@17Windows: Download from adoptium.net and run the installer.
Linux (Ubuntu/Debian):
sudo apt install openjdk-17-jdkVerify installation:
java -version
# openjdk version "17.0.x" ...Install Scala with Coursier
The recommended way to install Scala is via Coursier — Scala's artifact and application manager:
# macOS/Linux
curl -fL https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz | gzip -d > cs
chmod +x cs && ./cs setup
# Windows: download cs-x86_64-pc-win32.zip from coursier.ioCoursier installs scala, scalac, and sbt automatically. Verify:
scala --version
# Scala code runner version 3.x.x -- ...
sbt --version
# sbt version in this project: ...Install SBT (Scala Build Tool)
SBT is Scala's primary build tool — it compiles code, manages dependencies, and runs tests. If Coursier didn't install it:
# macOS
brew install sbt
# Windows: download from scala-sbt.org/download.html
# Linux: follow instructions at scala-sbt.orgCreating Your First SBT Project
Create a new directory and initialise an SBT project:
mkdir hello-scala && cd hello-scala
sbt new scala/scala3.g8SBT will prompt for a project name. This creates the standard structure:
hello-scala/
├── build.sbt ← project definition and dependencies
├── project/
│ └── build.properties
└── src/
└── main/
└── scala/
└── Main.scalaYour First Scala Program
Open src/main/scala/Main.scala:
@main def hello(): Unit =
println("Hello, Scala!")
println(s"2 + 2 = ${2 + 2}")Run it with SBT:
sbt run
# [info] Hello, Scala!
# [info] 2 + 2 = 4In Scala 3, @main replaces the object extends App pattern. The s"..." is string interpolation — ${expression} evaluates the expression and inserts the result.
The build.sbt File
Every SBT project has a build.sbt that defines the project:
val scala3Version = "3.4.2"
lazy val root = project
.in(file("."))
.settings(
name := "hello-scala",
version := "0.1.0-SNAPSHOT",
scalaVersion := scala3Version,
libraryDependencies ++= Seq(
"org.scalameta" %% "munit" % "0.7.29" % Test
)
)To add a library dependency, find it on search.maven.org and add it to libraryDependencies. Run sbt reload after changes.
Setting Up IntelliJ IDEA
IntelliJ provides the best Scala IDE experience:
- Download IntelliJ IDEA Community Edition from jetbrains.com (free)
- Open IntelliJ → Plugins → search Scala → Install → Restart
- File → Open → select your
hello-scaladirectory - IntelliJ detects the SBT project and imports it automatically
- Wait for indexing to complete (first time takes 2–5 minutes)
Once imported, you get full code completion, inline error highlighting, type information on hover, and integrated SBT task runner.
Using the Scala REPL
For quick experiments, use the Scala REPL (Read-Eval-Print Loop):
scala
# Welcome to Scala 3.x.x
scala> val x = 42
val x: Int = 42
scala> val message = s"The answer is $x"
val message: String = The answer is 42
scala> List(1, 2, 3).map(_ * 2)
val res0: List[Int] = List(2, 4, 6)
scala> :quitThe REPL is invaluable for trying out Scala expressions, testing library functions, and exploring unfamiliar APIs.
Frequently Asked Questions
Q: Should I use Scala 2 or Scala 3? Start with Scala 3 for new learning — it has cleaner syntax, better error messages, and represents the future of the language. If you are joining a team using Scala 2 (common in Spark shops), the core concepts are the same and transitioning between them is straightforward. This course covers both where the syntax differs.
Q: Can I use VS Code instead of IntelliJ? Yes. Install the Metals extension for VS Code (the official Scala language server). Metals provides code completion, go-to-definition, and hover types. IntelliJ's Scala support is more mature and feature-rich, but Metals + VS Code is a solid choice, especially on lower-powered machines.
Q: How do I run a specific file from the command line without SBT?
Use scala FileName.scala for simple single-file scripts. For Scala 3, scripts ending in .sc can be run with scala script.sc. For anything beyond a single-file experiment, SBT is the right tool.
Part of the Scala Mastery Course — Module 2 of 22.
