Setting up a Scala development environment based on Emacs, SBT & Ensime

In this post, I will quickly show how to get a productive Scala development environment by using Emacs as an editor, with Scala mode and Ensime as extensions, and SBT as a build tool.
The following assumes that you are running a Linux with emacs already installed and a minimal knowledge on them.

1. Installing Scala

Start by downloading the latest Scala distribution from this page. I usually opt for the tar gz version which I then extract in some directory.

Here’s a tip I use to facilitate upgrading to new versions of Scala: I extract one version’s archive in a directory (“/media/data/tools/scala/“). The extracted version’s directory is named after that version, “scala-2.8.1.final” for example”. I then create a symbolic link named “scala” pointing to the latest version:

ln -s scala-2.8.1.final scala

I then only reference the symbolic link, but never the actual directory, so that when a new version of Scala is released, I only have to change the link to point to its directory, instead of having to hunt references in different files.

I use this same trick for nearly every other tool I install manually, like maven, ant, roo, GAE, etc.

I then edit my “~/.bashrc” file to export Scala’s location and to add its bin directory to the PATH:

export SCALA_HOME=/media/data/tools/scala/scala

PATH=$PATH:${SCALA_HOME}/bin

export PATH

Re-open a shell (so that “.bashrc” is re-read) and type “scala” to check that Scala was correctly installed.

2. Installing SBT

Head to SBT’s downloads page and get the latest version of SBT’s jar file.
Place it somewhere in you local drive and create a shell file next to it named “sbt”:

#!/bin/sh
java -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@"

Make this file executable :

chmod +x sbt

And add its location to the PATH by editing “.bashrc” and adding this line before the export PATH clause:

PATH=$PATH:/media/data/tools/sbt

Re-open a shell (so that “.bashrc” is re-read) and type “sbt” to check that SBT was correctly installed. You should get this message:

Project does not exist, create new project? (y/N/s)

For the purpose of this tutorial, we are going to create a Scala project. Create a directory for this project and run SBT in it:

djo@j-desktop:~$ mkdir sbt-project
djo@j-desktop:~$ cd sbt-project/
djo@j-desktop:~/sbt-project$ sbt
Project does not exist, create new project? (y/N/s) y
Name: hello
Organization: none
Version [1.0]: 
Scala version [2.7.7]: 2.8.1

And then, in the generated “src/main/scala” directory, create a “Main.scala” file:

object Main {
  def greet(name : String) = "Hello "+name

  def main(args : Array[String]) : Unit = { 
    println(greet("World"))    
  }
}

Type “run” in the SBT shell to execute the program, which should just print “Hello World“.

3. Installing Ensime

Ensime is an Emacs extension that augments Scala editing with live error checking, auto-completion, inspection and a couple of refactorings.
Ensime requires Scala mode to be already installed in Emacs.

Scala mode can be downloaded from Scala’s SVN repository by executing this command which will download and place Scala mode’s files in a “scala-emacs” dir:

svn export http://lampsvn.epfl.ch/svn-repos/scala/scala-tool-support/trunk/src/emacs scala-emacs

I suggest that you do a checkout instead of an export so that you could check for updates with “svn update”.

Edit your .emacs file to add the following:

(add-to-list 'load-path "~/.emacs.d/site-lisp/scala-emacs")
(require 'scala-mode-auto)

(add-hook 'scala-mode-hook
            '(lambda ()
	       (scala-mode-feature-electric-mode)
               ))

Don’t forget to edit the first line to point to where you placed Scala mode’s files.

Now back to Ensime: download the latest version from the project’s web page and extract it somewhere. Edit your .emacs to add the following:

(require 'scala-mode)
(add-to-list 'auto-mode-alist '("\\.scala$" . scala-mode))
(add-to-list 'load-path "~/.emacs.d/site-lisp/ensime/elisp/")
(require 'ensime)
(add-hook 'scala-mode-hook 'ensime-scala-mode-hook)

Don’t forget to edit the third line to point to where you extracted ensime (you must point to the elisp subdir).

Also, to be able to launch the Scala REPL and the SBT shell from within emacs, add the following to you .emacs file (edit the locations accordingly):

(push "/media/data/tools/scala/scala/bin/" exec-path)
(push "/media/data/tools/sbt/" exec-path)

I’m not happy with this, because this stuff is already delcared in my “.bashrc” file. Anybody has a better solution ?

Voilà. Restart Emacs and open the Main.scala file.

4. Enabling Ensime for a project

while having the “Main.scala” file open in Emacs, press M-X and enter “ensime-config-gen” and RET. Ensime will prompt you to provide the project root, which is 3 levels up from the scala sources directory. Edit the lcoation from “~/sbt-project/src/main/scala/” to “~/sbt-project/” and hit RET.
It’ll then ask if the project is of type SBT. Answer yes.
Finally, input the project’s main package’s name. In this case, it is the default package, so I hit RET without inputting anything.

What preceded is needed only once per project. Afterwards, you only have to start Ensime by executing the command “ensime” (M-X then “ensime“). This will prompt you to locate the ensime configuration file. Usually, Ensime is smart enough to correctly guess this location, but if it doesn’t, point to the project’s root and hit RET.

5. Using Ensime

Quickly, try the following:

  • Edit Main.scala to type some garbage and save. The buffer should highlight the errors
  • While the Main.scala buffer is active, hit C-c C-v s. This should open SBT in another buffer
  • place the point on the greet method and hit C-c C-r r, which will launch the rename refactoring.
  • etc.

Sample

For more information on what Ensime can do, please refer to its manual.

About these ads

10 Responses to Setting up a Scala development environment based on Emacs, SBT & Ensime

  1. Pingback: 三分热度

  2. Pingback: links for 2011-02-20 « edansys

  3. Pingback: Scala 개발 환경 설정 « 힘껏 차라

  4. ferdy says:

    Thank you for the guide, I’ve set it all up, but when I launch ensime I get the following message in my minibuffer:

    Searching for program: no such file or directory, bin/server

    What could be going wrong?

    • jawher says:

      Many things:
      - Check if the bin/server in the ensime directlry is executable
      - Check that you specified tohe correct path to the ensime dir in the emacs config.

  5. Shouldn’t this be …

    #!/bin/sh
    java -Xmx512M -jar `dirname $0`/sbt-launch-.jar “$@”

    • First time the VERSION disappeared …

      #!/bin/sh
      java -Xmx512M -jar `dirname $0`/sbt-launch-VERSION.jar “$@”

      • jawher says:

        It should be set to match the sbt jar name. If I remember correctly, sbt downloads used to be named sbt-launch.jar without the version in it, but that’s besides the point :)

  6. ae589 says:

    Isn’t there an extra step between 3 and 4 to install the ensime-sbt-plugin?

  7. fugees says:

    I am using emacs + ensime in window7, there are some errors occured . the link of the question in stack overflow http://t.co/dzmLo9o9 .Please give me some help

Leave a Reply

Please log in using one of these methods to post your comment:

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: