Quick Scala/SBT Setup for JetBrains Intellij

I just recently replaced Win7 with Fedora Linux on my main workstation at home, because I became a bit frustrated with Win7's TrustedInstaller anchoring itself on my disk and disallowing me as an administrator to move it or remove it. I'm of the opinion that if some other entity overrides your administrative privileges and installs filesystems and users without your permission and it fights you every step of the way if you attempt to remove them, that operating system installation is compromised. If it is the OS manufacturer doing that as part of the design, the OS is inherently compromised, and therefore unsuitable for certain purposes. So I sought to replace Win7 with something that hasn't been compromised, and can give me freedom of choice and total administrative control. : I settled on Fedora Linux after days of trying out other OSs. The machine I installed this on has my best hardware, an SSD, 8G of memory, and a fairly good ATI video card in it (a Radeon HD 6800) that feeds into a dual monitor setup. I wanted to retain this configuration, and make full use of my system's resources. I tried FreeBSD 9 & 10, PC-BSD, Ubuntu, Suse and Fedora. Only Ubuntu, Fedora, Suse and PC-BSD were able to recognize my minimally-exotic video hardware, and only Ubuntu and Fedora were able to operate with multiple displays as well. I eliminated Ubuntu because it has become crazily commericalized, with "social search" badly mixed into sane, down-to-earth tasks like looking for a file on the disk. Ubuntu has become poorly monetized, and that's a Bad Thing. Truth be told, I am a big BSD fan, and would have given anything (except my dual-monitor seetup) to play with PC-BSD, which is incredibly interesting. Sadly, it could only deal with one monitor. So Back on Earth, in the here and now, the winner is Fedora, which hit the setup mark flawlessly, as it has done in the past, even on the old Google Chromebook prototype hardware, the CR-48. With the Hard Choices out of the way, I am now using Fedora Linux on my desktop.

But there are more problems. (Would it be *nix if there were not?) I was toying with the idea of moving away from Vim6 for awhile for Scala development, but it's a bit like the Hotel California. I guess the motivating factor is I kind of feel my Scala learning is slowing down, and made even slower by almost tortuous coding episodes lasting 10+ hours in which I bounce around between documents, books, internet searches and Scala REPL looking for the "rightest" way to write Scala and which abstractions to use. I am secretly hoping an IDE will make learning a bit faster by doing API lookups or code linting or refactoring rather than doing all that manually with VIM windows and a copy of SBT auto-building in the background.

One guy I used to work with said IDEs are like training wheels on a bike. He felt you don't actually get the opportunity to learn the language you're writing if you use an IDE. As fundamentally true as that might be in many respects, an IDE can offer the advantage of making you think in the API of these Boroque languages that have small cores and humongous libraries, like C# and Java. I call them Boroque because unlike C++ or C, or even Perl, the key to programming productively isn't technique using idioms of the core language, but technique using idioms of complex and massive libraries. IDEs can theoretically help by offerering up active documentation and choices in almost any context, right in the editor. Whether this actually helps you learn or not is still an open question, but I'm concerned about my ability to produce anything of any size other than very small programs without using one. So I will investigate this. It may turn out to be a wild goose-chase.

Which brings me to the pinultimate point of this post, the IDEs themselves. Specifically which one for Fedora? I looked at two. One was a Boroque adaptation of the Eclipse platform, an Indian product called Aptana Studio. It seemed to be able to handle every conceivable language known to Man, but I never even got to the stage of plugging in Scala support, because it kept on segfaulting the JVM on one of its dozens of modules every time I tried to use it for longer than a few minutes in Linux. I tried frobbing memory settings for the JVM, and even went to the long effort of installing "the official" Sun JDK on Fedora (no trivial task) to replace the default OpenJDK. Still no change. It's worth noting there are apparently no problems with it on Win8. Very stable. Ironic, considering Eclipse is Open Source. But I decided I do not want this thing on Linux nor do I want any sort of Eclipse. (I included instructions to install a new JVM in Linux are at the bottom of the post.)

So what's left? Jetbrains Intellij Idea Community Edition. It's no coincedence this is a first-choice for a lot of Scala developers. It works well on both Linux and Windows. It's free as in beer, and can be downloaded from here:. If you're on Linux and a fix it yourself type, Aptana Studio can be found here. Aptana looks very promising. Just not quite ready for Linux yet.

Now for the meat of the setup for JetBrains, Scala and SBT. They all work together rather nicely, and SBT is used externally for builds. Still playing with how to integrate that, but it can be done. I'd like to mention these setup steps are basically a short version of what you can find from Scalacourses.com but I've put them in this post, partly for my own quick personal reference. The speaker, Mike Slynn, explains everything much better in his video. View it. There are other videos available as well from JetBrains on Scala setup, but they are done by Eastern Europeans with extremely thick accents which makes it problematic to grok. They are specialists, and can't be all things to all people. But they make good software. Unfortunately, in its all-consuming way, that counts for everything these days.

So for my interpretation of the setup instructions from Mike, let us begin:

NOTE: Scala Compiler Requires more memory than Java, so you will want to tweak it. See tweaking starter somwhere below.

  • In the bin subdir of installation, there is both a shell script and batch file to start the IDE.
  • So, start the IDE. (You need Java installed. If you don't, stop at this step and install it.)
  • Now you get some thing about plugins. It's ok to opt-out of. Just forge past it. Ignore temptation.
  • Now you're at the Opening Quick Selection Screen
  • Click Configure > Plugins
  • Select Jetbrains repository
  • Get Scala plugin
  • Select the Browse Repositories button. (3rd party repos)
  • Get the SBT plugin
  • Get the Markdown plugin

NOTE: Don't run any of this stuff in the background or you'll regret it. Just be patient and wait for each download to finish and index. Think about all the time you're saving.

  • Allow the IDE to restart

  • Goto Configure > Settings again.

Notice there are two sections of options

  1. Template Project (default for all projects)
  2. IDE settings (global defaults)

Go into Compiler Settings

  • Tick the option to compile projects in parallel
  • Tick the option to make projects automatically OR leave unchecked and do ALT-F9 to build at will. Like a pro. (On the other hand, if you are reading this, better take the first option.)

NOTE that if we go to Compiler > Scala Compiler, there is nothing to set. It's all external builds and taken care of by SBT.

  • Click OK
  • Goto Project Defaults > Project Structure > Project (in project settings section)

NOTE: No SDK selected.

  • Click New and select JDK

NOTE: You should be placed in or near your JDK directory (Unix: /usr/lib/jvm/, usually "Latest"). Once selected, make sure it's found a Java SDK and it shows in the little window.

OK, we are DONE with setup for Scala.

Now for the MEMORY TWEAKING:

  • Open a console prompt

  • run /usr/java/<java_dir>/bin/jvisualvm &

  • Open Intellij

NOTE:

  1. The Overview Panel provides arguments you gave the IDE
  2. The Monitor Panel lets you observe usage to tune these values
  3. The Permgen Panel is marginally of interest. Nothing ever really goes beyond 250 MB here. Move along folks.

It helps to open a Scala project to see the difference in load in the Monitor Panel. You care about orange area (allocated heap) and blue area (the amount of heap being used). Your IDE should stay within the limits you allocate for memory. If it doesn't, the system gets bogged down, so allocate more memory and startup the IDE again. This is a thing you just have to watch.

  • You can manually change startup optons by editing: <intellij_dir/bin/idea(64)vmoptions

NOTE: This stuff can be found in the setup screens, so it's best to do it there. But if you must edit this file, back it up or bad things will happen. Like Intellij not starting ever again. The safe way to change the base heap to 2048 MB is from the menus, so go into Configure > IDE Settings > Scala and set memory to 2048. This is your main tweaking knob.

And last but not least, to change the Theme of the IDE:

Configure> Settings > Appearance (under IDE section)

  • Select Darcula for theme, allow restart

OK, bad enough, right? But there's more BAD for Linux users, as usual. If you want to change your JVM installation to something other than OpenJDK (and I'm not sure there is a good reason to in most cases) please check out this blog which shows the BEST set of instructions I could find for installing the Sun/Oracle version of Java on your Fedora system. I ran the following subset of the commands listed on my 64-bit Fedora workstation from this blog, and it worked just fine:

(copied shamelessly from the pros, as usual)

## JDK 64-bit ##
rpm -Uvh jdk-7u45-linux-x64.rpm

## JRE 64-bit ##
rpm -Uvh jre-7u45-linux-x64.rpm

# java ##
alternatives --install /usr/bin/java java /usr/java/latest/jre/bin/java 200000

## javaws ##
alternatives --install /usr/bin/javaws javaws /usr/java/latest/jre/bin/javaws 200000

## Java Browser (Mozilla) Plugin 64-bit ##
alternatives --install /usr/lib64/mozilla/plugins/libjavaplugin.so libjavaplugin.so.x86_64 /usr/java/latest/jre/lib/amd64/libnpjp2.so 200000

## Install javac only if you installed JDK (Java Development Kit) package ##
alternatives --install /usr/bin/javac javac /usr/java/latest/bin/javac 200000
alternatives --install /usr/bin/jar jar /usr/java/latest/bin/jar 200000

## java ##
alternatives --install /usr/bin/java java /usr/java/latest/bin/java 200000

## javaws ##
alternatives --install /usr/bin/javaws javaws /usr/java/latest/bin/javaws 200000

## Java Browser (Mozilla) Plugin 64-bit ##
alternatives --install /usr/lib64/mozilla/plugins/libjavaplugin.so libjavaplugin.so.x86_64 /usr/java/latest/lib/amd64/libnpjp2.so 200000