Install Scala and Set Up JVM
Install Scala and set up the JVM — Scala for Python Developers.
Focus: install scala and set up the jvm
You already know the pain: you've heard Scala's type safety and functional powers are game-changers, but the first step — getting it running — feels like a maze of JVM versions, build tools, and PATH variables. If you've only ever installed Python with pip, Scala's install process can look intimidating. The good news: I'll walk you through install Scala and set up the JVM in minutes, not hours. By the end, you'll have a working Scala environment to build your first program — and you'll be ready to tackle the next lesson in the Scala for Python Developers track.
The problem this lesson solves
Scala doesn't come with its own runtime like Python does. It runs on the Java Virtual Machine (JVM), which is a huge advantage for performance and Java interoperability, but it also means you must install two pieces of software before you can run a single line of Scala.
If you skip proper setup, you'll fight cryptic errors like scala: command not found or UnsupportedClassVersionError — time you'd rather spend writing code. For a Python developer, the closest analogy is installing Python 3.8 when your system expects 3.10: everything breaks at the worst possible moment.
This lesson exists to remove that friction. You'll learn exactly what the JVM is, how to install it, how to pair it with a build tool like sbt or Scala CLI, and how to verify everything works. No more guessing, no more searching Stack Overflow at 2 a.m.
Core concept / mental model
Think of Scala as a high-performance engine, and the JVM as the chassis it has to be bolted onto. Python bundles its own engine (the interpreter) with your code, so python script.py just works. Scala, however, compiles to bytecode that the JVM interprets and runs. Without the JVM, Scala code is just text files with no way to execute.
Here's the mental model in three layers:
- JVM: The runtime engine — responsible for memory management, garbage collection, and executing bytecode. It's your
python3equivalent, but it's language-agnostic (Java, Scala, Kotlin all run on it). - Scala compiler (
scalaorscalac): Translates your.scalasource code into bytecode. Think of it aspip install+ interpreter combined — it validates your code's types and produces runnable output. - Build tool: A project manager that handles dependencies, compilation, and running your code. This is your
pip+virtualenvrolled into one, but with features like incremental compilation and a REPL.
The beauty is that Scala's tooling is designed to be installed once and reused across projects. Once the JVM and build tool are set up, you won't need to think about them again — just like you don't reinstall Python every time you start a new script.
How it works step by step
Installing Scala is a two-stage process: first the JVM, then the build tool and Scala compiler. Here's the logical order, with the why behind each step.
1. Install the JVM
Scala needs a JDK (Java Development Kit) version 8 or newer. For modern Scala (3.x), use JDK 17 or 21. The JVM provides the runtime, and the JDK includes the java command you'll use to launch Scala programs.
2. Choose your installation method
You have three solid paths:
- Coursier (recommended): A universal installer for Scala and JVM — it handles both in one shot. It's the official way to install Scala 3.
- sbt launcher: The traditional Scala build tool, often paired with a manually installed JDK.
- Scala CLI: A newer, script-oriented tool perfect for learning and small projects.
3. Verify your environment
Test that both java and scala are on your PATH, and that the versions match. A mismatch (e.g., Scala 3 requiring JDK 17 but you have JDK 11) will cause errors like UnsupportedClassVersionError.
4. Run your first REPL command
The REPL (Read-Eval-Print Loop) is your interactive playground — like python in your terminal. It's the fastest way to test Scala syntax before writing full files.
Hands-on walkthrough
Let's get your environment running. I'll walk through the Coursier method (the quickest), then show you the manual approach for when you need full control.
Option A: Quick install via Coursier (recommended)
Open your terminal and run:
# On macOS (Homebrew)
brew install coursier/formulas/coursier
coursier setup
# On Linux
curl -fL "https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz" | gzip -d > cs
chmod +x cs
./cs setup
# On Windows (PowerShell)
curl -fL https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-win.zip -o cs.zip
tar -xf cs.zip
.\cs-x86_64-pc-win.exe setup
After the setup script finishes, it will install a JDK and the Scala compiler automatically. Restart your terminal, then verify:
scala -version
java -version
Expected output (versions will vary):
Scala code runner version 3.3.1 -- Copyright 2002-2023, LAMP/EPFL
openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment (build 17.0.9+9)
OpenJDK 64-Bit Server VM (build 17.0.9+9, mixed mode, sharing)
Option B: Manual JVM + sbt install
If you prefer a manual approach, install a JDK first:
# macOS
brew install openjdk@17
# Ubuntu/Debian
sudo apt update && sudo apt install openjdk-17-jdk
# Windows — download from adoptium.net and run the installer
Then install sbt:
# macOS
brew install sbt
# Linux
curl -fL https://github.com/sbt/sbt/releases/download/v1.9.7/sbt-1.9.7.tgz | tar -xz -C /opt
sudo ln -s /opt/sbt/bin/sbt /usr/local/bin/sbt
# Windows — download the .msi from scala-sbt.org
Now create your first project:
mkdir hello-scala && cd hello-scala
sbt new scala/scala3.g8
The sbt new command scaffolds a project structure, similar to running cookiecutter in Python. When prompted, leave defaults for names, then run:
sbt run
You'll see Hello, world! printed after the first compilation. This is your Python's python main.py moment — but with a compiler instead of an interpreter.
Test the Scala REPL
Once the JVM is set up, open the REPL:
scala
Then type a quick expression:
val language = "Scala"
println(s"Welcome to $language!")
Output:
val language: String = Scala
Welcome to Scala!
Notice how the REPL shows the type of every value — that's your first taste of Scala's static typing, a huge difference from Python's dynamic style.
Verify with a standalone script (Scala CLI)
If you used Coursier, you also get scala-cli. Write a file:
// hello.scala
@main def hello(): Unit =
println("Hello from Scala!")
Run it:
scala-cli run hello.scala
Expected output:
Compiling project...
Hello from Scala!
This is your equivalent of python hello.py.
Compare options / when to choose what
| Method | Best for | JVM handling | Learning curve | Notes |
|---|---|---|---|---|
| Coursier | Beginners, quick setup | Automatic | Low | Installs everything; also manages JVM versions |
| Manual JDK + sbt | Teams with existing JVM infra | Manual | Medium | Full control; standard for enterprise projects |
| Scala CLI | Scripts, learning, notebooks | Delegates to existing JDK | Very low | Ideal for single-file experimentation |
Pro tip: For this track, start with Coursier. It handles the JVM version for you, so you won't stumble on
UnsupportedClassVersionErrorlater.
Troubleshooting & edge cases
Error: scala: command not found
Cause: The install path isn't in your PATH.
- Fix: Re-run
coursier setupand ensure it modifies your shell profile (.bashrc,.zshrc). Or add the path manually:export PATH="$PATH:$HOME/.local/share/coursier/bin".
Error: UnsupportedClassVersionError
Cause: Your Scala version needs a newer JDK than what's installed.
- Fix: Check
java -version. If it's below 17, install JDK 17 via Coursier (cs java --update) or your package manager.
Error: Failed to construct terminal; falling back to dumb terminal (sbt)
Cause: The terminal doesn't support ANSI escapes (often in CI or minimal shells).
- Fix: Set
SBT_OPTS="-Dfile.encoding=UTF8"or use-batchmode:sbt -batch run.
Slow first compile
You'll see a delay the first time you run sbt because it downloads dependencies. This is normal — like pip install fetching packages. Subsequent runs are much faster thanks to incremental compilation.
Windows-specific: PATH issues
- Fix: After installing via the
.msi, open a new PowerShell window (not the same one). The installer updates PATH but doesn't propagate to open terminals.
What you learned & what's next
You've now installed Scala and set up the JVM, which means you've crossed the biggest hurdle for Python developers moving to Scala. Let's recap what you achieved:
- Explained why Scala needs the JVM — a runtime that executes compiled bytecode, unlike Python's interpreter.
- Completed a hands-on installation using Coursier, sbt, or Scala CLI — all verified with version checks and a running REPL.
- Understood how to distinguish between the JVM, the Scala compiler, and the build tool — a mental model that will guide you in every future project.
You're now ready to dive into Scala's syntax. The next lesson in this track will introduce you to Scala basics: variables, types, and functions, where you'll write your first real programs using val, def, and expressive type inference. That's where Scala's elegance really shines — and you'll have the environment to enjoy it.
Remember: The setup is the hardest part. Once this is done, Scala's learning curve flattens out, and you'll be writing functional, type-safe code in no time.
Practice recap
Now that your environment is ready, open a terminal and type scala to launch the REPL. Try defining a few variables and printing them — experiment with val (immutable) and var (mutable) to see how Scala's type inference works. Then create a hello.scala file and run it with scala-cli run hello.scala to complete a full compile-run cycle. This 10-minute practice will solidify the setup and prepare you for the next lesson on Scala basics.
Common mistakes
- Installing only Scala without a JDK — Scala won't run without a JVM, and you'll get
Error: Unable to find a java runtime. - Using an old JDK (e.g., 8) with Scala 3 — causes
UnsupportedClassVersionError; always use JDK 17+ for Scala 3. - Not restarting the terminal after installation, so commands like
scalaaren't found because PATH hasn't refreshed. - Forgetting to check for multiple Java versions —
java -versionmay show an old JVM, breaking Scala's version validation.
Variations
- Use Scala CLI for a script-focused workflow — it simplifies running single files and is great for learning or prototyping.
- Try IntelliJ IDEA with the Scala plugin — it provides a GUI to manage JVM versions and sbt projects, handy for IDE-centric developers.
- Leverage Docker with an official Scala image for isolated, reproducible environments — useful for CI pipelines or trying Scala without local installation.
Real-world use cases
- Setting up a data engineering pipeline with Apache Spark, which runs on the JVM and is written in Scala.
- Building a microservice with Play Framework that needs a consistent JVM setup across developer and production environments.
- Using Scala in a CI/CD pipeline (GitHub Actions) to compile and test code on ephemeral JVM containers.
Key takeaways
- Scala runs on the JVM, so installing a JDK (17+) is a mandatory first step.
- Coursier is the fastest and most reliable way to install both Scala and the JVM with a single command.
- Always verify with
scala -versionandjava -versionbefore writing code to avoid version mismatch headaches. - Use
sbt runorscala-cli runto execute your first program, just likepython script.py. - The Scala REPL is your sandbox — use it to test expressions interactively, with type feedback at every step.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.