How do I install Scala on RHEL 8 / 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 scala
Last metadata expiration check: 3:59:21 ago on Thu 16 Sep 2021 07:15:17 PM UTC.
Rocky Linux 8 - AppStream
Name Stream Profiles Summary
scala 2.10 [d] common [d] A hybrid functional/object-oriented language for the JVM
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
Install Scala by running the command below.
sudo yum install @scala
Accept installation prompts:
Dependencies resolved.
======================================================================================================================================================================================================
Package Architecture Version Repository Size
======================================================================================================================================================================================================
Installing group/module packages:
scala noarch 2.10.6-14.module+el8.3.0+241+f23502a8 appstream 22 M
Installing dependencies:
hawtjni-runtime noarch 1.16-2.module+el8.3.0+133+b8b54b58 appstream 42 k
jansi noarch 1.17.1-1.module+el8.3.0+241+f23502a8 appstream 78 k
jansi-native x86_64 1.7-7.module+el8.3.0+133+b8b54b58 appstream 74 k
java-1.8.0-openjdk-headless x86_64 1:1.8.0.302.b08-0.el8_4 appstream 34 M
javapackages-tools noarch 5.3.0-2.module+el8.3.0+125+5da1ae29 appstream 43 k
jline noarch 2.14.6-2.module+el8.3.0+241+f23502a8 appstream 156 k
Installing module profiles:
scala/common
Enabling module streams:
maven 3.5
scala 2.10
Transaction Summary
======================================================================================================================================================================================================
Install 7 Packages
Total download size: 56 M
Installed size: 143 M
Is this ok [y/N]: y
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 on CentOS 8 / RHEL 8
The first requirement is Java. Ensure you have Java installed on your RHEL 8 / CentOS 8 machine. The following guide should be helpful.
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: Install Scala on CentOS 8 / RHEL 8
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.13.6"
wget https://downloads.lightbend.com/scala/$VER/scala-$VER.rpm
Replace 2.13.1
with the recent one available on the downloads section.
Install Downloaded package with dnf
command.
$ sudo dnf install scala-$VER.rpm
Last metadata expiration check: 4:04:31 ago on Thu 16 Sep 2021 07:15:17 PM UTC.
Dependencies resolved.
======================================================================================================================================================================================================
Package Architecture Version Repository Size
======================================================================================================================================================================================================
Upgrading:
scala noarch 2.13.6-1 @commandline 132 M
Transaction Summary
======================================================================================================================================================================================================
Upgrade 1 Package
Total size: 132 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
Running scriptlet: scala-2.13.6-1.noarch 1/2
Upgrading : scala-2.13.6-1.noarch 1/2
Cleanup : scala-2.10.6-14.module+el8.3.0+241+f23502a8.noarch 2/2
Running scriptlet: scala-2.10.6-14.module+el8.3.0+241+f23502a8.noarch 2/2
Verifying : scala-2.13.6-1.noarch 1/2
Verifying : scala-2.10.6-14.module+el8.3.0+241+f23502a8.noarch 2/2
Upgraded:
scala-2.13.6-1.noarch
Complete!
Verify Scala installation by checking version.
$ scala -version
Scala code runner version 2.13.6 -- Copyright 2002-2021, LAMP/EPFL and Lightbend, Inc.
Test Scala Installation on CentOS 8 / RHEL 8
Try to print “Hello World” in Scala.
$ scala
Welcome to Scala 2.13.6 (OpenJDK 64-Bit Server VM, Java 11.0.12).
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
.
$ vim scala.sh
#!/bin/sh
exec scala "$0" "$@"
!#
object HelloWorld extends App {
println("Hello, world!")
}
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.
$ vim HelloWorld.scala
object HelloWorld {
def main(args: Array[String]): Unit = {
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: