Skip to content

Instantly share code, notes, and snippets.

@shajra
Last active August 29, 2015 14:10
Show Gist options
  • Save shajra/9e6595772eb8b6a8cf69 to your computer and use it in GitHub Desktop.
Save shajra/9e6595772eb8b6a8cf69 to your computer and use it in GitHub Desktop.
Getting started with Scala using SBT

If you don't know much about Scala, but want to get started quickly. It's by no means an guide to Scala or SBT, the build system for Scala I'm recommending for getting started. There's a learning curve to picking up both Scala and SBT. All I'd like to do is remove the barrier to entry, so you can begin your study.

I'm going to assume you've done some programming in some other language, but not necessarily work on the Java virtual machine (JVM), which Scala targets.

I'm also going to assume you have a Java Developer Kit (JDK) already installed and available on your path. You can get one from Oracle, or your operating system may have OpenJDK on it:

$ sudo apt-get install openjdk-8-jdk

Let's start in a fresh directory for a new project:

$ mkdir scala.trying_out
$ cd scala.trying_out

First, we're going to get a script from Paul Phillips's GitHub account. It's a little hairy, but it does the job of downloading SBT from an official location:

$ curl -s https://raw.githubusercontent.com/paulp/sbt-extras/master/sbt > ./sbt
$ chmod 0755 ./sbt

Although SBT is used by most Scala projects (both professionally and open source), it's also been designed to be very usable with very little configuration. So we can use it to getting started quickly without having to pick up a toy process or workflow.

Later on, you can make your SBT configuration more advanced, configuring it to pull down various SBT plugins. Some of these plugins can even help you generate configuration for IDEs like Eclipse or IntelliJ IDEA. But for now, we'll just use SBT very simply with simple text files and interactive command-line sessions (you may be surprised how productive some people are without an IDE -- SBT helps with this).

You can run SBT without any configuration file by using the -sbt-create flag:

$ ./sbt -sbt-create

You'll see the script start downloading SBT. Just so you know, all the files downloaded will end up in one of two places: ~/.sbt and ~/.ivy2 in your home directory. ~/.sbt is for SBT-specific files, and ~/.ivy2 is where dependencies are cached for all your SBT projects.

Upon running SBT, you'll be put into a prompt. Although with guidance SBT can be easy to use, it's not an intuitive build system, so typing help might not give you an idea for the basic tasks you'd like to run. Here's a breakdown of the tasks that I think are most important for someone new to Scala:

  • compile: compile Scala code for the project
  • console: jump into a console (REPL) with declared depedencies on the JVM classpath
  • reload: reload SBT configuration files
  • clean: clean out compiled artifacts
  • exit: leave SBT

Try console:

> console
[info] Updating {file:/home/shajra/tmp/scala.trying_out/}scala.trying_out...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val msg = "hello"
msg: String = hello

scala> println(msg)
hello

Notice that the version of Scala (2.10.4) may not be the latest. This is because SBT runs on an earlier version, but we can configure SBT pretty easily to use another version for our code than what SBT uses. This involves writing a small file with an ".sbt" suffix in our project directory. Most people call this file "build.sbt" Let's do that by getting out of the console:

scala>
[success] Total time: 3 s, completed Nov 29, 2014 3:01:42 PM

You can't see it, but I typed Ctrl-D to get out of Scala console ("exit" makes a system call to exit the JVM, which SBT traps and disallows). and we'll get out of SBT with a exit command so we can edit the file:

> exit
$ nano build.sbt  # please use whatever text editor you prefer

In this file, we'll put the following line:

scalaVersion := "2.11.4"

and we can now get back into SBT and when we run the console, we'll see that the requested version of Scala is downloaded and an interactive console for it started:

$ ./sbt
[info] Set current project to scala-trying_out (in build file:/home/shajra/tmp/scala.trying_out/)
> console
[info] Updating {file:/home/shajra/tmp/scala.trying_out/}scala-trying_out...
[info] Resolving org.sonatype.oss#oss-parent;7 ...
[info] downloading https://repo1.maven.org/maven2/org/scala-lang/scala-library/2.11.4/scala-library-2.11.4.jar ...

... more downloads ...

[info] Done updating.
[info] 'compiler-interface' not yet compiled for Scala 2.11.4. Compiling...
[info]   Compilation completed in 7.925 s
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala>

TODO: I'll probably extend this. . . but it's hard to figure out what the scope is of a writeup like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment