Scala on Raspberry - updated

2 minute read

A few years ago I have shown how to run Scala on a Raspberry Pi. That was on a Raspberry Pi 1 Model B, the oldest of the revisions, with a single-core CPU.

Hardware has moved on and so I wanted to see the mileage with newer Scala version on a Raspberry Pi 3 with quad-core CPU and more memory.

Also, a suitable Java runtime is now easily available on the Raspberry and generally, all tools have evolved.

So here are the steps:

Preparation

On the Raspberry, first check the Java version, it should be at least 1.8:

  tobias@rpi06:~ $ java -version
  java version "1.8.0_65"
  Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
  Java HotSpot(TM) Client VM (build 25.65-b01, mixed mode)

That should do, lets try.

Get SBT and let it do the job

The idea is to install sbt on the Raspberry and let it pull the rest. So we head over to the Scala website and check for the sbt download here.

We just need to execute:

  tobias@rpi06:~ $ echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
  tobias@rpi06:~ $ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
  tobias@rpi06:~ $ sudo apt-get update
  tobias@rpi06:~ $ sudo apt-get install sbt

We are almost there, but we need to tweak the Java runtime memory for the small Raspberry.

We can either change the memory (in MB) via command line as in

  sbt - mem 500 .....

or via Environment

  export SBT_OPTS="-mem 500"
  sbt ......

I used the first approach to try out different memory sizes, and then put them in the environment once I settled on one number. For me the -mem 500 worked.

Creating a new Hello world project: As described here but with the added -mem parameter:

  $ sbt new sbt/scala-seed.g8 -mem 500
  ....
  Minimum Scala build.

  name [My Something Project]: hello

  Template applied in ./hello

The first time it downloads a lot of scala jars and this will take a while, approx 15-20 minutes, but patience is ultimately rewarded.

Just enter this newly created directory and start “sbt run”

  cd hello
  sbt run -mem 500

Again, first time it will download stuff and take some time (4-5 min), but eventually it will say “hello” first time it took 250 s. The second run was much faster, but still about 30 sec for sbt just to check whether anything had changed in the project.

Conclusion

I hope this may be of help for some people out in the Internet, at least this note will serve as a reminder to myself how to get started with Scala on the Raspberry Pi.

Meanwhile, feel free to let me know what you think in the comments below, or enjoy some of my cat videos

Think cat videos are bullshit? Then try this: Cowshit is the new Bullshit

Leave a Comment