setup intellij idea on linux

[Some steps in this post are generic and apply to other similar applications.]

Intellij Idea is a smart Java & Scala IDE, and is faster too compared to heavyweight champions (read Eclipse). The community edition download is portable/ self-contained, and just needs to be extracted. However, some settings especially regarding Java needs to be done on linux distributions that don’t come with Oracle Java, e.g. Linux Mint.

1. Download the community edition from here and extract at a convenient location (say /home/user/intelliJ_idea). Lets call this location IDEA_ROOT.

2. The IDE recommends Oracle Java rather than OpenJDK that ships with Linux Mint, so download Java from here and extract in a permanent location (say /home/user/jdk1.7.0/). A similar situation is if we have several Java versions in our system and want to use a specific one.

3. IntelliJ IDEA is run through $IDEA_ROOT/bin/idea.sh. In this shell script, IntelliJ searches for Java from 3-4 alternatives. One of them is $JDK_HOME, which can be set to point to the JDK of our choice. To set JDK_HOME, open ~/.profile and add the following at the end:

JDK_HOME=$HOME/apps/jdk1.7.0/
export JDK_HOME

where $HOME is the user’s home directory, i.e. /home/user. Save the file and run the following from terminal to make it effective without a re-login:

$ source ~/.profile

Note that if you have set other environment variables in the past using this process, you may want to comment the corresponding ‘set’ and ‘export’ commands in .profile to avoid setting them again, or making the changes twice.

4. Now IntelliJ IDEA can be run from terminal with

$ $IDEA_ROOT/bin/idea.sh
(Replace $IDEA_ROOT with the exact location if it is not set in the environment like $JDK_HOME).

5. While we are at it, lets also make our new JDK version as default. If we do a

$ which java

(even idea.sh runs it to get the default java in absence of other alternatives), then it returns

/usr/bin/java

This java points to OpenJDK by default in a fresh OS installation. It can be changed to our favorite java version by running the following on any Debian system:

$ sudo update-alternatives --install /usr/bin/java java $JDK_HOME/bin/java 2
$ sudo update-alternatives --config java

update-alternatives

Henceforth /usr/bin/java will come from the version we select above.

If Java did not come with the OS, and Oracle Java is the only JDK extracted on the machine, then thisĀ stepĀ is not required, since we have only one JDK (i.e. no alternative).

6. The above step is crucial if we want to run the IDE through a desktop shortcut (or more specifically for Gnome 3, Launcher in Applications Overview) that uses system settings for Java. Setting JDK_HOME in .profile suffices only if the IDE is run from a shell that uses the .profile. But for system wide reference, we need to change the system default. Ref. an earlier post for creating such a launcher:

[Desktop Entry]
Version=1.0
Name=IntelliJ Idea
Comment=IntelliJ Idea 12 Community Ed.
Comment[en]=Java and Scala IDE
GenericName=IntelliJ Idea
Exec=/home/user/apps/intellij-129.1359/bin/idea.sh
Icon=/home/user/apps/intellij-129.1359/bin/idea.png
StartupNotify=true
MimeType=text/x-utility;
Terminal=true
Type=Application
Categories=Programming;Other

Setting Terminal=true can log valuable information conveniently in a dedicated terminal when the application is running.

Of course, we could also have created a script that calls idea.sh and sets our JDK_HOME in a fresh terminal session, then create a desktop shortcut without the need to alter system java default (for the sake of other applications). Whatever works for one!

Now we can simply open IntelliJ Idea from Applications Overview, which certainly is quicker than old school opening-a-terminal-and-typing-the-Idea-starter-script-command.

7. [Scala]
a. Download the JetBrains Scala plugin separately (Plugins > Install JetBrains Plugin > Scala). Also, download sbt and Scala Compiler, and extract each in a convenient location (say SBT_HOME and SCALA_HOME). An environment variable SCALA_HOME may be set system-wide (similar to JDK_HOME) for general purpose, but in IntelliJ we can set it as required within individual projects.
b. Add $SBT_HOME\bin and $SCALA_HOME\bin to PATH and make it effective immediately (similar to the step for JDK_HOME). Replace the variables with exact path if they are not set.
c. Open IntelliJ Idea and create a new project. Select “Scala Module” on the right. Select JDK 1.7 for “Project SDK” and set Scala Home (select the SCALA_HOME location in the file explorer). If there is a warning about Scala Docs, it can be ignored (it is better to refer the latest API Docs online rather than having a local dated copy).
d. To quickly test whether Scala is working with library references:
i. Create a new Scala Object (expand Project > right-click on “src” folder > Scala Class > enter Scala Object Name and select “Object” from the choice)
ii. IntelliJ creates a scaffold as per template for Scala object. Modify it for a simple Hello World:

object MyFirstScalaObject extends App {
println("Hello, World!")
}

If we click next to App above and hit Ctrl+Space, we should see a suggestion for App specific to Scala. This asserts that Scala libraries are picked up by the IDE.

8. [Scala] Import vs Create Project
It is recommended to first do the setup and then create the first project, rather than changing configuration for existing project which is more complex than such a simple task should be.

A quick and dirty way of importing projects is to actually ‘create’ them:
a. [Optional] In case you want to re-import an already existing project because certain configuration files have gone corrupt, open /home/user/.IdeaIC12/config/options/other.xml in some text editor and remove the recent projects entry for that project. The entry can be found in

component name="RecentProjectsManager" > option name="recentPaths" > list > option value="$USER_HOME$/path/to/project-to-be-removed"

Delete the entire line containing option value. If the corrupt project was the last you were working on, then remove the entry for option name = “lastPath” below it, which would contain the path as its option value. Save the options.xml file.

b. Go to the location of the project (say PROJECT_ROOT) to be imported, and delete all subfolders except ‘src’. Other than source code, we don’t need anything else as IntelliJ can re-create them for us. In any situation, the folders/ files we want to delete would be ‘yourProjectName.iml’ and ‘.idea’.

c. Open IntelliJ and select ‘Create Project’ from the wizard. Select ‘Scala’ module, then select the same PROJECT_ROOT as the project location using the browse button. IntelliJ will automatically populate project name with the PROJECT_ROOT folder name. Expand ‘More Settings’ to see that even module name is populated with it. If that is not the correct module name, specify one there. Scala Home would be automatically set if you had created some project using wizard earlier in IntelliJ, else see step 7.

d. Confirm the settings and IntelliJ will re-create all project files that are needed.

This is a faster way rather than babbling with the module/ facets/ library settings in project structure, assuming that there are no custom project settings that IntelliJ cannot create through its wizard.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s