Scala 3 for Beginners: Install Scala and Run Your First Program
Edited by Dev Kilaru, Senior Data Analyst. How we make our content
Introduction
Scala is a statically typed language for the Java Virtual Machine that combines object-oriented and functional programming. It is the language Apache Spark is written in, which makes it valuable for data engineering on platforms such as Databricks, and it is also used for backend services and streaming systems. This course teaches Scala 3, the current major version, whose syntax is cleaner than older Scala 2 code you may still find online. This first lesson gets your tools working and runs a program.
Prerequisites
- A Windows, macOS, or Linux computer
- A terminal or command prompt
- No Scala or Java knowledge is required, though any programming experience helps
Step-by-Step Guide
Step 1: Understand what you are installing
Scala source code is compiled to JVM bytecode, so it needs a Java Development Kit to run. Install a long-term support JDK such as 17 or 21 from a vendor like Eclipse Temurin. On top of the JDK you need the Scala compiler and a build tool. The simplest route is Coursier, the official Scala installer, which downloads a JDK if needed, the Scala command-line tools, and sbt, the standard build tool for larger projects. Because everything runs on the JVM, Scala programs can use any Java library, which is a big part of its appeal.
Step 2: Install with Coursier
Follow the instructions on the Scala website's install page for your operating system to get the Coursier launcher, called cs. Running its setup command then installs the standard Scala tools and places them on your path. Open a new terminal window afterwards so the path change takes effect. If a command is not found, the new terminal is the first thing to check. Installers change over time, so the official page is the most reliable reference for the exact steps on your platform.
cs setup
java -version
scala-cli version
sbt --versionStep 3: Write and run a script
Scala CLI runs a single file without any project setup, which is ideal for learning. Create a file named hello.scala. The main annotation marks a function as the program entry point, replacing the verbose static main method that Scala 2 and Java require. Indentation defines blocks in Scala 3, so there are no braces here. Run the file with scala-cli run, and the first run may take a little longer while it compiles and downloads the compiler.
// hello.scala
@main def hello(): Unit =
println("Hello, CaliberTek!")Step 4: Try the REPL for quick experiments
The REPL, which stands for read-eval-print loop, evaluates each line you type and prints the result with its type. It is the quickest way to try an idea or learn a library method. Each result is stored in a generated name such as res0, so you can reuse it. Exit with Ctrl+D or the :quit command. Use the REPL for tiny experiments and files for anything you want to keep.
scala-cli repl
scala> 2 + 3
val res0: Int = 5
scala> "scala".toUpperCase
val res1: String = SCALAStep 5: Pass arguments and read input
A main function can take command-line arguments as a sequence of strings. This small program greets each name given on the command line, and it introduces two ideas you will use constantly: the for loop with the do keyword, and string interpolation with the s prefix, which inserts values into text. Anything after the script name on the command line is passed to the program. If no arguments are given, the loop simply does nothing.
@main def greet(names: String*): Unit =
for name <- names do
println(s"Hello, $name!")
// scala-cli run greet.scala -- Asha BenStep 6: Choose an editor
Scala has first-class support in IntelliJ IDEA with the Scala plugin and in Visual Studio Code or other editors through Metals, a language server that gives completion, error messages, and navigation. For a Scala CLI file, open the folder in VS Code with the Metals extension and let it import the project. A good editor matters more in Scala than in many languages, because the type information it shows on hover teaches you what the compiler infers. Do not worry about configuring everything on day one, because the REPL and a text file are enough for the first lessons.
FAQs
- Should I learn Scala 2 or Scala 3?
- Learn Scala 3 for new work. It is the current version, has a cleaner syntax, and can use Scala 2 libraries. Many Apache Spark projects still use Scala 2.12 or 2.13, so you may meet older syntax, and the core ideas carry over directly.
- Do I need to know Java first?
- No. It helps to understand what the JVM is, but Scala is a complete language you can learn on its own. Knowing Java does make it easier to read library documentation and stack traces.
- Why learn Scala if I use Python for data work?
- Scala gives you static typing, speed, and direct access to Spark's native API. It is common in data platform teams and in streaming and backend systems, and learning it makes you more effective at reading Spark internals even if you mostly write PySpark.
Summary
You now have a working Scala 3 setup, you can run a script with Scala CLI, you have tried the REPL, and you have written a main function that takes arguments and uses string interpolation. The next lesson covers values, types, and the control structures that make Scala code expression-oriented.
Related tutorials
- Schedule and Debug Shell Scripts: cron, bash -x, and Portability
- Shell Scripting Project: A Backup Script with Rotation and a Log Report
- Writing Safe Bash Scripts: set -euo pipefail, getopts, and trap
- Shell Pipes, Redirection, grep, sed, and awk for Text Processing
- Bash Loops, Arrays, and Functions Explained
Comments
Log in or create a free account to join the discussion.