Install and manage multiple Java versions on Linux using alternatives
Tags: java, beginners, tutorial
Published: 2019-08-18 12:19:35

On this post, I will guide you on installing Java on your development Linux machine. I decided to do this post after getting some questions on how do I manage multiple java versions in my Development environments if I use something to manage it like [Sdkman](https://sdkman.io/), which I don't, in this post I will explain why. 

Being in this industry for over 20 years I have developed software and scripts in many different languages like JavaScript, Pascal, Go, Python and others but I am mainly a passionate Java developer and I am committed to helping other colleagues to start programming in Java and help to demystify the fallacy that Java is complex or difficult to start. Java is the most used programming language for developing complex and enterprise software and it has by far the better ecosystem with it's available libraries, IDEs and tooling.

My preferred development environment is Linux so I'd rather use [Linux alternatives](https://linux.die.net/man/8/alternatives) to manage java SDK installations as it's built-in in Linux and allow you to manage not only java but any other binaries you want to manage and make accessible in your command line when using Linux.  I will guide you to the process of installing Java 11 and running your first Hello World application using it. 

The full installation process will be using the command line. So let's start, open a terminal console and `cd` to your preferred working directory. 

- Make sure to have [wget](https://www.gnu.org/software/wget/wget.html) installed.

```shell
sudo apt update && sudo apt install wget
```

- Download openjdk:

```shell
wget https://download.java.net/openjdk/jdk11/ri/openjdk-11+28_linux-x64_bin.tar.gz
```

> Can check for updated java 11 versions here: https://jdk.java.net/java-se-ri/11

- Once download finished, add permissions:

```shell
chmod +x openjdk-11+28_linux-x64_bin.tar.gz
```

- Create folder where jdk will be installed

```shell
mkdir /usr/lib/jvm/open-jdk-11
```

- Extract it to /usr/lib/jvm/open-jdk-11 folder you have just created.

```shell
tar -xzf ./openjdk-11+28_linux-x64_bin.tar.gz -C /usr/lib/jvm/open-jdk-11 --strip-components=1
```

- Update alternatives to add java, javac, jshell and jar

  - list installed version

```shell
update-alternatives --list java
```
    
  - configure java installation

```shell
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/open-jdk-11/bin/java 1
```
    
  - configure javac installation

```shell
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/open-jdk-11/bin/javac 1
```
    
  - configure jar installation

```shell
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/open-jdk-11/bin/jar 1
```
    
  - configure jshell installation

```shell
sudo update-alternatives --install /usr/bin/jshell jshell /usr/lib/jvm/open-jdk-11/bin/jshell 1
```

  - Test installation, get java version: 

```shell
java -version
```
    
  - Test installation using jshell

```shell
jshell
```
  - Type in

```shell
System.out.println("Hello World");
```
   
  - Hit `Ctrl + D` to exit.

If you have multiple versions of java installed using this same process above, you can just switch between them using alternatives, 

- Display installed versions of java

```shell
sudo update-alternatives --display java
```

- Config the version you want to use: 

```shell
sudo update-alternatives --config java
```

## Adding a new version and switching between them

Based on some comment suggestions I decided to extend the post to have a second version installed and show explicitly how to switch between them. 

Lately I've been using Azul JVMs mostly so this time I will download the latest LTS Java 17 Open JDK from [Azul](https://www.azul.com/downloads/?package=jdk) 

- Download it with curl, I am placing it in my $HOME/tmp file:

```shell
curl -L https://cdn.azul.com/zulu/bin/zulu17.32.13-ca-jdk17.0.2-linux_x64.tar.gz > ~/tmp/zulu17.32.13-ca-jdk17.0.2-linux_x64.tar.gz
```

Cd in the folder where you downloaded it.

- Create folder where jdk will be installed

```shell
sudo mkdir /usr/lib/jvm/azul-open-jdk-17
```

- Extract it to /usr/lib/jvm/azul-open-jdk-17 folder you have just created.

```shell
sudo tar -xzf ./zulu17.32.13-ca-jdk17.0.2-linux_x64.tar.gz -C /usr/lib/jvm/azul-open-jdk-17 --strip-components=1
```

- Check the config options for java

```shell
sudo update-alternatives --config java
```

You will see a list of configured java in alternatives, the new one is not there so we need to register it like we did in the beginning of this article, let's do it for java and javac, make sure to check the Selection number and add the new one to the next available, hit enter to continue with the current version before progressing, let's now register the new one we downloaded. 

![Java versions installed alternatives](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3e1efref4wyxx6kaeazk.png)
 
- configure java installation

```shell
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/azul-open-jdk-17/bin/java 2
```
> The number in the end is the priority you can choose which one fits your goals better, by default the highest priority will be picked(lower number).
    
  - Repeat for javac, check the installed javac with `sudo update-alternatives --config javac` and then configure it with next available Reference.

```shell
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/azul-open-jdk-17/bin/javac 2
```
    
  - configure jar installation

```shell
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/azul-open-jdk-17/bin/jar 2
```
    
  - configure jshell installation

```shell
sudo update-alternatives --install /usr/bin/jshell jshell /usr/lib/jvm/azul-open-jdk-17/bin/jshell 2
```

Now that you have the new JDK installed and configured you can easily switch between versions using: 

```shell
sudo update-alternatives --config java
```

Pick the number of the version you want from the Selection list. 

Type in the Reference number for the one you want to be used from the displayed list and check with `java -version` the same can be done for, javac, jshell, jar or any other tools you want to manage multiple versions using alternatives on linux. 

You can now pick the option you want from the available list. That's it, you're done and have a working local development java environment ready to go. 

If you want to quick start with creating an API in Java using Spring Boot, make sure you have git and maven installed(`sudo apt install git && sudo apt install maven`) and check out this [Spring Boot Crash Course](https://dev.to/thegroo/spring-boot-crash-course-21nm), it's quite easy and quick to follow.