SDKMAN - application to manage tools versions

SDKMAN1 is a great tool for installing multiple versions of different tools and switching between versions easily. It is safe to say that it is a must-have for any programmer.

Installation is very easy

1 curl -s "https://get.sdkman.io" | bash 
2 source "$HOME/.sdkman/bin/sdkman-init.sh"

Main commands

In this post i'll use an example with java.

sdk list

Displays a list of tools that can be installed.
At the moment, the list looks like this:

 1activemq         doctoolchain     infrastructor    ki               mvnd             spark
 2ant              flink            java             kobweb           mybatis          springboot
 3asciidoctorj     gaiden           jbake            kotlin           neo4jmigrations  sshoogr
 4ballerina        gradle           jbang            kscript          pierrot          taxi
 5bpipe            gradleprofiler   jmc              layrry           pomchecker       test
 6btrace           grails           jmeter           leiningen        quarkus          tomcat
 7concurnas        groovy           joern            maven            sbt              vertx
 8connor           groovyserv       jreleaser        mcs              scala            visualvm
 9cuba             hadoop           karaf            micronaut        scalacli         webtau
10cxf              http4k           kcctl            mulefd           schemacrawler    znai

sdk list java

Displays the versions of the java tool available for installation.
Below is an excerpt of the command result:

 1================================================================================
 2Available Java Versions for Linux 64bit
 3================================================================================
 4 Vendor        | Use | Version      | Dist    | Status     | Identifier
 5--------------------------------------------------------------------------------   
 6 Java.net      |     | 19.ea.18     | open    |            | 19.ea.18-open       
 7               |     | 19.ea.17     | open    |            | 19.ea.17-open       
 8               |     | 19.ea.16     | open    |            | 19.ea.16-open       
 9               |     | 19.ea.15     | open    |            | 19.ea.15-open       
10               |     | 19.ea.14     | open    |            | 19.ea.14-open       
11               |     | 19.ea.5.lm   | open    |            | 19.ea.5.lm-open     
12               |     | 19.ea.4.lm   | open    |            | 19.ea.4.lm-open     
13               |     | 19.ea.1.pma  | open    |            | 19.ea.1.pma-open    
14               |     | 18           | open    |            | 18-open             
15               |     | 17.0.2       | open    |            | 17.0.2-open         
16               |     | 16           | open    | local only | 16-open             
17               |     | 11.0.12      | open    | installed  | 17.0.1-oracle        
18               |     | 11.0.11      | open    |            | 11.0.11-open        
19               |     | 11.0.10      | open    |            | 11.0.10-open        
20               |     | 11.0.2       | open    |            | 11.0.2-open         
21               |     | 8.0.302      | open    |            | 8.0.302-open        
22               |     | 8.0.292      | open    | installed  | 8.0.292-open        
23               |     | 8.0.282      | open    |            | 8.0.282-open        
24               |     | 8.0.265      | open    |            | 8.0.265-open

sdk install java 17.0.1-oracle

Installing the version with the identifier 17.0.1-oracle.
After installation, you will be asked if you want to use this version as the default version on your system.
The result of the command:

 1Downloading: java 17.0.1-oracle
 2
 3In progress...
 4############################################ 100,0%
 5############################################ 100,0%
 6
 7Repackaging Java 17.0.1-oracle...
 8
 9Done repackaging...
10
11Installing: java 17.0.1-oracle
12Done installing!
13
14Do you want java 17.0.1-oracle to be set as default? (Y/n): 

sdk use java 17.0.1-oracle

Use the above command to change the version to be used in the current terminal session.
The result of the command:

1Using java version 17.0.1-oracle in this shell.

java version configuration per project

When working on multiple projects, it may happen that one project needs java 8, another java 11, and another java 7 😱.

Fortunately, there is a simple recipe to not have to remember/check what version a project is using - the sdk env command that manages the configuration of the sdkman environment.

  1. Use sdkman to set the java version appropriate for your project. In this example sdk use java 17.0.1-oracle.
  2. In the root directory of the project, run sdk env init. A .sdkmanrc file will be created containing:
  • tool name - in this case java java
  • tool version - in this case 17.0.1-oracle. Example content below:
    1java=17.0.1-oracle
    
  1. Add this file to the repository
  2. The person who will be using this repository on their computer will be able to run the sdk env command.
    This command will set the java version set in the .sdkmanrc file in the terminal.
    If a particular version of java is not installed then a message will be shown
1Stop! Candidate version is not installed.
2
3Tip: Run the following to install this version
4
5$ sdk install java 17.0.1-oracle

If the version is already installed, the following message will appear

1Using java version 17.0.1-oracle in this shell.

Automatically set java version after entering project directory

There is an even more convenient way to use the sdkman environment configuration.
We run the sdk config command, which opens the sdkman configuration file editor.
Set the sdkman_auto_env preference to true.
Then open a new terminal window or run the command source "$HOME/.sdkman/bin/sdkman-init.sh".
From now on, when entering the project directory where the .sdkmanrc file is, the java version will be set automatically.
Additionally, when you exit the project directory the java version will be rolled back to the default java version.

Conflict with application hstr

In one of the posts I mentioned a useful tool hstr.
Depending on the order in which sdkman and hstr are configured in the ~/.bashrc file, you may see the following messages with each command

1bash: PROMPT_COMMAND: line 0: syntax error near unexpected token `;'
2bash: PROMPT_COMMAND: line 0: `history -a; history -n; ;sdkman_auto_env'

To improve performance, swap the order of the configurations in the ~/.bashrc file. Below is an example

 1#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
 2export SDKMAN_DIR="/home/user/.sdkman"
 3[[ -s "/home/user/.sdkman/bin/sdkman-init.sh" ]] && source "/home/user/.sdkman/bin/sdkman-init.sh"
 4
 5# HSTR configuration - add this to ~/.bashrc
 6alias hh=hstr                    # hh to be alias for hstr
 7export HSTR_CONFIG=hicolor       # get more colors
 8shopt -s histappend              # append new history items to .bash_history
 9export HISTCONTROL=ignorespace   # leading space hides commands from history
10export HISTFILESIZE=10000        # increase history file size (default is 500)
11export HISTSIZE=${HISTFILESIZE}  # increase history size (default is 500)
12# ensure synchronization between bash memory and history file
13export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"
14# if this is interactive shell, then bind hstr to Ctrl-r (for Vi mode check doc)
15if [[ $- =~ .*i.* ]]; then bind '"\C-r": "\C-a hstr -- \C-j"'; fi
16# if this is interactive shell, then bind 'kill last command' to Ctrl-x k
17if [[ $- =~ .*i.* ]]; then bind '"\C-xk": "\C-a hstr -k \C-j"'; fi

This issue should go away in future versions, as I have put out a PR on github with a fix.


  1. https://sdkman.io/ ↩︎

Translations: