Skip to Content
Course content

1: Setting Up Scala and sbt

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Think of starting a Scala project like setting up a professional commercial kitchen. You have your recipes (your Scala code), but a recipe is just ink on paper. To actually get a meal on the table, you need the industrial ovens and stoves (the Java Virtual Machine, or JVM) to do the heavy lifting. But here is the catch: a head chef doesn't spend their time manually hunting for every single grain of salt or chopping every onion from scratch every single time. They have a kitchen manager who ensures the ingredients are ordered, the workstations are prepped, and the staff is coordinated. In our world, that kitchen manager is sbt (the Scala Build Tool).

To map this directly to your machine: your Scala code is the recipe, the JVM is the oven that actually executes the instructions, and sbt is the manager that handles your libraries, compiles your code, and runs your tests. Without sbt, you'd be manually managing classpath strings and JAR files—a nightmare I've lived through, and one I'm saving you from.

Getting the Tooling on Your Machine

Back in the day, installing Scala was a fragmented mess. Now, we use Coursier (often just called cs). It's essentially a "manager for your managers." I highly recommend using the cs setup command. It doesn't just install the Scala compiler; it handles the JVM (JDK) and sbt in one go.

When you run the setup, you're essentially telling your computer: "I want to be a Scala developer; give me everything I need to actually start typing." If you've already got a JDK installed, Coursier is smart enough to recognize it, but letting it handle the versioning usually saves you from those annoying "Unsupported Class Version" errors that haunt the JVM ecosystem.

The Anatomy of an sbt Project

sbt is opinionated about where things go. If you put your files in the wrong folder, sbt will simply ignore them, and you'll spend an hour wondering why your code isn't compiling. I've been there.

A standard Scala project looks like this:

my-project/
├── build.sbt            <-- The "Kitchen Manager's" instruction manual
└── src/
    ├── main/
    │   └── scala/       <-- Your actual application logic lives here
    └── test/
        └── scala/       <-- Your test suites live here

The build.sbt file is the most important file in your directory. It's where you define your project name, the version of Scala you're using, and any external libraries (dependencies) you need. For example, if you wanted to build a project that parses JSON, you wouldn't write a JSON parser from scratch; you'd add a line to build.sbt to pull in a library like Circe or play-json.

Actually Running Your Code

You don't usually run Scala files by calling the compiler directly from the terminal. Instead, you interact with the sbt shell. Navigate to your project root and type sbt. This drops you into an interactive prompt.

From here, you have a few key commands:

  • compile: Checks your code for errors and turns it into bytecode.
  • run: Compiles the code (if needed) and executes the main logic.
  • test: Runs everything in your src/test folder.
  • console: Opens a REPL (Read-Eval-Print Loop), which is like a scratchpad where you can test snippets of Scala code using the libraries defined in your build.sbt.

My advice? Spend a lot of time in the console. It's the fastest way to prototype a logic change without having to re-run the entire application.




📋 Practical Task

Initializing a Weather-Tracker Project Structure

Your goal is to manually create the directory skeleton for a project called weather-tracker and configure it to use Scala version 3.3.0.

Perform the following steps in your terminal and editor:

  1. Create a root folder named weather-tracker.
  2. Inside that folder, create the nested directory structure: src/main/scala and src/test/scala.
  3. Create a file named build.sbt in the root folder.
  4. Inside build.sbt, add the following configuration:
    name := "WeatherTracker"
    scalaVersion := "3.3.0"
  5. Launch the sbt shell from the root directory and run the compile command to ensure the project initializes without errors.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.