Skip to main content

Hello World (first programs) · Beginner · 14 min

Hello World in Scala 3 with Scala CLI

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Scala 3.10.1 · Scala CLI 1.19.0 · Eclipse Temurin JDK 25

You will install Scala CLI 1.19, run a Scala 3.10 Hello World with a pinned version, try the REPL, take a typed name from the command line and package the program as a JAR that runs with java -jar.

On this page

Scala is a statically typed language that runs on the Java Virtual Machine (JVM). Your Scala code compiles to the same .class bytecode that Java produces, so a Scala program can use any Java library and runs wherever Java runs. What Scala adds on top is a language that mixes object-oriented and functional programming, with a compiler that works out most types for you.

Scala's best-known home is data engineering. Apache Spark, the engine many companies use to process large datasets across clusters, is written in Scala, and Scala is one of its first-class APIs alongside Python, Java and R. On the backend, teams use Scala with libraries such as Apache Pekko for concurrent systems and with functional libraries such as Cats Effect and ZIO. Because it runs on the JVM, a Scala service sits comfortably next to Java services and uses the same libraries, servers and monitoring tools. For bigger projects than a handful of files, Scala developers use a build tool such as sbt or Mill; Scala CLI stays useful for scripts, small tools and trying ideas.

Since Scala 3.5, the scala command is Scala CLI, a single tool that compiles, runs, opens a REPL and packages programs. You do not install a separate compiler: Scala CLI downloads the Scala version each file asks for and caches it. In this tutorial you install Scala CLI 1.19, run Hello World on Scala 3.10, pin that version in the file itself, try the REPL, then go one step further: take a name and a number as typed command-line arguments and package the program as a JAR that any machine with Java can run.

Step 1 · Check that JDK 25 is installed

Scala CLI compiles and runs your code on a JVM, so you need a JDK first. If you do not have one, follow the first step of Hello World in Java, which installs Eclipse Temurin JDK 25. Then check it:

Bash
java -version

Scala CLI can download a JVM by itself when none is installed, but it uses the one you already have when it finds it, which saves a large download.

Check it worked

The first line starts with something like openjdk version "25.0.5". Any JDK 25 patch release works.

Step 2 · Install Scala CLI

VirtusLab, who build Scala CLI, publish it as a single native program for each platform. Download the 1.19.0 build for your processor, unpack it into ~/.local/bin and name it scala. The x86_64 file is about 45 MB, so on prepaid mobile data, download it once over Wi-Fi and keep a copy.

Do not use sudo apt install scala for this. Ubuntu's own scala package has long been an old Scala 2 release, not Scala 3.

Bash
sudo apt update
sudo apt install -y curl ca-certificates gzip
SCALA_CLI_VERSION=1.19.0
case "$(uname -m)" in
  x86_64) TARGET=x86_64-pc-linux ;;
  aarch64) TARGET=aarch64-pc-linux ;;
  *) echo "Unsupported processor: $(uname -m)"; false ;;
esac
mkdir -p ~/.local/bin
curl -fL "https://github.com/VirtusLab/scala-cli/releases/download/v${SCALA_CLI_VERSION}/scala-cli-${TARGET}.gz" | gzip -d > ~/.local/bin/scala-cli
chmod +x ~/.local/bin/scala-cli
ln -sf scala-cli ~/.local/bin/scala
export PATH="$HOME/.local/bin:$PATH"
scala version

What this does:

  • The case block picks the build for your processor: x86_64 for most laptops and servers, aarch64 for ARM machines.
  • curl -fL ... | gzip -d > ~/.local/bin/scala-cli downloads the compressed program and unpacks it in one go. -f makes curl fail on an HTTP error instead of saving an error page.
  • chmod +x marks the file as a program you can run.
  • ln -sf scala-cli ~/.local/bin/scala adds a second name, scala, pointing at the same file. That is the command name the Scala project uses.
  • export PATH=... puts ~/.local/bin on your path in this shell. Ubuntu adds that folder to your path when you log in, if it exists.

The release does not publish checksum files, so the download relies on HTTPS from GitHub. A program you unpack yourself gets no apt updates. To upgrade, repeat this step with a newer SCALA_CLI_VERSION from the Scala CLI releases page.

Other routes give you the same tool. VirtusLab's installer script (curl -sSLf https://scala-cli.virtuslab.org/get | sh) always installs the newest release. Coursier, the Scala package manager, installs it with cs install scala-cli. On macOS, use the aarch64-apple-darwin or x86_64-apple-darwin file from the same release page. On Windows, run the .msi installer from that page.

Check it worked

scala version prints something like Scala CLI version: 1.19.0 and a default Scala version of 3.10.1. If you see command not found, run the export PATH=... line again.

Step 3 · Write and run Hello World

Make a throwaway folder and create hello.scala in it:

Bash
mkdir -p ~/hello-scala-demo
cd ~/hello-scala-demo
cat > hello.scala <<'SCALA'
@main def hello(): Unit =
  println("Hello, world!")
SCALA

You can also type the two lines into your editor and save them as hello.scala. Line by line:

  • @main marks the method below it as the place the program starts. You can name the method anything; it does not have to be called main, and it does not have to sit inside a class.
  • def hello(): Unit = defines a method called hello with no parameters. Unit is the return type, Scala's word for "returns nothing useful", like void in Java. The = says the method body follows.
  • println("Hello, world!") prints the text and a line break. The two spaces of indentation put this line inside the method.

There are no curly braces and no semicolons. Scala 3 lets you use indentation instead of braces to show where a block starts and ends, much like Python. You may still write { and } if you prefer, and you will see both styles in real code, because Scala 2 code always uses braces.

Now run it:

Bash
scala run hello.scala

The first run is slow and needs a network connection. Scala CLI downloads the Scala 3 compiler and standard library into a cache under ~/.cache/coursier, starts a background compile server called Bloop that keeps the compiler warm, compiles your file and runs it on the JVM. Later runs reuse the cache and the server, so they take a second or two and download nothing.

You will also see a new hidden folder, .scala-build, next to your file. It holds the compiled .class files. You can delete it at any time; Scala CLI rebuilds it.

Check it worked

The last line printed is Hello, world!. Above it you may see lines such as Compiling project and Compiled project, which Scala CLI writes while it builds. Run scala run hello.scala a second time and it finishes much faster.

Step 4 · Pin the Scala version in the file

Right now Scala CLI compiles with its own default version. A colleague with a newer Scala CLI would get a different Scala. Fix the version in the file itself with a using directive, a special comment that Scala CLI reads before compiling. Rewrite hello.scala:

Bash
cat > hello.scala <<'SCALA'
//> using scala 3.10.1

@main def hello(): Unit =
  println("Hello, world!")
  println(s"Running on Scala ${scala.util.Properties.versionNumberString}")
SCALA
scala run hello.scala

What is new:

  • //> using scala 3.10.1 starts with //, so to the Scala compiler it is an ordinary comment. Scala CLI reads it and fetches exactly Scala 3.10.1 for this file. Using directives must come before any code. Other directives add libraries (//> using dep ...) or set compiler options, so a single file can carry its whole build setup.
  • The second println is also indented, so it belongs to the same method. A line back at the left edge would end the method.
  • s"..." is an interpolated string: ${...} puts the result of an expression into the text. scala.util.Properties.versionNumberString reads the version recorded inside the Scala standard library JAR the program is running with. Since Scala 3.8 the standard library is released with the same version number as the compiler, so here it reports the version your directive asked for. (Older Scala 3 releases, such as 3.0 and 3.1, used the Scala 2.13 library and reported 2.13 here.)

Check it worked

The program prints Hello, world! and then Running on Scala 3.10.1.

Step 5 · Try the REPL

The REPL (read, evaluate, print loop) runs Scala one line at a time, which is the quickest way to test an idea. Start it:

Bash
scala repl

At the scala> prompt, type these lines one at a time and press Enter after each:

Text
scala> val price = 250
val price: Int = 250

scala> val label = s"Total: ${price * 3}"
val label: String = Total: 750

scala> price / 2.0
val res0: Double = 125.0

scala> :quit

Look at what the REPL prints back. You never wrote a type, but it reports Int, String and Double. That is type inference: the compiler works out the type from the value, and from then on the type is fixed. val declares a value that cannot be reassigned; try price = 300 and the REPL refuses with an error about reassigning a val. Use var only when a value really has to change. When you do not name a result, the REPL names it for you (res0).

Check it worked

Each line you typed echoes back with its name, its inferred type and its value, as above, and :quit returns you to your shell prompt.

Step 6 · Take a typed name from the command line

In Scala 3, the parameters of a @main method are the program's command-line arguments, and the compiler converts each one to the type you declare. Create greet.scala:

Bash
cat > greet.scala <<'SCALA'
//> using scala 3.10.1

@main def greet(name: String, times: Int): Unit =
  for i <- 1 to times do
    println(s"$i. Hello, $name!")
  val letters = name.length
  println(s"$name has $letters letters.")
SCALA
scala run greet.scala -- Amara 2

Line by line:

  • @main def greet(name: String, times: Int): Unit = declares two parameters. The first argument arrives as a String. The second is converted to an Int before your code runs, so you never parse it yourself.
  • for i <- 1 to times do loops over the numbers from 1 to times. 1 to times is a range, and <- takes each value from it in turn. The do keyword and the indented line below it form the body, again with no braces.
  • val letters = name.length stores the length. Its type, Int, is inferred.
  • $name and $letters put plain names into the text; braces are only needed for longer expressions.

On the command line, -- separates Scala CLI's own options and files from the arguments for your program. Everything after it goes to greet.

Now give it a number that is not a number, and then leave the number out:

Bash
scala run greet.scala -- Amara two
scala run greet.scala -- Amara

In both cases your own code never runs. The compiler generates a small wrapper around a @main method that converts the arguments first. When a conversion fails or an argument is missing, the wrapper prints an Illegal command line message and stops without calling greet.

Check it worked

The first run prints 1. Hello, Amara!, 2. Hello, Amara! and Amara has 5 letters. The run with two prints Illegal command line after first argument: java.lang.NumberFormatException: For input string: "two". The run without a number prints Illegal command line after first argument: more arguments expected. The words "after first argument" tell you the problem is with the second argument, times; the first one, name, was fine.

Step 7 · Package the program as a JAR

scala run needs Scala CLI on the machine. To hand your program to someone who only has Java, package it as an assembly: one JAR that holds your classes and the Scala library together.

Bash
scala --power package greet.scala -o greet.jar --assembly --preamble=false
ls -lh greet.jar
java -jar greet.jar Kofi 1

What each part does:

  • --power unlocks Scala CLI commands that the scala command keeps restricted by default, and package is one of them. To turn it on for good, run scala config power true.
  • package greet.scala -o greet.jar builds the file and writes the output to greet.jar.
  • --assembly puts the Scala standard library inside the JAR, which is why the file is a few megabytes for a short program.
  • --preamble=false leaves out a small launcher script that Scala CLI would otherwise put at the front of the file. Without it, you get a plain JAR that java -jar runs on Linux, macOS and Windows.

Scala CLI found the @main method and recorded greet as the JAR's start class. Under the hood, the compiler turned @main def greet into a class called greet with an ordinary Java-style main method, which is all the JVM needs. Note that there is no -- here: you are calling java, not Scala CLI.

Check it worked

ls -lh shows greet.jar at a few megabytes, and java -jar greet.jar Kofi 1 prints 1. Hello, Kofi! and Kofi has 4 letters.

Step 8 · Fix the mistakes beginners hit and clean up

These come up early. Each has a known cause:

  • scala: command not found: ~/.local/bin is not on your path in this terminal. Log out and back in, or run the export PATH=... line again.
  • Your arguments are treated as file names: you left out --, so Scala CLI tries to read Amara as a source file and stops with an error. Put -- between the file name and your program's arguments.
  • unclosed character literal or a similar error about a character: you used single quotes for text. In Scala, 'A' is a single character and text needs double quotes, "Amara".
  • An error about indentation, or a line that runs at the wrong time: with optional braces, indentation is part of the syntax. Indent with spaces, keep every line of a block at the same depth, and do not mix tabs with spaces.
  • A message that the package sub-command is restricted: you left out --power.
  • Reassignment to val: you gave a val a new value. Use var if it really has to change, and keep val as your default.

Clean up when you are done. First stop Bloop, the background compile server that your first scala run started. It keeps a JVM running, using memory, until you stop it or restart the computer. Then remove the folder you made:

Bash
scala --power bloop exit
cd ~
rm -rf ~/hello-scala-demo

bloop is one of the restricted sub-commands, so it needs --power like package did. Scala CLI starts Bloop again the next time you compile something.

To remove Scala CLI as well, first run scala --power directories. It prints the folders Scala CLI uses for its own data, cache and Bloop's working files; their exact paths depend on your operating system. Delete ~/.local/bin/scala and ~/.local/bin/scala-cli, then the folders that command listed. Finally, delete the ~/.cache/coursier folder to free the space the downloaded compilers and libraries use. Other Scala tools, such as sbt, share that cache, so keep it if you use them.

Check it worked

ls ~/hello-scala-demo reports that there is no such file or directory, and ps aux | grep -i bloop no longer shows a Java process for Bloop (only the grep line itself). scala version still works if you kept Scala CLI.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.