SSD Grow


Learn How to Install Java and Its virtual Machine on Ubuntu 22.04

Java is a widely-used, high-level, object-oriented programming language that has become an industry standard. The Java Virtual Machine (JVM) is a vital component that allows computers to execute Java applications, as well as programs that can be compiled to Java bytecode, such as Kotlin and Scala. The use of Java and the JVM is fundamental to many DevOps tools, including popular applications such as Apache Tomcat, Cassandra, and Jenkins. In this tutorial, we will delve deeper into the significance of Java and the JVM in the context of DevOps, and examine how they are applied in widely-used software tools.
Before starting, you should have a basic understanding of the Linux command line. Additionally, you will need an Ubuntu 22.04 server, and a non-root user with sudo privileges. You can obtain affordable and powerful Ubuntu servers from our website. To learn how to access your server and create a sudo user, you can refer to our guide on How to access your server using SSH.

Updating Package Cache: Keeping Your System Up-to-Date

Start by updating the packages in the package manager cache to the latest available versions using the following command:

sudo apt update

Installing the JRE/JDK using Apt

To install the JRE from the OpenJDK version of Java, use the following command:

sudo apt install default-jre

This will install the default JRE in the Ubuntu repositories, and will allow you to execute Java programs.
Once the installation is finished, check your Java version to ensure that the JRE was successfully installed:

java -version

You should receive an output similar to the following:

openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu222.04, mixed mode, sharing)

Next, install the JDK to compile and run some Java software that requires it. To do so, use the following command:

sudo apt install default-jdk

To ensure that the JDK is successfully installed, check the version of the Java compiler javac:

javac -version

You should receive an output similar to the following:

javac 11.0.17

With this, you now have Java installed on your system with OpenJDK. If you are looking to install the Official Oracle JDK, follow the next option.

Installing Oracle JDK

If you want to install and use OracleJDK, and not OpenJDK, or switch between the two depending on your requirements, use the following instructions.
First, install some required packages:

sudo apt install -y libc6-x32 libc6-i386

Go to the Oracle downloads page and download the x64 Debian Package version using wget:



Once your .deb package is downloaded, install it using apt:

sudo apt install ./jdk-19_linux-x64_bin.deb

Once installed, use the following commands to set these new packages for update-alternatives:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-19/bin/java 100
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-19/bin/javac 100

Manage Java

If you’ve installed one or more Java versions, then you can use the update-alternatives command to set the default version for your system.
To select your default Java installation, run the following command:

sudo update-alternatives --config java

You’ll receive a list of the Java versions you currently have installed on your system:

There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
2 /usr/lib/jvm/jdk-19/bin/java 100 manual mode

Press to keep the current choice[*], or type selection number:

Press Enter to leave the current option, or type the number of the Java version you want to set.
For example, type in 2 and press Enter to set the OracleJDK version.
You can also use update-alternatives to configure the default version of your Java compiler:

sudo update-alternatives --config javac

You should receive an output similar to the following:

There are 2 choices for the alternative javac (providing /usr/bin/javac).

Selection Path Priority Status
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac 1111 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac 1111 manual mode
2 /usr/lib/jvm/jdk-19/bin/javac 100 manual mode

Again, choose 2 to use the OracleJDK compiler as your default compiler and press Enter.

To verify that Java was installed, check out its version:

java -version

You should receive an output similar to the following:

java version "19.0.1" 2022-10-18
Java(TM) SE Runtime Environment (build 19.0.1+10-21)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)

To verify that the Java compiler was installed, check out its version:

javac -version

You should receive an output similar to the following:

javac 19.0.1

Configuring the Java Home Directory Environment Variable

Now that you’ve installed Java on your Ubuntu server, you need to to set up the $JAVA_HOME environment variable, which is a variable used by many Java packages and software programs written in Java to determine where Java is installed.
First run the update-alternatives to get the path of your current Java installation:

sudo update-alternatives --config java

This will list paths of your different Java installations, which will be similar to the following:

0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
2 /usr/lib/jvm/jdk-19/bin/java 100 manual mode

Here, OpenJDK is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java and OracleJDK is located at /usr/lib/jvm/jdk-19/bin/java.
Choose and copy a path depending on your requirements, then open the /etc/environment file:

sudo nano /etc/environment

Add your path at the end of this file and assign it to a variable called JAVA_HOME, excluding the /bin/java portion, like so:


Save and close /etc/environment.

Reload /ect/environment:

source /etc/environment

Now check out the value of your Java Home variable:


This should print the path you’ve set:


You have learned how to install Java on your Ubuntu system using two different methods. The first method involves installing the OpenJDK version available in your Ubuntu repositories, while the second method involves installing the official OracleJDK. Additionally, you have become proficient in installing and switching between both versions effortlessly, making it easier to meet your changing needs.

Related Articles