How to Install Scala on CentOS 7

Share on:

Table of Contents

Scala is a strong statically typed general-purpose programming language which 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.

How to Install Scala on CentOS 7

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:

sudo yum update -y && sudo reboot

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

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:

sudo 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:

openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

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

sudo cp /etc/profile /etc/profile_backup      #Backup the profile file in order to prevent unintentional mistakes
echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk' | sudo tee -a /etc/profile
echo 'export JRE_HOME=/usr/lib/jvm/jre' | sudo tee -a /etc/profile
source /etc/profile

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

echo $JRE_HOME

Download and install Scala

Download and install the latest Scala RPM file from the Scala official website:

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

Verify your installation:

scala -version

The output should resemble:

Scala code runner version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL

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:


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 Scala")
Hello Scala

If you want to quit the Scala shell:


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:

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

Save and quit:


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 HelloWorld

The output will read:

Hello Scala!

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

vi script.sh

Populate the file with:

exec scala "$0" "$@"
object HelloWorld extends App {
  println("Hello Scala!")


Save and quit:


Run the script in the bash shell:

sh script.sh

Again, the output will read:

Hello Scala!

Lots of developers would appreciate Scala because of its unique features and its seamless interaction with java code. Hope this guide will help you.