Provisioning a VM with Scala and SBT

So I’ve been giving a little more time and attention to both Scala and Vagrant.  Vagrant is a nice way to manage development environments via the use of VM’s and Scala, of course, is a great hybrid of OO and FP.  One of the things I’ve done is to start working on a provisioning script for Vagrant so I can always get myself a development environment with Scala without much trouble.  So here’s the steps I follow.

One time steps:

1.) Get Vagrant

2.) Get Virtual Box (or some other VM management tool).

3.) Install both of those packages on your development machine.

4.) Create a new development directory on your dev machine.

5.) Run vagrant init <boxname> <box definition> in the development directory. You can find more detail on this command here and you can find a great list of base box definitions here.

6.) Edit the vagrantfile installed in the development directory to add this line:

config.vm.provision “shell”, path:”provision.sh”

7.) Add the following provision.sh file in the same directory as the vagrantfile.


#!/usr/bin/env bash
# Onorio Catenacci 3 January 2014

echo "Installing scala and setting it up . . . "

apt-get update > /dev/null 2>&1
apt-get install -y scala > /dev/null 2>&1

# Thanks to pardigmatic on StackOverflow for this procedure to install
# SBT via apt-get on Ubuntu
# See here: http://stackoverflow.com/questions/13711395/install-sbt-on-ubuntu

echo "Installing SBT . . ."
debfile="repo-deb-build-0002.deb"
wget http://apt.typesafe.com/"$debfile" > /dev/null 2>&1
dpkg -i "$debfile" > /dev/null 2>&1
apt-get update > /dev/null 2>&1
apt-get install -y sbt > /dev/null 2>&1

basevim="/home/vagrant/.vim"

echo "Setting up VIM for syntax highlighting"
#Create directories for vim syntax highlighting then fetch the files from github
#Credit where credit's due, one liner is from blog posting by Bruce Snyder
#http://bsnyderblog.blogspot.com/2012/12/vim-syntax-highlighting-for-scala-bash.html

mkdir -p "$basevim"/{ftdetect,indent,syntax} && for d in ftdetect indent syntax ; do wget --no-check-certificate -O "$basevim"/$d/scala.vim https://raw.github.com/scala/scala-dist/master/tool-support/src/vim/$d/scala.vim; done > /dev/null 2>&1

rm -f "$debfile"

8.) Start the vm via the vagrant up –provision command.  This will force the provision.sh file to be run to install scala, sbt and the proper vim bindings for correct syntax highlighting of scala code.  This way everytime you initialize the vm (via the vagrant init in the development directory) scala, sbt and the proper bindings for scala syntax highlighting will be automatically installed.

9.) One more thing; you can log in to the machine with vagrant ssh.  When you do, you may want to change the amount of memory that sbt asks for in its startup.  In this particular case you should be able to run $vim /usr/bin/sbt and modify these two lines:

local mem=${1:-1536}

and

declare –r default_sbt_mem=1536

On my machine [edit: it’s an Ubuntu 13.10 VM] in particular I had to set this to 512 [edit: turns out 256 works even better] on both lines in order to get sbt to start successfully.  I can’t give you a particular number which is guaranteed to work—play around with this until you find the right value.

Leave a comment