I’d recently finished Martin Odersky’s Functional Programming Principles in Scala class Coursera. I’d briefly played with OCaml for a couple of months back in 2002, when I was on the verge of dropping out of college and had so much time on my hands that I didn’t know what to do with it. Subsequently, KS introduced me to Scheme and SICP. Given these precedents, despite joining about week late, the class was a breeze and I thoroughly enjoyed it.
The class used the Typesafe Scala IDE, which is built on Eclipse. I’ve been using JetBrains PyCharm for most of my python work and when they had IntelliJ 12 Ultimate Edition on sale at a 75% discount last week, I bought it. Last night, I spent a bit of time setting up IntelliJ to work with Scala on my Retina MBP. I’m posting this as a future reference.
To start with, we need to install scala and sbt using homebrew.
brew install scala --with-docs brew install sbt
I also install drip, a JVM launcher which preloads and keeps a warm JVM instance to make things appear faster. This is optional, but highly recommended.
brew install drip
We also need to add the following lines to ~/.bash_profile.
export JAVA_HOME=$(/usr/libexec/java_home) export SCALA_HOME=/usr/local/Cellar/scala/2.9.2/libexec export JAVACMD=drip export DRIP_SHUTDOWN=30 export SBT_OPTS="-XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:PermSize=128M -XX:MaxPermSize=512M"
Finally, get JDK docs and sources from Apple. Download and install “Java for OS X 2012-006 Developer Package” from here.
Now, to configure IDEA 12. Start by installing the Scala plugin from the plugin repository and restart IDEA.
Next, start a new project in IDEA and select “Scala Module”. Enter the project name, click on the New button for Project SDK. The $JAVA_HOME directory should already be selected in the dialog which appears, just click on Choose to proceed.
In Scala settings, select the “Set Scala Home” radio button and enter “/usr/local/Cellar/scala/2.9.2/libexec” in the text box below it. It’ll show a warning about not being able to find the /doc/scala-devel-docs/api directory. We’ll set it later. Click on “Finish” to create the new project.
Now to setup the documentation directories. Select “File -> Project Structure”. Select “Libraries” under “Project Settings” in the left most pane. Select “scala-library”. The JavaDocs path would be set to “/usr/local/Cellar/scala/2.9.2/libexec/doc/scala/devel-docs/api”, which is incorrect. Remove that entry and add a new JavaDoc entry pointing to “/usr/local/Cellar/scala/2.9.2/share/doc/scala”. This is where homebrew installs scala docs if scala is installed with the “–with-docs” option. Click on “Facets” under “Project Settings” and ensure that the “Language Level” is set to “Scala 2.9″.
It’s also a good idea to ensure that the locations for Java docs are set properly. Select “SDKs” under “Platform Settings”. On the “Sourcepath” tab, ensure that there’s an entry pointing to “/Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home/src.jar!/src”. On the “Documentation Path” tab, ensure there are two entries pointing to “/Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home/docs.jar!/docs/api” and “/Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home/appledocs.jar!/appledoc/api”.
And that’s it. You could probably start by adding a Scala Worksheet to the project and try playing with the REPL. The default shortcut for inline documentation in IDEA is “Ctrl-J”.
DhilipSiva on December 26th, 2012 at 10:53 AM says:
Hi Jeethu,
Awesome. I am currently reading SICP as you advised me back in March. Thanks. And nice post
Setting up for better Scala development on Android | My programming escapades on December 27th, 2012 at 3:52 AM says:
[...] Setting up Scala with IntelliJ IDEA on Mountain Lion [...]