Scala for the Impatient

Cay Horstmann

Scala Installation Instructions

You have the choice of using Scala with your own editor and command line tools, or with an integrated development environment.

I recommend that you start out with the command line tools and move to an IDE as your programs get more complex. But if you don't like the command line, you can certainly start with an IDE.

Scala uses the Java virtual machine, so you must have Java installed on your machine. (There is also a .NET version of Scala, but these instructions assume Java.) You need the JDK, not just the Java runtime. Get it from http://www.oracle.com/technetwork/java/javase/downloads/index.htm.

Command Line Tools

Download the latest stable Scala distribution from http://scala-lang.org/downloads. Extract into a convenient location, preferably one without spaces in any directory name. 

To try it out, open a terminal window and type

path/to/your/scala/bin/scala

where, of course, path/to/your/scala is the path to the directory in which you extracted the Scala distribution.

You should get a prompt similar to the following:

Welcome to Scala version 2.9.1 (Java HotSpot(TM) Server VM, Java 1.7.0).
Type in expressions to have them evaluated.
Type :help for more information.

scala> 

If so, type  in 6 * 7 and hit the Enter key. If you get 42, type :quit and Enter.

If you got an error message that scala is not a valid command, double-check your directory path.

If you get an error message that java is not a valid command, then set the JAVA_HOME environment variable to the directory containing your Java installation. For example,

set JAVA_HOME="c:\Program Files\java\jdk1.7.0"
scala-2.9.1-final\bin\scala

It is a good idea to add both the Java and Scala bin directories to the PATH environment variable. The details depend on your operating system.

API Documentation

I like to install the API documentation locally so that it is available offline. Download scala-docs-version.txz (which you can uncompress with  tar -xvfJ) or, if you don't have tar, scala-docs-version.zip from http://www.scala-lang.org/downloads.



Then point your browser to /path/to/scala-docs-version/api/index.html.

Eclipse

The Scala team maintains an Eclipse plugin at http://scala-ide.org. First install Java and Eclipse, then the plugin, following the directions on that web site. You need not install the Scala command-line tools.

You need to make two  tweaks to your Eclipse installation. First, select Scala -> Setup Diagnostics and click the “Use Recommended Default Settings” radio button. This turns on autocompletion. Next, increase the heap size to at least 1024MB. That's a bit trickier—see these instructions.

To get a REPL in Eclipse,  first make a Scala project. Then right-click on the project and select Scala -> Create Scala Interpreter.

IDEA

First, install the Scala command-line tools, as described above. Be sure to install the developer documentation with sbaz install scala-devel-docs. IDEA uses it.

Download IDEA from http://www.jetbrains.com/idea/.  You can download either the free or the commercial version. Start the program and click on ”Open Plugin Manager”, then select the Available tab. Right-click on Scala and select the option to install the plugin.

To run the REPL, make a new project, add a Scala class inside, then right-click on the class and select Run Scala  Console.

NetBeans

First, install the Scala command-line tools, as described above. Download NetBeans from http://netbeans.org. Any version will do.

Stable versions of the plugin are at http://wiki.netbeans.org/Scala, but they may lag behind the latest Scala version. Type Scala into the Search field on that page to locate experimental versions.

Download and unzip the file that contains the plugins (which have extension .nbm).

In NetBeans, select Tools -> Plugins -> Downloaded -> Add Plugins, then navigate to the directory into which you have unzipped the plugins. Select all  .nbm files and click Install.

You can't run the REPL inside NetBeans.

Emacs and Ensime

Eclipse, NetBeans, and IntelliJ are familiar products, and their Scala plugins have come a long way. Nevertheless, some users find issues with features, stability or performance. If you are among them, check out Emacs. Emacs is works quite well, provided you are willing to adapt to an ancient (but surprisingly functional) user interface.

Emacs support is not included in the Scala 2.9.2 distribution.  Run 

git clone https://github.com/scala/scala-dist.git

The downloaded code will have a directory scala-dist/scala-tool-support/src/emacs. You can remove all other directories from the download.

Add these lines to your .emacs file:

(add-to-list 'load-path "/path/to/scala-dist/scala-tool-support/src/emacs")
(require 'scala-mode-auto)

You can now edit Scala files with syntax highlighting and autoformatting. To format, select a region and hit Tab.

To run the REPL, pick Scala  -> Run Interpreter from the menu.

If you want autocompletion and refactoring, you need to install Ensime. Follow the Quick start instructions at https://github.com/aemoncannon/ensime. A handy cheat sheet of the keyboard shortcuts is here.