Scala is a general-purpose programming language it supports both functional programming and Objects oriented programming paradigm. Scala is a strong static type system. Scala is an acronym for “Scalable Language”*.  Scala program runs on top of Java Virtual Machine.

Download Scala

You can download a current version of scala from https://www.scala-lang.org .

Configuring Scala

Path and Environment
For quick access, You need to add scala and scalac to your path. For example:

Environment Variable Value (example)
Linux
$SCALA_HOME /usr/local/share/scala
$PATH $PATH:$SCALA_HOME/bin
Windows
%SCALA_HOME% c:\Progra~1\Scala
%PATH% %PATH%;%SCALA_HOME%\bin

sbt is a popular build tool for Scala projects. With sbt, you don’t need to download and install Scala. Just set scalaVersion in your build definition, and sbt will retrieve that version.

Install SBT

  • Installing sbt on Mac

using homebrew

$ brew install sbt

using Macports

$ port install sbt
  • Installing sbt on Windows

We can install sbt on windows by just downloading the ZIP file and make a PATH entry for SBT bin location in the environment variable or we can Install using MSI installer.

  • Installing sbt on Linux

Ubuntu and other Debian-based distributions use DEB package to install sbt. We can also install manually by adding sbt package repository in Ubuntu build system.

echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
sudo apt-get update
sudo apt-get install sbt

Red Hat Enterprise Linux and other RPM-based distributions, use RPM package. We can also install SBT manually by using following steps

curl https://bintray.com/sbt/rpm/rpm | sudo tee /etc/yum.repos.d/bintray-sbt-rpm.repo
sudo yum install sbt

SBT Project Setup

Very first-time project setup I always prefer using command prompt rather than going with IDE generated project code. Using command prompt it helps us to understand the internals of build system like keywords used in the build file, an order of operation during build process etc.

$mkdir HelloScala
$cd HelloScala
$vim build.sbt (for windows use notepad or any text editor)

Add the following to build.sbt file. you need to add only three things for the start.
1. version on scala
scalaVersion := “2.11.7”
2. Name of the project
name := “Hello Scala”
3. Add Other libraries dependency if requires
libraryDependencies += “org.scalatest” % “scalatest_2.11” % “2.2.6”

For multiple libraries, either we can add each new libraries in a new line or we can make a sequence of libraryDependencies.

libraryDependencies ++= Seq(
“net.codingwell” %% “scala-guice” % “4.0.1”,
“net.ceedubs” %% “ficus” % “1.1.2”,
“net.debasishg” %% “redisclient” % “3.3”
)

after build.sbt file is ready, come back to project’s root folder and type sbt to initiate build session inside the project.

$sbt

after a successful run, sbt prompt will come. We can then pass commands like compile, test and run for the project.

press ctrl + d to come out of sbt console.

Now it’s time to Create your first Scala program. First, you need to create source directories for the project. Go to project root folder and type following command.

$mkdir -p src/main/scala/

mkdir with -p flag will create all parent directory recursively.

Inside scala directory create HelloScala.scala file.

vim HelloScala.scala

object HelloScala {
def main(args: Array[String]): Unit = {
println("Hello, Scala!")
}
}

Now at project root directory pass command sbt run to run the project.

[info] Running HelloScala
Hello, Scala!

You just created & ran your first scala project from scratch.

Note: sbt installation instruction and link for the package may change in future, Please check http://www.scala-sbt.org/  for updates.

Posted by:Rahul Kumar

Rahul Kumar working as a Technical lead at Bangalore, India. He has more than 5 years of experience in distributed system design with Java, Scala, Akka toolkit & Play Framework. He developed various real-time data analytics applications using Apache Hadoop, Mesos ecosystem projects, and Apache Spark. He loves to design products around big data and with high velocity streaming data. He had given a couple of talks on Apache Spark, Reactive system and Actor Model in LinuxCon North America, Cassandra summit & Apache Bigdata Summits.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s