How to Install and Use Scala on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

Scala (which stands for β€œScalable Languageβ€œ) is a general-purpose programming language built on the Java virtual machine that combines functional programming and object-oriented programming paradigms.

It addresses Java’s limitations while offering rich features like object-oriented design, curly-brace syntax, immutability, lazy evaluation, pattern matching, and operator overloading, enabling a more concise and expressive coding approach.

Scala can be compiled into Java bytecode, enabling it to seamlessly run on the Java Virtual Machine (JVM), in browsers through JavaScript, or even as a standalone native executable.

This article will guide you through the seamless installation of Scala on your preferred Linux system, accompanied by a step-by-step guide to running a sample Scala program.

Tutorial Details

DescriptionScala
Difficulty LevelLow
Root or Sudo PrivilegesYes
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisites–
Internet RequiredYes

Key Features

Scala, a remarkably versatile programming language, empowers developers to create a diverse array of applications, spanning from Web development to data processing, analytics, scripting and automation, machine learning, desktop applications, and beyond.

Scala boasts a remarkable array of key features, which include:

  • Functional Programming
  • Object-Oriented Programming
  • Static Typing
  • Conciseness
  • Type Inference
  • Concurrency
  • Library Ecosystem
  • Interactive Shell (REPL)

It’s intriguing, isn’t it? Let’s delve into the process of installing Scala on your Linux system.

How to Install Scala on Linux

Scala can be installed in two different ways: one is by using the official curl command to install and configure Scala without any interaction, and another is by manually installing Java and Scala from the repositories.

Let’s begin by utilizing the official and recommended approach using the curl command:

Installing Scala on Linux via Curl Command (Recommended)

1. Launch your terminal and execute the provided command to refresh your system’s repository information to the most recent version.

$ sudo apt update                                                                                                            #For Debian or Ubuntu
$ dnf check-update                                                                                                         #For Red Hat or Fedora
$ sudo pacman -Sy                                                                                                          #For Arch or Manjaro
$ sudo zypper ref                                                                                                             #For OpenSUSE

2. Execute the following command to install the latest version of Scala on your preferred Linux (x86-64 or ARM64) system.

#For x86-64 architecture (Generally for Laptop and Desktop with Intel or AMD CPU)
$ curl -fL https://github.com/coursier/coursier/releases/latest/download/cs-x86_64-pc-linux.gz | gzip -d > cs && chmod +x cs && ./cs setup

#OR

#For ARM64 architecture (like Raspberry Pi)
$ curl -fL https://github.com/VirtusLab/coursier-m1/releases/latest/download/cs-aarch64-pc-linux.gz | gzip -d > cs && chmod +x cs && ./cs setup

3. Upon executing the aforementioned command, it will initiate the download of necessary resources while seamlessly identifying the JVM. In case the JVM is not detected, a prompt will appear on your screen – just press β€œY” to confirm.

Scala installation prompts that the JVM is missing

4. It will initiate the download and configuration of OpenJDK 8 in the background. Following this, you’ll be prompted to update the β€œ~/.profile” file – just press β€œY” to confirm.

Modifying the "~/.profile" file

5. Finally, you will be prompted to include the path β€œ~/.local/share/coursier/bin” in your shell configuration file. Just confirm by pressing β€œY” for yes.

Updating the shell configuration file

It will now begin downloading the essential files necessary for running Scala on your system.

Installing Scala via the curl command

After completing the installation, relaunch your terminal or restart your system in order to apply the changes. Then execute the provided command to confirm the successful installation of Scala.

$ scala -version

Output:

Checking the Scala version installed via curl

Installing Scala on Linux via Repository

This method involves manual steps to set up the JDK on your system. For detailed guidance, please refer to our article on installing Java on Linux.

Assuming you already have JDK installed, you can proceed by selecting one of the following commands to effortlessly install Scala from your system repository:

$ sudo apt install scala                                                                                           #For Debian or Ubuntu
$ sudo dnf install scala                                                                                           #For Red Hat or Fedora
$ yay -S scala                                                                                                           #For Arch or Manjaro
$ sudo zypper install scala                                                                                     #For OpenSUSE

Once the installation is complete, verify that Scala has been successfully installed by executing the following command:

$ scala -version

Output:

Checking the Scala version installed via repository

Note that the Scala β€œ2.11.12” version was acquired through the repository, in contrast to the official curl command, which installed the latest Scala β€œ3.3.0” version.

How to Run a Scala Program on Linux

After installing Scala on your system by following any of the instructions mentioned earlier in this article, you are now ready to embark on your Scala programming journey. Start by creating a straightforward β€œHello World” program.

1. Create a text file named β€œHelloWorld.scala” using the text editor of your preference.

$ vim HelloWorld.scala

2. Copy and paste the simple β€œHello World” program in the aforementioned file.

object HelloWorld {
    def main(args: Array[String]): Unit = {
        println("Hello, World!")
        println("- Linux TLDR!")
    }
}

3. Use the β€œscalac” command to compile the Scala program into bytecode.

$ scalac HelloWorld.scala

4. After compiling the program, you can use the β€œscala” command to run it.

$ scala HelloWorld

Output:

Running a simple Scala program

Congratulations on successfully creating your first Scala program!

Final Word

I hope you find this article useful. If you have any questions or queries related to the topic, then feel free to tell us in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.