Linux

How to Install Java on Ubuntu 22.04

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

How to Install Java on Ubuntu 22.04. Java is one of the most popular programming languages which is widely used to built applications. It runs on all major operating systems and devices.

In this guide you are going to learn how to install multiple versions of Java (11, 17, 18) Runtime Environment (JRE), Java Development Kit (JDK) and Open JDK.

You will also learn how to switch between Java versions and also the Java Development kit versions.

Available Java versions in Ubuntu 22.04

Start by updating the packages to the latest version available.

sudo apt update
sudo apt upgrade

By default Ubuntu 22.04 repositories include two OpenJDK packages, Java Runtime Environment (JRE) and Java Development Kit (JDK).

To check the available packages you can use the following command.

sudo java -version
Output
Command 'java' not found, but can be installed with:
apt install openjdk-11-jre-headless  # version 11.0.15+10-0ubuntu0.22.04.1, or
apt install default-jre              # version 2:1.11-72build2
apt install openjdk-17-jre-headless  # version 17.0.3+7-0ubuntu0.22.04.1
apt install openjdk-18-jre-headless  # version 18~36ea-1
apt install openjdk-8-jre-headless   # version 8u312-b07-0ubuntu1
Ask your administrator to install one of them.

If you are not sure what to install you can choose the default OpenJDK (JDK 11) version.

Install Java Runtime 11

Execute the following command to install the default Java Runtime Environment (JRE), which will install the JRE from OpenJDK 11

 sudo apt install default-jre

Once the installation is completed you can verify the Java version using the following command.

 java -version 

You will receive an output similar to the one below.

openjdk version "11.0.15" 2022-04-19
OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)

Now you have installed Java 11 Runtime on your Ubuntu 22.04.

Install Java Development Kit

If need Java Development Kit to compile Java programs, you can also install Open JDK

 sudo apt install default-jdk

Once the installation is completed you can verify the Java version using the following command.

 java -version 

You will receive an output similar to the one below.

javac 11.0.15

Now you have installed Java Development Kit on your Ubuntu 22.04.

Install Java JDK 17

Execute the following command to install the default Java JDK, which will also install the JRE from OpenJDK 17.

 sudo apt install openjdk-17-jdk

Once the installation is completed you can verify the Java version using the following command.

 java -version 

You will receive an output similar to the one below.

openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment (build 17.0.3+7-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 17.0.3+7-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)

Now you have installed Java 17 Runtime on your Ubuntu 22.04.

Install Java JDK 18

Execute the following command to install the default Java JDK, which will also install the JRE from OpenJDK 18.

 sudo apt install openjdk-18-jdk

Once the installation is completed you can verify the Java version using the following command.

 java -version 

You will receive an output similar to the one below.

openjdk version "18-ea" 2022-03-22
OpenJDK Runtime Environment (build 18-ea+36-Ubuntu-1)
OpenJDK 64-Bit Server VM (build 18-ea+36-Ubuntu-1, mixed mode, sharing)

Now you have installed Java 18 Runtime on your Ubuntu 22.04.

Switch between Java (JRE) Versions

If you have multiple Java versions installed, you can change the version easily using the following command.

sudo update-alternatives --config java

This command prompts you to choose Java version.

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

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-18-openjdk-amd64/bin/java   1811      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode
  3            /usr/lib/jvm/java-18-openjdk-amd64/bin/java   1811      manual mode

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

Choose the number and press Enter.

Switch between Java (JDK) Versions

You must also change the Java development kit version, otherwise you might get into compatibility issues.

To change the javac version you can use the below command.

sudo update-alternatives --config javac

This command prompts you to choose Java Development version.

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

  Selection    Path                                          Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-18-openjdk-amd64/bin/javac   1811      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/javac   1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/javac   1711      manual mode
  3            /usr/lib/jvm/java-18-openjdk-amd64/bin/javac   1811      manual mode

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

Choose the number and press Enter.

Now you have configured the Java Development Kit version also.

Configure JAVA_HOME Environment Variable

To setup path you need to find the installation path using the update-alternatives command.

sudo update-alternatives --config java 

Here you will see the path of Java.

  • OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java
  • OpenJDK 17 is located at /usr/lib/jvm/java-17-openjdk-amd64/bin/java
  • OpenJDK 18 is located at /usr/lib/jvm/java-18-openjdk-amd64/bin/java

Now you can add this to the environment file.

 sudo nano /etc/environment

Now add the path as shown below, here shown the method to add Java 11 to the path.

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

For the changes to be applied either logout and login or execute the following command for the current session.

source /etc/environment 

To verify the environment variable of Java

echo $JAVA_HOME

You will get the installation path you just set.

/usr/lib/jvm/java-11-openjdk-amd64

That’s all.

Take your first step towards a career in Java software development with this introduction course

Conclusion

Now you have learned how to install multiple versions of Java 11, 17 or 18 and configure JAVA_HOME on Ubuntu 22.04.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.