Using sdkman to simplify development

Oftentimes you find that there are different SDK requirements for different projects. A classic example would be that your Scala project might be stuck at JDK 8 level1, but Java projects themselves might be using later versions, such as JDK 11.

It would be cumbersome to switch between different JDK versions manually, having to make sure to correctly setup JAVA_HOME when you switch between projects. sdkman is there to address this problem and the same class of problems for other types of development setups as well. These include Scala, Groovy, Gradle and a lot more.

Installation and setup

Follow the instructions to install sdkman.

The installation script integrates to bash/zsh automatically. You can alternatively add the following at the end of the ~/.bashrc or ~/.zshrc for bash and zsh respectively.

export SDKMAN_DIR="$HOME/.sdkman"
[[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && source "$HOME/.sdkman/bin/sdkman-init.sh"

SDKMAN_DIR is the default location for the install and on a POSIX system it is $HOME/.sdkman.

Default location can be customised by exporting $SDKMAN_DIR variable to a custom location, as shown below,

export SDKMAN_DIR="/usr/local/sdkman" && curl -s "https://get.sdkman.io" | bash`

Integration with fish shell

Fish shell, which is my current shell, requires a third party plugin, or manual setup. I found that sdkman-for-fish works quite nicely. This plugin adds sdkman binaries to fish path and also adds autocompletion for sdkman commands.

To install for fish shell with the popular fisher2 tool run

fisher add reitzig/sdkman-for-fish

Usage

Usage is illustrated in following examples in terms of Java SDKs, but adapting the same commands for other SDKs should be quite simple given they follow the same usage patterns.

  • To see a list of all available SDK types,

    sdk list
    
  • To get a list of available Java sdk versions,

    sdk list java
    
  • To install a version of Java, after selecting a version using the above command,

    sdk install java 8.0.232.hs-adpt
    
  • To remove sdk version,

    sdk rm java 8.0.232.hs-adpt
    

    uninstall also works in place of rm.

  • How to change the default version globally,

    sdk default java 11.0.5.hs-adpt
    
  • Using a specific version for a specific shell session without changing the default globally,

    sdk use java 8.0.232.hs-adpt
    

Where to find the installed SDKs?

Candidates for a given SDK is found under the candidates directory in $SDKMAN_DIR. For example, in Java SDKs,

  • Location for JDK 11 from OpenJDK,
    $SDKMAN_DIR/candidates/java/11.0.5.hs-adpt`.
    

    Specific version locations are useful for setting up project specific JDKs in IDEs.

  • Current version is a symbolic link to one of the instlaled candidates,

    $SDKMAN_DIR/candidates/java/current
    

It is useful to set the JAVA_HOME environment variable to always point to the current version.

  • For bash/zsh shells,

    export JAVA_HOME='$SDKMAN_DIR/candidates/java/current'
    
  • For fish shell,

    set -g -x JAVA_HOME '$SDKMAN_DIR/candidates/java/current'
    

Footnotes

  1. Running vs compiling in Scala JDK compatibility

  2. fisher - A package manager for the fish shell. 

Leave a comment