How to Install Scala on CentOS 7

Estimated reading: 3 minutes 790 views

Scala is an object-oriented and functional programming language. It a popular language that has been used for developing applications, such as Spark, Akka, and Lift.

Prerequisites:

All of the instructions in this article are applicable to a non-root sudo user using CentOS 7. Thus, you need to deploy a fresh CentOS 7 server instance and create a non-root sudo user before diving in.

Step 1: Update your system

After logging in as the non-root sudo user from your SSH terminal, the first thing you need to do is to update the system:

yum update -y && reboot

Use the same user to log in again after the system reboots.

Step 2: Install OpenJDK Environment

Scala requires the Java runtime version 1.6 or later. Here, you can install the latest version of OpenJDK Runtime Environment 1.8.0 using YUM:

yum install java-1.8.0-openjdk.x86_64

You can validate the installation of Java runtime by running the following command:

java -version

This command should output something that resembles:

Besides, you need to set the “JAVA_HOME” and “JRE_HOME” environment variables.

cp /etc/profile /etc/profile_backup
echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk' | tee -a /etc/profile
echo 'export JRE_HOME=/usr/lib/jvm/jre' | tee -a /etc/profile
source /etc/profile

Now, you can print the two environment variables for review:

Step 3: Download and install Scala

Download and install the latest Scala RPM file from the Scala official website, which at the time of writing is 2.11.8:

cd ~
wget http://downloads.lightbend.com/scala/2.11.8/scala-2.11.8.rpm
yum install scala-2.11.8.rpm

Verify your installation:

scala -version

The output should resemble:

Step 4: Examples of using Scala

The Scala installation is complete. Let’s have a look at how to use it.
Run the Scala code runner and get into the Scala shell:

scala

In the Scala shell, you can calculate the result of a formula:

scala> 1+2
res0: Int = 3

or, execute a function:

scala> println("Hello Greencloud")
Hello Greencloud

If you want to quit the Scala shell:

:q

You can also use the scalac program to compile .scala source code.
Write the source code of an example program using vi:

vi hello.scala

Input the code segment below:

// Scala program to print Hello World!
object GreenCloud
{
// Main Method
def main(args: Array[String])
{
// prints Hello World
println("Hello World!")
}
}

Save and quit:

:wq

Compile the source code with scalac:

scalac hello.scala

The program will output two compiled files: HelloWorld.class and HelloWorld$.class. You can run the compiled file with scala:

scala GreenCloud

The output will read:

Moreover, you can embed Scala functions into a bash script, and then run the script using bash:

vi script.sh

Populate the file with:

#!/bin/sh
exec scala "$0" "$@"
!#
object HelloWorld extends App {
  println("Hello world!")
}
HelloWorld.main(args)

Save and quit:

:wq

Run the script in the bash shell:

sh script.sh

Again, the output will read:

That concludes our tutorial, thank you for reading.

Leave a Comment

Share this Doc

How to Install Scala on CentOS 7

Or copy link

CONTENTS