Tuesday, April 7, 2020

How to Install Scala on CentOS 7

How to Install Scala on CentOS 7

scala

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

Step 1: Update your system


[root@monitor ~]# yum update -y

[root@monitor ~]# reboot

Step 2: Install OpenJDK


Scala requires the Java runtime version 1.6 or later.

[root@monitor ~]# yum install java-1.8.0-openjdk -y

You can verify your Java installation

[root@ip-10-0-0-219 ~]# java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
[root@ip-10-0-0-219 ~]#


Step 3 : You need to set the "JAVA_HOME" and "JRE_HOME" environment variables.


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

Step 4: Now Verify that your variable are set properly or not:


[root@monitor home]# echo $JAVA_HOME
/usr/lib/jvm/jre-1.8.0-openjdk
[root@monitor home]# echo $JRE_HOME
/usr/lib/jvm/jre
[root@monitor home]#


Step 5: Install Scala


[root@monitor ~]# wget http://downloads.lightbend.com/scala/2.11.8/scala-2.11.8.rpm
[root@monitor ~]# yum install scala-2.11.8.rpm
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Examining scala-2.11.8.rpm: scala-2.11.8-0.noarch
Marking scala-2.11.8.rpm to be installed
Resolving Dependencies

Step 6: Verify your installation:

[root@monitor ~]# scala -version
Scala code runner version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL
[root@monitor ~]#

How to Use Scala:


Run the Scala code runner and get into the Scala shell:

[root@monitor ~]# scala
Welcome to Scala 2.12.10 (OpenJDK 64-Bit Server VM, Java 1.8.0_242).
Type in expressions for evaluation. Or try :help.

scala>

Example 1: Addition of 2 numbers

scala> 3+3
res0: Int = 6

scala>

If you want to quit the Scala shell:

:q

scala> :q
[root@monitor ~]#


Example 2 : Write a code and compile it


[root@monitor ~]# vi hello.scala
[root@monitor ~]# cat hello.scala
object HelloScala {
  def main(args: Array[String]) {
    println("Hello Scala!")
  }
}
[root@monitor ~]#

Compile the source code with scalac:

scalac hello.scala

[root@monitor ~]# scalac hello.scala
[root@monitor ~]#

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

[root@monitor ~]# ls
@ hello.scala  HelloScala.class  HelloScala$.class    scala-2.11.8.rpm  scala-2.12.10.rpm
[root@monitor ~]# scala HelloScala
Hello Scala!
[root@monitor ~]#


close
Subscribe
CLOSE ADS
CLOSE ADS