Skip to content

Latest commit

 

History

History
190 lines (124 loc) · 8.12 KB

getting-started.md

File metadata and controls

190 lines (124 loc) · 8.12 KB
layout title
singlepage-overview
Getting Started with Scala 3

Try Scala without installing anything

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.

Install Scala on your computer

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.

Using the Scala Installer (recommended way)

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 and scalac command-line tools for Scala 2.13 -- not Scala 3).

For more information, read the coursier-cli documentation.

... or manually

You only need two tools to compile, run, test, and package a Scala project: Java 8 or 11, and sbt. To install these manually:

  1. Download Java from Oracle Java 8, Oracle Java 11, or AdoptOpenJDK 8/11. Refer to JDK Compatibility for Scala/Java compatibility detail.
  2. Install sbt

Create a “Hello, world” project with 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.

Using the command line

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:

  1. cd to an empty folder.
  2. 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.
  3. When prompted, name the application hello-world. This will create a project called "hello-world".
  4. 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 %}

With IntelliJ IDEA

You can skip the rest of this page and go directly to Building a Scala Project with IntelliJ and sbt {% endcomment %}

Open the “Hello, world” project

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.

Using IntelliJ IDEA

  1. Download and install IntelliJ Community Edition
  2. Install the Scala plugin by following the instructions on how to install IntelliJ plugins
  3. Open the build.sbt file, then choose Open as a project

Using VS Code with Metals

  1. Download VS Code
  2. Install the Metals extension from the Marketplace
  3. 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 the source code

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.

Run the “Hello, world” project

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:

  1. cd into hello-world.
  2. Run sbt. This opens up the sbt console.
  3. 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 a target 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.

Next steps

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