layout | title |
---|---|
singlepage-overview |
Getting Started with Scala 3 |
To start experimenting with Scala 3 right away, use “Scastie” in your browser. Scastie is an online “playground” where you can experiment with Scala examples to see how things work, with access to all Scala compilers and published libraries.
Installing Scala means installing various command-line tools and build tools. We recommend using the Scala installer tool "Coursier" that automatically installs all the requirements, but you can still manually install each tool.
The Scala installer is a tool named Coursier, whose main command is named cs
.
It ensures that a JVM and standard Scala tools are installed on your system.
Install it on your system with the following instructions.
Follow the instructions to install the cs
launcher then run:
$ ./cs setup
Along with managing JVMs, cs setup
also installs useful command line tools:
- A JDK
- The sbt and mill build tools
- Ammonite, an enhanced REPL
- scalafmt, the Scala formatter
- The Coursier CLI, to install further Scala-based applications
- (the
scala
andscalac
command-line tools for Scala 2.13 -- not Scala 3).
For more information, read the coursier-cli documentation.
You only need two tools to compile, run, test, and package a Scala project: Java 8 or 11, and sbt. To install these manually:
- Download Java from Oracle Java 8, Oracle Java 11, or AdoptOpenJDK 8/11. Refer to JDK Compatibility for Scala/Java compatibility detail.
- Install sbt
To create a project, you can either use a command-line tool or an IDE. If you are familiar with the command line, we recommend that approach.
sbt is a build tool for Scala. sbt compiles, runs, and tests your Scala code. (It can also publish libraries and do many other tasks.)
To create a new Scala project with sbt:
cd
to an empty folder.- Run this command
sbt new scala/hello-world.g8
. This pulls the 'hello-world' template from GitHub. It also creates a target folder, which you can ignore. - When prompted, name the application
hello-world
. This will create a project called "hello-world". - Let’s take a look at what just got generated:
hello-world/
project/ (sbt uses this for its own files)
build.properties
src/main/scala/ (all of your Scala code goes here)
Main.scala (entry point of program)
build.sbt (sbt’s build definition file)
The scala file Main.scala
in src/main/scala
is all we need for now.
More documentation about sbt can be found in the Scala Book and in the official sbt documentation
{% comment %}
You can skip the rest of this page and go directly to Building a Scala Project with IntelliJ and sbt {% endcomment %}
Let’s use an IDE to open the project. The most popular ones are IntelliJ IDEA and VS Code. They both offer rich IDE features, but you can still use many other editors.
- Download and install IntelliJ Community Edition
- Install the Scala plugin by following the instructions on how to install IntelliJ plugins
- Open the build.sbt file, then choose Open as a project
- Download VS Code
- Install the Metals extension from the Marketplace
- Next, open the directory containing your build.sbt file. When prompted to do so, select Import build.
Metals is a “Scala language server” that provides support for writing Scala code in VS Code and other editors like Atom, Sublime Text, and more, using the Language Server Protocol. (For details on how Metals works, see, “Write Scala in VS Code, Vim, Emacs, Atom and Sublime Text with Metals”.)
View these two files in your IDE:
- build.sbt
- src/main/scala/Main.scala
When you run your project in the next step, the configuration in build.sbt will be used to run the code in src/main/scala/Main.scala.
If you’re comfortable using your IDE, you can run the code in Main.scala from your IDE.
Otherwise, you can run the application from a terminal with these steps:
cd
into hello-world.- Run
sbt
. This opens up the sbt console. - Type
~run
. The~
is optional and causes sbt to re-run on every file save, allowing for a fast edit/run/debug cycle. sbt also generates atarget
directory for its own use, which you can ignore.
When you’re finished experimenting with this project, press [Enter]
to interrupt the run
command.
Then type exit
or press [Ctrl][d]
to exit sbt and return to your command line prompt.
Now that you’ve created a first “Hello, world” example with Scala 3, you’re ready for some next steps. Consider checking out:
- The Scala 3 Book, which provides a set of short lessons introducing Scala’s main features
- The migration guide helps you to migrate your existing Scala 2 code base to Scala 3.
When you want to connect with other Scala users, there are several mailing lists and real-time chat rooms available. Check out our Scala community page for a list of these resources, and for where to reach out for help.
$ curl -Lo cs https://git.io/coursier-cli-linux && chmod +x cs && ./cs setup
Follow the instructions to install the cs
launcher then run:
$ ./cs setup
If you use Homebrew:
$ brew install coursier/formulas/coursier && cs setup
Alternatively, if you don’t use Homebrew:
$ curl -Lo cs https://git.io/coursier-cli-macos && chmod +x cs && (xattr -d com.apple.quarantine cs || true) && ./cs setup
Download and execute the Scala installer for Windows based on coursier