Scala is a concise, high-level language focused on combining functional and object-oriented programming together with a strong static type system to help avoid bugs in complex applications. These design decisions were created as a way to help address the problems and criticisms of Java, while still utilizing JVM and Java stacks. This article is written to help you install Scala on Linux as well as Windows, and shows how to write your first simple program.

Installation

Windows:
On the command line, type java -version to verify the required version is found.

C:\Users\CoLap>java -version

Once you have the appropriate Java version, navigate to the Scala Download website. Here you will find the 2 popular ways to install Scala, along with other methods for installation. Installation and Scala Build Tool will be covered here.

Intellij IDE:
Navigate to the Jetbrains download page and select the version of the IDE to download, upon selection the download will begin immediately. Once the download has been completed, you can find an installation executable in your Downloads folder.

Run the executable. You will be shown with a windows security alert, select ‘run’. Then select ‘yes’ to allow the executable to make changes. The Intellij setup will then appear.

Click next, then select the path you wish Intellij to install at (default path recommended) and click next. Then select any of the optional functionalities you desire and or click next. Finally choose a start menu folder to contain the shortcut to Intellij (default recommended) and click install.

Once the Installation has finished, run Intellij and set up customization settings, default plugins, and finally install Scala language support plugin. If you missed it the customization settings you can, on Intellij startup, configure plugins.

And under ‘Marketplace’ tab, find the Scala plugin and install. Validate that you have installed Scala by checking under ‘Installed’ tab.

Back on the startup window, select ‘Create New Project’.

Select ‘Scala’ then ‘IDEA’ and click next.

Create a project name and select create under Scala SDK, then choose the latest version. After setting up project details, select finish. Now you are in the project view where you can see your resources in the project folder on the left.

To create a Scala file to start coding, right click ‘src’, and under ’new’ select ‘Scala Class’. Here you can place your main function and begin coding. To see more on coding, look at steps to execute a simple program below.

Installing Scala Build Tool:

Navigate to Scala SBT and select the Windows download for the SBT. Once the download has been completed, you can find an installation msi in your Downloads folder.

Run the msi. You will be shown with a windows security alert, select ‘run’. The SBT setup wizard will then appear.

Click next, read and accept the license agreement then click next. The custom setup will appear, you may make changes according to your needs (default recommended) then click next and install. You will be prompted to allow the program to make changes, select yes.

Now that the tool is set up. Create a new empty folder somewhere convenient to access and on the command line, cd to that directory.

C:\Users\CoLap> cd Documents/ScalaProgram

Next, run the command:

> sbt new scala/hello-world.g8

This pulls the ‘hello-world’ template from GitHub and create other folders which can be ignored. When prompted, name the Hello World template as ‘hello-world’. This will create the project to program in.

Next, cd into the hello-world directory and run the command:

> sbt

Now that you are in the sbt console, run the command:

> ~run

The ‘~’ allows you to actively compile and run your Scala program automatically each time the file is updated (saved). To find your scala file just look through the directory for: hello-world/src/main/scala/Main.scala

You may edit the file with your favorite text editor.

Linux:
On a terminal window type java -version.

$ java -version

Installing on Linux

We will focus on an Ubuntu installation for this tutorial. Once you have a working Java 8 JDK there are two options on how to run Scala in Linux. The simplest way is to download and install the Scala plugin for IntelliJ. On Ubuntu we recommend you use snaps to install the IDE (what are snaps?). Ubuntu 16.04 LTS and later come prepackaged with snap and you can run the following command to install IntelliJ:

$ sudo snap install intellij-idea-community --classic --edge

Keep your installation up to date by simply typing:

$ snap refresh

Launch IntelliJ and access the settings menu (CTRL + Alt + S). Under the Plugins Tab click the Install button for Scala.

We will then create a new project to finish installing the Scala SDK.
Launch IntelliJ and go to File -> New -> Project.
Choose Scala from the options on the left and then IDEA from the options on the right.

Since this is the initial launch the Scala SDK is empty. Click the Create button on the right and then the Download button. You will be able to choose your SDK level. Select the highest available.

Once this is complete you can click the Finish button and proceed to the section below to write and run your first Scala project. If you prefer to use .deb packages or the command line you can do the following to install sbt:
Download the DEB package from this location. (the current version of sbt as of this writing is 1.2.8) and run it.

You will be able to keep it up to date along with your normal packages with:

$ sudo apt-get update$ sudo apt-get upgrade

Or run the following commands on a terminal window to add the necessary repositories to your environment:

$ 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

Running a Simple Program

Intellij Project:
If you followed our steps to install Scala through an IDE here is how you can create and run a simple project.

Create a New Project on IntelliJ. Choose Scala on the left and then choose SBT on the right side.

Set your JDK version to 1.8 and ensure your SBT version is at least 0.13.13 and then Finish

This creates a structure necessary to run your project and also add test case. We want to create a package.

Expand the directories on the left to ExampleProject -> src -> main

Right click the scala folder and choose New->Package

Right click the newly created package and choose New -> Scala class

Name it Main and choose “object” as the Kind.

Your IDE structure should now look like this

Write the following sample code on the right side.

object main extends App {    println(“Welcome to Scala!”)}

Click on Run (or Alt + Shift + F10) and choose main. Your terminal window will display the following:

Congratulations. You have successfully written and executed your first Scala program with an IDE.

The Interactive Interpreter:

To start the Scala REPL, type Scala in your command line:

$ scala

This will greet you with the welcome message, version, and the Scala prompt:

Welcome to Scala 2.12.8 (Java HotSpot(TM) 64-Bit Server VM, Java 10.0.2).
Type in expressions for evaluation. Or try :help.
scala> $

From here, we can create and assign a variety of variables and expressions, all from inside the interpreter.

scala> val width = 5
width: Int = 5
scala> val height = 6
height: Int = 6
scala> val area = width * height
area: Int = 30
scala> val a = Array(10,100,1000)
a: Array[Int] = Array(10, 100, 1000)

Function objects can also be created and executed as well:

scala> object HelloWorld {
| def main(args: Array[String]): Unit = {
| println(“Hello, World!”)
| }
| }
defined object HelloWorld
scala> HelloWorld.main(Array())
Hello, World!

And lastly, the shortcut :q or :quit is used to exit the interpreter:

scala> :q

A More Traditional Compile:
For the more traditional javac command line compile approach, the scalac command will compile one or more Scala files into Java bytecode to be executed on any standard JVM.

Start by opening your favorite text editor and writing this simple Scala program from above, and save it as HelloWorld.scala:

object HelloWorld {
def main(args: Array[String]): Unit = {
println(“Hello, World!”)
}
}

Now, open the command line in the same location as our Scala file and run:

$ scalac HelloWorld.scala

This generates our class files in our current directory. To specify the directory, run the same command again but include the -d flag to specify an output directory:

$ scalac -d bin HelloWorld.scala

Now to execute our program, we run our scala command:

$ scala HelloWorld
Hello, World!

This executes the program from our compiled class files. To specify the directory, we can use the -cp flag to choose the classpath of our compiled class files.

$ scala -cp bin HelloWorld
Hello, World!

Using Scala Build Tools

A question that may arise is why use SBT in the first place, and the answer is quite simple. When writing a small program that only has a few source files, it is not difficult to compile all the source files by using the command line. However, once you start working with bigger projects that can have hundreds of source files, it would take too much time and effort to compile manually. Instead, a build tool would be able to compile all of these files for you.

There are many advantages to using SBT. For example, because it was written in Scala, the build tool has the whole language for support when compiling code. On top of that, you can even use it for projects that require both Java and Scala. It’s model is built on an update-on-request principle that allows it to be more nimble and focused on applications. Additionally, SBT has helpful features that improve efficiency, such as incremental tests/compilation and library correction. Incremental testing and compiling allows developers to focus on the changes that they need to commit, but more importantly, it allows the vast majority of the unchanged code to remain unaffected. Library correction helps the user download and correct the necessary libraries from within the project. It also works with other important software programs. For example, the dependency management parts of its application rely on Apache Ivy, also allowing it to support Maven repositories.

In order to correctly use the SBT, the user will need to create a build.sbt file that includes information on how the project will operate. Some relevant information to include would be a list of all the source files, the project name, and the versions of SBT and Scala being used. It should also include the tasks, settings, and managed dependencies needed in the project. Once all of the necessary information is included, the SBT will read the build.sbt file and compile the project.

Overall we have gone over what SBT is, why to use it specifically, and how it works. All of this is relevant information if one decides to use Scala as the programming language of their choice for their project.

Authors:

Dan Martinez @DanielMartinez

Abdul Asif @AbdulAsif

Colby Kuhnel

Huy Nguyen @HuyNguyen

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store