ScalaGoodies - Atom, Ensime, Jupyter and Hydrogen

In this post I am introducing a source kit ( ScalaGoodies), for installing my current favorite selection of Scala development tools, incidentally also available for free. Here is a bird’s-eye view of what you can get:

Using my kit you should be able to add easily to your Scala project:

  • the Atom editor configured for Scala including powerful completions (courtesy of Ensime)
  • the ability to evaluate Scala code directly from Atom (thanks to Hydrogen)
  • a better sbt launcher and also a terminal in Atom for launching it
  • Jupyter, an advanced web based REPL (a.k.a. Noteebook), including the excellent jupyter-scala kernel for evaluating you Scala code.

If you are interested in this tools, read on for a description of how to install and use them quickly and easily.

Current state of Scala development tools

As Scala developer, I started to code using initially the free Scala-IDE. Later, I switched to the paid IntelliJ Idea IDE with the Scala plugin.

In the meanwhile, the ecosystem of Scala development tools improved, to the point it is now possible to have a development experience similar to those IDES, for free. The avalilable features rival, and sometimes surpass, those offered by their commercial counterpart. Indeed, this collection of tools is currently my favorite development environment.

An advantage of using multiple development tools than a monolitic IDE is you can enable the tools you need and leave out those who don’t, since each one is optimized only for one purpose. The disadvantage is putting all of them together is sometimes complicated and time expensive.

For this reason, I created a source kit to make things easier. Of course initially I wrote it just for myself, but since I believe it is useful to a wider audience, I am sharing it with everyone on GitHub: ScalaGoodies

Being a source kit, it is based on git. So, an absolute prerequisite to use this kit is you need git installed. It can be used even if your project is under version control with other VCS, but you need to install Git first.

If you are using git, your local git installation should be already fine. If you are not using git as you version control, you need to initialize executing git init from the top level directory of your project. Once initialized, you can proceed as described below.

The kit has been tested on OSX 10.10 and Windows 10, but it should work also on any “normal” Linux distro.

Prerequisites and Installation

Before starting, as I said, you need git including a bash shell. All the scripts are in bash, even for Windows. Since Windows is moving to bash too, and Git for Windows already provides a pretty good bash, there is little point in using another tool. You need also a Java Development Kit, Atom and Python, as described below.

You should start opening the bash shell. Then, change to the directory of your source project. Now, to use the whole kit, you need to execute the following commands. You can actually skip some of them, as described later here.

1
2
3
4
5
6
git remote add ScalaGoodies https://github.com/sciabarra/ScalaGoodies
git fetch --all
git merge ScalaGoodies/esbt
git merge ScalaGoodies/completion
git merge ScalaGoodies/worksheet
bin/install.sh

You do not need to install all the features though.

  1. You can skip the merge esbt if you do not want an advanced SBT
  2. you can skip the merge completion if you do not want to use the Atom editor with completion
  3. you can skip the merge worksheet if you do not want an advanced REPL (or if you are using Windws…)

Note the installation of the worksheet you ask for the root password, to install the python libraries for Jupyter.

Also note the prerequisites:

  1. to merge esbt you need to pre-install the Java Development Kit
  2. to merge completion you need to pre-install the Atom editor
  3. to merge worksheet you need to pre-install Python 2.7 and a compiler

It is actually not easy to install the worksheet on windows, as it requires a compiler and sudo. I recommend to skip the installation of the worksheet on Windows, but YMMV. I was unable to complete the installation of the worksheet on window for this reason, while on Linux and OSX is pretty easy.

Now, let’s see what we have installed.

Atom with Ensime

Ensime is a code analysis tool, intially written to provide advanced completion for Scala for the Emacs Editor. However it is a generic server that can be used with other editors, including Atom.

Atom is a powerful and customisable editor. It is actually written in Javascript and it is based on Chromium. In a sense, Atom is really a web browser turned code editor. Because it is so easy to customise, a plugin to integrate Ensime was a very natural choice and the result is pretty powerful.

To use Atom with Ensime, if you used my installer, all you need to do is to run bin/atom.sh from your project folder. When Atom shows up, the first time it will also download some dependencies:

The important thing to remember is: to get completions, you need to manually start Ensime. To do so, type the sequence control+shift+p on Windows or command+shift+P on Mac to invoke the command palette, then search for Ensime: Start and execute it.

The reason of this complicated startup lies in the idea that Atom is based on commands. Some commands can be activated with keystrokes but not all. The less frequent ones must be invoked from the command palette. Starting Ensime is not a frequent commands, you do it once per session. So there is not a direct keystroke.

Note also the first time it runs, Ensime will download some depenendencies. You are ready when you see the Ensime Connected message.

Now you are ready to use Ensime. Open a Scala file and you will enjoy a number of features available, demostrated here:

The most important features is, of course, code completion. Just start to type an expression, and the editor will give you a list of the legal completions available. Needless to say, it is a huge time saving, the feature that usually mandates the adoption of a full IDE specific for your language, instead of a generic text editor.

Another key feature for Scala is type information. Just moving the cursor over a variables will tell you the type of the variable. For Scala, this feature is invaluable, given the type inference.

In the picture you can also notice the error messages that appear as you type your code, doing a sort of “compilation” on-the-fly. Other features are available, you can check here for details.

Jupyter and Hydrogen

A very nice feature of some IDEs is the notebook. In short, it the ability to type code and evaluate it on the fly. It is very similar to the classical console available for Scala and other languages, except the notebook will keep a track of what you have typed before, and you can re-execute the code without having to type it again.

Using the installer I provided you will get actually 2 notebooks: a stand-alone one, named Jupyter, and another one embedded in Atom, named Hydrogen.

Let’s see Jupyter first. You can start it with the script bin/jupyter.sh. It will open a web browser, showing a file browser. You can create a new notebook, selecting Scala 2.10 or Scala 2.11 and you will get a web page where you can type your scala code and evaluate it.

Jupyter is very useful and I use it for all my exploratory coding.

But what I think is is a really winner feature is the ability to use Atom and evaluate the code directly from the source you are editing.

All you have to do is to highlight a snippet of code and press alt+shift+enter, and a windows will appear after the cursor showing the result.

This feature is actually provided by the same engine that powers Jupyter, and it is installed together Jupyter. The Atom plugin doing the magic is Hydrogen and it is powered by this Scala-Jupyter kernel, which in turn it uses Ammonium (a fork of the Ammonite REPL).

SBT embedded

In addition to the Atom based plugins, I included some command line tools to enhance SBT.

The esbt branch provides:

You can just use bin/sbt.sh with a number of features on the command line. However, my preferred configuration (although not mandatory) is using the options provided by bin/esbt.sh.

As you may know, sbt usually keeps many files outside of your project directory. I do not like this for a number of reasons, and I like to have everything under my project folder.

I provided the script esbt.sh to do exactly this, placing everything under the project folder. If you I use esbt.sh, it will use a locally installed sbt-launch.jar under bin, it will place the boot directory of sbt under project/boot, and the sbt repository under project/ivy2. Note also the atom configuration is placed under project/atom

Last but not least, I included in the Atom installation also the terminal plus plugin so you can run those commands from within Atom.

Conclusion

In short, ScalaGoodies should make easy to setup a modern and powerful Scala development enviroment. Since I use it, I almost gave up using IntelliJ Idea. I feel I have all I need for productive work with a more lightweight, flexible and customisable enviroment.