Today we will learn how to download Scala and then install Scala on Linux, Unix and Windows OS. Scala runs on top of JVM, so you should have JDK 1.6 or higher version installed already to proceed with Scala installation. Since you are here to learn Scala, I am assuming that you know how to install Java on your system.
Download and Install Scala on Linux systems
Scala installation on Linux, Ubuntu, Mac OS X or any Unix based system is same, so below steps should work for any *nix system.
- Verify the JDK installation on your machine. Open the shell/terminal and type
java -version
andjavac -version
.$ java -version java version "1.8.0_20" Java(TM) SE Runtime Environment (build 1.8.0_20-b26) Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode) $ javac -version javac 1.8.0_20
- Download Scala Binaries from https://www.scala-lang.org/download/. As of writing this post Scala version is 2.11.6, so you should be getting downloaded file as scala-2.11.6.tgz. Unzip the scala-2.11.6.tgz file using the following command as shown below.
$ tar -xvzf scala-2.11.6.tgz scala-2.11.6/ scala-2.11.6/man/ scala-2.11.6/man/man1/ scala-2.11.6/man/man1/scala.1 scala-2.11.6/man/man1/scalap.1 scala-2.11.6/man/man1/fsc.1 scala-2.11.6/man/man1/scaladoc.1 scala-2.11.6/man/man1/scalac.1 scala-2.11.6/bin/ scala-2.11.6/bin/scalac ...
After unzipping, change the path to point to the directory using
cd
command as shown below.For instance my directory is Downloads in which Scala binaries are unzipped.
$ cd ~/Downloads [[email protected] Downloads]$
Now we are in the downloads directory where Scala binaries are present. Just go to the bin directory.
[[email protected] Downloads]$ cd scala-2.11.6 [[email protected] scala-2.11.6]$cd bin
Now enter the scala shell as shown below.
[[email protected] bin]$ ./scala Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_20). Type in expressions to have them evaluated. Type :help for more information. scala>
This is the Scala REPL shell in which we can type programs and see the outcome right in the shell.
Download and install Scala on Windows
- Verify the JDK installation on your windows machine by typing the following commands in the command prompt.
Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved. C:UsersPankaj>java -version java version "1.8.0_31" Java(TM) SE Runtime Environment (build 1.8.0_31-b13) Java HotSpot(TM) Client VM (build 25.31-b07, mixed mode, sharing) C:UsersPankaj>javac -version javac 1.8.0_20 C:UsersPankaj>
- Download Scala binaries from https://www.scala-lang.org/download/. The Scala installer file will be downloaded with .msi extension.Double Click or Open scala-2.11.6.msi file and select Run. The Setup Wizard appears, click on Next and complete the installation process. Scala installer will set the Path environment variable too, so that you can run it from anywhere.
- Open the command prompt and navigate to the bin directory of the installed scala by typing cd command as shown below.
C:Program Files (x86)>cd scala C:Program Files (x86)scala>cd bin C:Program Files (x86)scalabin>scala.bat Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_3 1). Type in expressions to have them evaluated. Type :help for more information. scala>
This is the scala shell in which we can type the programs and see the output in the shell itself. We can check Scala version using below command.
C:UsersPankaj>scala -version Scala code runner version 2.11.6 -- Copyright 2002-2013, LAMP/EPFL
Scala Hello World Example
Let us now see how to write and execute a program in the shell. Below is a simple class Student
that we can define in the Scala shell.
class Student() {
var id:Int = 0
var age:Int = 0
def studentDetails(i:Int,a:Int) {
id = i
age = a
println("Student Id is :"+id);
println("Student Age is :"+age);
}
}
Output: defined class Student
Below image shows how it will look in the Scala shell.
Here we create a Student
class and print the student details in the studentDetails
method by passing student id and age as parameter. If there are no errors in the code then a message “defined class Student” is displayed.
Create the student object and invoke the studdetails
method by passing the student id and age.
object Stud {
def main(args:Array[String]) {
val stu = new Student();
stu.studentDetails(10,8);
}
}
Returns: defined object Stud
Now run the code by typing Stud.main(null) and you will see below output as shown in the image.
That’s all for now for download and install scala on different operating systems, we will look into more Scala features in coming posts.