How to install Scala on Linux 9

Share on Social Media

In this Linux tutorial, you will learn how to install Scala on Linux 9 or other Red Hat based Linux distributions. #centlinux #linux #scala

What is Scala? :

Scala is a strong statically typed high-level general-purpose programming language that supports both object-oriented programming and functional programming. Designed to be concise, many of Scala’s design decisions are aimed to address criticisms of Java.

Scala source code can be compiled to Java bytecode and run on a Java virtual machine (JVM). Scala can also be compiled to JavaScript to run in a browser, or directly to a native executable. On the JVM Scala provides language interoperability with Java so that libraries written in either language may be referenced directly in Scala or Java code. Like Java, Scala is object-oriented, and uses a syntax termed curly-brace which is similar to the language C. Since Scala 3, there is also an option to use the off-side rule (indenting) to structure blocks, and its use is advised. Martin Odersky has said that this turned out to be the most productive change introduced in Scala 3.

Unlike Java, Scala has many features of functional programming languages (like Scheme, Standard ML, and Haskell), including currying, immutability, lazy evaluation, and pattern matching. It also has an advanced type system supporting algebraic data types, covariance and contravariance, higher-order types (but not higher-rank types), anonymous types, operator overloading, optional parameters, named parameters, raw strings, and an experimental exception-only version of algebraic effects that can be seen as a more powerful version of Java’s checked exceptions.

The name Scala is a portmanteau of scalable and language, signifying that it is designed to grow with the demands of its users. (Source: Wikipedia)

Video to install Scala on Linux:

YouTube player

Environment Specification:

We are using a minimal Rocky Linux 9 virtual machine with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – Rocky Linux release 9.1 (Blue Onyx)
  • Hostname – scala-01.centlinux-com.preview-domain.com
  • IP Address – 192.168.88.128/24

Update your Rocky Linux Server:

It is a best practice to update your Linux operating system, before installing a new software.

By using a ssh client, connect with your Rocky Linux server as root user.

Refresh your Yum cache by using dnf command.

# dnf makecache
Rocky Linux 9 - BaseOS                          1.3 kB/s | 4.1 kB     00:03
Rocky Linux 9 - AppStream                       1.2 kB/s | 4.5 kB     00:03
Rocky Linux 9 - Extras                          974  B/s | 2.9 kB     00:03
Metadata cache created.

Execute following command at Linux bash prompt to update your Linux operating system.

# dnf update -y

If the above command update software packages related to Linux Kernel.

Then, you should reboot your Linux operating system with new Linux Kernel before installing Scala 3 software.

# reboot

After reboot, check the versions of your Linux Kernel and Linux operating system.

# cat /etc/rocky-release
Rocky Linux release 9.1 (Blue Onyx)

# uname -r
5.14.0-162.12.1.el9_1.0.2.x86_64

Install Scala 3 Prerequisites:

Scala 3 requires JVM (Java Virtual Machine). Therefore, you may install Oracle Java or OpenJDK, which is available in standard yum repositories, by using dnf command.

# dnf install -y wget gzip java-17-openjdk

We have also install wget and gzip software packages to download and unzip the Coursier software.

After installation, verify the version of Java.

# java --version
openjdk 17.0.6 2023-01-17 LTS
OpenJDK Runtime Environment (Red_Hat-17.0.6.0.10-3.el9_1) (build 17.0.6+10-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-17.0.6.0.10-3.el9_1) (build 17.0.6+10-LTS, mixed mode, sharing)

Install Scala on Linux:

Execute wget command to download Coursier setup straight from the Linux commandline.

# wget https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz
--2023-02-14 21:46:28--  https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz
Resolving github.com (github.com)... 20.207.73.82
Connecting to github.com (github.com)|20.207.73.82|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.githubusercontent.com/coursier/launchers/master/cs-x86_64-pc-linux.gz [following]
--2023-02-14 21:46:29--  https://raw.githubusercontent.com/coursier/launchers/master/cs-x86_64-pc-linux.gz
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.109.133, 185.199.110.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 20759374 (20M) [application/octet-stream]
Saving to: ‘cs-x86_64-pc-linux.gz’

cs-x86_64-pc-linux. 100%[===================>]  19.80M  1.12MB/s    in 19s

2023-02-14 21:46:49 (1.04 MB/s) - ‘cs-x86_64-pc-linux.gz’ saved [20759374/20759374]

Now, use gunzip command to extract Coursier zip file.

# gunzip cs-x86_64-pc-linux.gz

Rename the extracted file to cs for better accessibility. Also grant the execute privilege to cs file.

# mv cs-x86_64-pc-linux cs
# chmod +x cs

Execute cs command to initiate Scala 3 installation on Rocky Linux server.

# ./cs setup
Checking if a JVM is installed
Found a JVM installed under /usr/lib/jvm/java-17-openjdk-17.0.6.0.10-3.el9_1.x86_64.

Checking if ~/.local/share/coursier/bin is in PATH
  Should we add ~/.local/share/coursier/bin to your PATH via ~/.profile, ~/.bash_profile? [Y/n] Y

Checking if the standard Scala applications are installed
  Installed ammonite
  Installed cs
  Installed coursier
  Installed scala
  Installed scalac
  Installed scala-cli
  Installed sbt
  Installed sbtn
  Installed scalafmt

Execute .bash_profile to setup Scala environment for your current Bash shell.

# source ~/.bash_profile

To verify Scala 3 installation, check the version of Scala software as follows.

# scala -version
Scala code runner version 3.2.2 -- Copyright 2002-2023, LAMP/EPFL

Create HelloWorld in Scala Language:

By using vim text editor, create a Scala source file.

# vi HelloWorld.scala

Add following code in this file.

object HelloWorld {
    def main(args: Array[String]) = {
        println("HelloWorld!")
    }
}

By using scalac command compile your Scala source file into Java byte code.

# scalac HelloWorld.scala

After successful compilation, you can execute your HelloWorld program.

# scala HelloWorld
HelloWorld!

Conclusion – Install Scala on Linux 9:

In this Linux tutorial, you have learned how to install Scala on Linux 9 or other Red Hat based Linux distributions.