(Last Updated On: March 23, 2019)

How do I install Scala on RHEL / CentOS 8?. Scala is an object-oriented and functional programming language designed to be extremely concise and logical. Scala Programming is based on Java and powers popular applications, such as Spark. If you understand Java syntax, it should be easy to master Scala.

Install Scala on RHEL / CentOS 8 from AppStream repository

Scala for RHEL/CentOS is distributed through AppStream repository.

$ sudo yum module list | grep scala
scala 2.10 [d] default [d] A hybrid functional/object-oriented language for the JVM

Install Scala by running the command below.

$ sudo yum install @scala

Verify Scala installation by checking version.

$ scala -version
Scala code runner version 2.10.6 -- Copyright 2002-2013, LAMP/EPFL

Install Scala from RPM package

Here are the few steps required to have Scala installed on RHEL / CentOS 8 from an RPM package.

Step 1: Install Java

The first requirement is Java. Ensure you have Java installed on your RHEL 8 / CentOS 8 machine. The following guide should be helpful.

How to Install Java 11 (OpenJDK 11) on RHEL 8

Validate installation of Java runtime using the command:

$ java -version
openjdk version "11-ea" 2018-09-25
OpenJDK Runtime Environment (build 11-ea+28)
OpenJDK 64-Bit Server VM (build 11-ea+28, mixed mode, sharing)

Step 2: Download and install Scala

After Java is installed, proceed to download Scala RPM package and install it on your RHEL 8 machine. First, check for the latest release of Scale on the Downloads page.

export VER="2.12.8"
wget https://downloads.lightbend.com/scala/$VER/scala-$VER.rpm

Replace 2.12.8 with the recent one available on the downloads section.

Install Downloaded package with dnf command.

$ sudo dnf install scala-$VER.rpm
Updating Subscription Management repositories.
Updating Subscription Management repositories.
Last metadata expiration check: 0:19:41 ago on Thu 14 Mar 2019 08:12:19 PM EAT.
Dependencies resolved.
Package Arch Version Repository Size
Installing:
scala noarch 2.12.8-1 @commandline 124 M
Transaction Summary
Install 1 Package
Total size: 124 M
Installed size: 595 M
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installed: scala-2.12.8-1.noarch
Running scriptlet: scala-2.12.8-1.noarch 1/1
Installing : scala-2.12.8-1.noarch 1/1
Running scriptlet: scala-2.12.8-1.noarch 1/1
Installed: scala-2.12.8-1.noarch
Verifying : scala-2.12.8-1.noarch 1/1
Installed:
scala-2.12.8-1.noarch
Complete!

Verify Scala installation by checking version.

scala -version

Test Scala Installation

Try to print “Hello World” in Scala.

$ scala
Welcome to Scala 2.12.8 (OpenJDK 64-Bit Server VM, Java 11-ea).
Type in expressions for evaluation. Or try :help.
scala> println("Hello Scala World")
Hello Scala World
scala> :q

:q is used to quit from Scala shell.

Running Scala as shell script

You can run Scala program as a shell script . Save file below to scala.sh.

 #!/bin/sh
  exec scala "$0" "[email protected]"
  !#
  object HelloWorld {
    def main(args: Array[String]) {
      println("Hello, Scala World! " + args.toList)
    }
  }
  HelloWorld.main(args)

Run the script like below.

sh scala.sh

Compiling Scala Code

The scalac command is used to compile Scala source file and generate Java bytecode which can be executed on any standard JVM. See example below.

$ cat HelloWorld.scala 
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}

Compile it.

$ scalac HelloWorld.scala

By default scalac generates the class files into the current working directory but you can specify a different output directory using the -d option.

Use scala command to execute the generated bytecode.

$ scala HelloWorld
Hello, world!

That’s all. Enjoy doing Scala Development on RHEL 8 and refer to the Official Documentation for more information.

More reading:

How to Install Zabbix Server on RHEL 8 / CentOS 8

Install and Configure phpMyAdmin on RHEL 8 / CentOS 8

How to Install Apache with mod_ssl & mod_http2 on RHEL 8