How to Install Java on Ubuntu 22.04
6 min read
Java is a popular programming language and computing platform used for building applications and software solutions. Java runs on all major operating systems and devices. You can find applications developed in Java on your laptop, phone, and game console.
This guide describes how to install various versions of Java on Ubuntu 22.04.
Before You Begin
There are several different implementations of Java. OpenJDK and Oracle JDK are the two main implementations of Java, with little or no differences between them and are functionally almost the same. The main difference is that OpenJDK is an open-source project while Oracle JDK requires a paid license or permits only non-commercial use of the software, such as development or personal use. Also, Oracle JDK has a few additional commercial features that are unavailable with OpenJDK.
The default Ubuntu 22.04 repositories include two OpenJDK packages, Java Runtime Environment (JRE) and Java Development Kit (JDK). The JRE consists of the Java virtual machine (JVM), classes, and binaries that allow you to run Java programs. The JDK includes the JRE and development/debugging tools and libraries necessary to build Java applications.
If you’re unsure which Java package to install, the general recommendation is to install to the latest or the default OpenJDK version. However, some Java applications may require a specific version, so it is best to consult the application documentation.
Installing OpenJDK in Ubuntu
Before installing Java, you can check if it is already installed on your system by typing:
If Java is not installed on your system, the output of the command above will tell you that the command
java is not found. Otherwise, it will print the installed Java version.
When installing new packages, you should always first refresh the local packages index by running the following commands as a user with sudo privileges or root:
sudo apt update
At the time of writing, there are four long-term supported (LTS) versions of Java: 8, 11, 17, and 21. The default Java development and runtime in Ubuntu 22.04 is set to Java 17, meaning you will get Java 17 installed on your system if you install the
Execute the following command to install OpenJDK version 17:
sudo apt install openjdk-17-jdk
Once the installation is complete, you can verify it by checking the Java version:
The output should look something like this:
openjdk version "184.108.40.206" 2023-08-24 OpenJDK Runtime Environment (build 220.127.116.11+1-Ubuntu-0ubuntu122.04) OpenJDK 64-Bit Server VM (build 18.104.22.168+1-Ubuntu-0ubuntu122.04, mixed mode, sharing)
That’s it! You have successfully installed Java on your Ubuntu machine.
JRE is included in the JDK package. If you need only JRE, install the
openjdk-17-jre package. For minimal Java runtime, install the
Installing Oracle Java in Ubuntu
The Oracle JDK packages are not available in the default Ubuntu repositories. If, for some reason, your application requires Oracle Java, you can install Oracle Java either by downloading the file from the Oracle website or by using a third-party repository such as Linux Uprising PPA. In this example, we’ll download the Debian package from the Oracle website.
Visit the Oracle Java Archive site and open the download page of the Java version you need.
We’ll download and install version 21.
Click the Linux x64 Debian Package from the file list to download the
If you’re installing Java on a server, use the
to download the file. Right-click on the link to copy the address and paste it after
Once the file is downloaded, execute the following command to install Oracle JDK on your system:
sudo apt install ./jdk-21.0.1_linux-x64_bin.deb
Replace the filename in the command above if you downloaded another Java version.
Setting the Default Java Version
The default version of Java is used by default when debugging, building, or running Java-based applications. If you have multiple Java versions installed on your Ubuntu system, you can check which version is set as the default one by typing:
To change the default version, use the
sudo update-alternatives --config java
You will be presented with a list of all installed Java versions. For example, if you have OpenJDK 11 and 17 an Oracle JDK 21 installed, the output will look something like the below:
There are 3 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/jdk-21-oracle-x64/bin/java 352329728 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/jdk-21-oracle-x64/bin/java 352329728 manual mode Press <enter> to keep the current choice[*], or type selection number:
Enter the number of the version you want to be used as the default and press
Verify that the default version has been changed by executing:
JAVA_HOME Environment Variable
JAVA_HOME environment variable
is used by some Java applications to determine the JDK installation location.
To set the
JAVA_HOME variable, first find the Java installation path with
sudo update-alternatives --config java
In this example, the paths to the different
java binaries are as follows:
- OpenJDK 21 is located at
- OpenJDK 17 is located at
- OpenJDK 11 is located at
JAVA_HOME/bin/java. So we should set the
JAVA_HOMEto one of the paths above, excluding the
Once you have found the path of your preferred Java installation, open the
sudo nano /etc/environment
Assuming you want to set
JAVA_HOME to point to OpenJDK 17, add the following line at the end of the file:
For changes to take effect on your current session, you can either log out and log in or run the following
To verify that the
JAVA_HOME environment variable was correctly set, type:
You should see the path to the Java installation:
/etc/environmentis a system-wide configuration file, which is used by all users. If you want to set the
JAVA_HOMEvariable on a per-user basis, then add the line to the
.bashrcor any other configuration file that is loaded when the user logs in.
You can uninstall Java like any other package installed with
For example, to uninstall the
openjdk-17-jdk package, enter:
sudo apt remove openjdk-17-jdk
We have shown you how to install OpenJDK 17 and OpenJDK 11 from the default Ubuntu 22.04 repositories using the
apt package manager and how to download and install the Oracle JDK.
If you have any questions, feel free to leave a comment.