Scala From the Ground Up: [1] Setup

Mohammed Awad-Allah
Code & Data
Published in
4 min readMay 3, 2015

Throughout this series, we’ll explore the Scala programming language. We’ll start from Installation and the basics, passing by intermediate features and finally we’ll discover some of the advanced topics.

Part 1 will act as an introduction as well as guide you through the setup process to start using Scala

1. Check Requirements

The only requirement for install Scala is to have Java 1.6 or later. To check this requirement, open your terminal (Mac/Linux) or Cmd Prompt (Windows) and type the following command

$ java -version

You should get output similar to the one below.

$ java -version java version "1.6.0_65" Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

If instead you’ve got an error, you need to install JRE and/or add it to your path. Follow this guide for instructions.

2. Download Scala

You can get scala in one of three ways:

  1. Download and install the binaries
  2. Get TypeSafe Activator
  3. Install a Scala IDE

In this tutorial we’re going to install Scala using the first method. You can (and should) try the other two methods.

So, go ahead and download Scala binaries from their website

3. Installation

After downloading the compressed binaries, extract them. And then copy the folder to the following destination according to your platform

Mac/Linux: /usr/local/share/scalaWindows: C:\Program Files\Scala

This is not a requirement, but it’s always good to organize things ☺

In order to use Scala commands from the Terminal/Cmd without changing the directory, you’ll need to point your PATH to the installation location that we’ve just moved our files to.

4. Check our Installation

Open the terminal and type the following command

$ scala -version

If you got the following output, YAY! you did it.

Scala code runner version 2.11.4 -- Copyright 2002-2013, LAMP/EPFL

5. Hello Scala — REPL Edition

Now that we’ve successfully installed Scala, it’s time to write our first program. And What’s better than starting with the classic Hello World !

We’ll write our “Hello World” application in two ways. First, in this section, we’ll explore it using Scala Interpreter scala (aka REPL).

In the following section, we’ll write our application in a separate file and compile it using Scala compiler scalac and then we’ll run it.

The Scala Interpreter is an interactive environment where you can write your code and see the result immediately. It’s mainly used to learn features of the language or to try new ideas without much setup. To start the Scala Interpreter, type the following command in the terminal/cmd.

$ scala Welcome to Scala version 2.11.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65). Type in expressions to have them evaluated. Type :help for more information.scala>

Go ahead and write the following line

print("Hello Scala")

Simply, this line of code has only one function print which prints to the screen what’s fed to it as a parameter, in this case, the string Hello Scala.

If you tried this, you should immediately see the following output to the screen.

Hello Scala

CONGRATULATIONS! you just wrote your very first Scala program. Let’s try it in a different way.

6. Hello Scala (again) — Compiled Edition

In this section, we’ll write our code to a file, compile it and then run it. This is how code is written in production.

1. Open your favorite text editor.

2. In the folder of your choice, create a new file, named Hello.scala

3. Edit the file by adding the following code.

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

There’s not much to this code. First we create a new object named Hello and we define the main function (which is the entry point of our application). Inside the main function we only have one line println(“Hello Scala”) which prints Hello Scala to the terminal.

4. In the terminal/cmd, cd to the directory you put Hello.scala in, then type the following command

$ scalac Hello.scala

This instructs the Scala Compiler scalac to compile the file named Hello.scala. If everything goes well, you won’t see anything printed to your shell. If you see any errors printed to the screen, fix them first, then run the above command again.

5. To run the application, type the following command

$ scala Hello

This tells scala to run the compiled file (which is Hello.class). You should see the following output to the screen.

Hello Scala

In this article we didn’t cover much. But we have setup our environment and made sure that everything works well. In the next article we’ll explore features of the language using scala interpreter.

Originally published at m-medhat.tumblr.com.

--

--