Install Scala on macOS with Homebrew: A Step-by-Step Guide

When I first decided to try Scala, I was excited to explore its blend of functional and object-oriented programming. But before writing any code, I had to get everything set up on my Mac. Fortunately, with Homebrew, the process was smooth and straightforward. If you’re in the same boat, this guide will walk you through the exact steps I took to install Scala using Homebrew, ensuring you’re ready to start coding in no time.

Thank me by sharing on Twitter 🙏

Why Use Homebrew for Scala Installation?

Homebrew is a powerful package manager for macOS that simplifies the process of installing software. Instead of downloading Scala manually and dealing with configurations, Homebrew handles it all. Not only does it install the software, but it also makes updating and maintaining dependencies hassle-free. This ease of use made it my go-to choice for getting Scala on my machine.

1. Installing Scala via Homebrew

The first thing I did was open up my Terminal. With Homebrew already installed on my Mac, the process to install Scala was straightforward. Here’s the exact command I ran:

ShellScript
brew install scala

This command installs the latest version of Scala available in Homebrew’s repository. After a brief download, the installation process finished, and I was ready to check if everything worked.

To verify the installation, I ran:

ShellScript
scala -version

This command displayed the version of Scala installed, confirming that it was successfully installed. If you get an output showing the version number, it means Scala is now available on your Mac and ready to use.

In Case You Need a Specific Version

If you want a different version of Scala (say, for compatibility with a project), you can use:

ShellScript
brew search scala

This command lists available versions. Once you’ve identified the one you need, you can install it by specifying the version.

2. Why I Installed SBT Alongside Scala

Although you can write and compile Scala code with the basic installation, most serious projects require the Scala Build Tool (SBT). SBT simplifies managing dependencies, building projects, and running Scala code. Here’s how I installed it:

ShellScript
brew install sbt

After installing, I confirmed that everything was set up correctly by checking the SBT version:

ShellScript
sbt sbtVersion

SBT will initialize the first time it runs, which might take a minute or two. Once it finishes, you’ll see the installed version, and you’ll be ready to use SBT for any Scala project.

3. Writing Your First Scala Code

After setting everything up, I was eager to write my first Scala code. The quickest way to get started is by launching the Scala REPL (Read-Eval-Print Loop), which allows you to execute Scala code interactively.

Here’s how to open the Scala REPL:

ShellScript
scala

Once inside, I tried a quick example:

Plaintext
val greeting: String = "Hello, Scala!"
println(greeting)

This printed:

Plaintext
Hello, Scala!

The REPL is perfect for experimenting with Scala’s syntax and features before diving into larger projects.

4. Managing Dependencies and Building Projects with SBT

As I progressed, I found that real-world Scala projects often require external libraries. With SBT, adding dependencies becomes easy. Inside an SBT project, you just need to edit the build.sbt file.

Here’s an example build.sbt file I used for one of my projects:

Plaintext
name := "MyScalaProject"
version := "0.1.0"
scalaVersion := "2.13.12"
libraryDependencies += "org.typelevel" %% "cats-core" % "2.10.0"

To create a new SBT project, navigate to a directory and initialize it:

ShellScript
sbt new scala/hello-world.g8

This command sets up a basic project template. You can then compile and run your project using:

ShellScript
sbt run

SBT handles downloading and linking dependencies automatically, which saves time and effort when dealing with complex libraries. This was one of the things that made working with Scala enjoyable for me.

5. Keeping Everything Up to Date

One thing I quickly learned is that it’s essential to keep tools like Scala and SBT up to date. Fortunately, Homebrew makes this easy. To update Scala and SBT, I ran:

ShellScript
brew update
brew upgrade scala sbt

This command ensures that I always have the latest versions installed, keeping my development environment consistent and secure.

Conclusion

Installing Scala on macOS using Homebrew was a smooth process. With just a few commands, I had both Scala and SBT up and running, ready for anything from quick experiments in the REPL to more serious project work. The combination of Homebrew and SBT makes managing dependencies and staying up to date a breeze, freeing up more time to focus on coding.

Whether you’re just getting started with Scala or adding it to your development toolkit, this setup will give you a solid foundation. With everything installed and configured, the fun part—exploring Scala’s powerful features—can finally begin.

Share this:

Leave a Reply