Table of Contents
Java is one of the most widely used programming languages in the world. You use Java to build web applications, backend services, desktop software, and Android apps. Many popular tools also depend on Java, including Jenkins, Elasticsearch, Kafka, SonarQube, and various game servers.
When you install Java on Ubuntu, you usually choose between JRE and JDK:
- JRE (Java Runtime Environment) lets you run Java applications.
- JDK (Java Development Kit) lets you develop and run Java applications.
If you only want to run a Java-based application or server, the JRE is enough. If you plan to write, compile, or debug Java code, you need JDK.
In this guide, you will learn how to install Java OpenJDK and Java JRE on Ubuntu 16.04 and Ubuntu 20.04.
Prerequisites
You need a Ubuntu server that is configured with a static IP address. If you do not have a server already, why not spin up a robust and reliableĀ Cloud Server in under 30 seconds?
Understanding Java Versions on Ubuntu
Before installing Java, it helps to understand the Java versions available on Ubuntu 20.04. This makes it easier to choose the right package for your needs.
OpenJDK vs Oracle JDK
Ubuntu ships with OpenJDK, which is the open-source implementation of Java. It is free, well-maintained, and suitable for most applications. For most users and servers, OpenJDK is the recommended choice.
Oracle JDK is the official Java distribution from Oracle. Some enterprise or legacy applications specifically require Oracle JDK. In such cases, you must install it manually and follow Oracleās licensing terms.
LTS vs Non-LTS Java Versions
Java releases come in two types:
- LTS (Long-Term Support) versions receive long-term updates and security fixes.
- Non-LTS versions get features faster but have shorter support cycles.
Ubuntu focuses on stability, so it primarily provides LTS Java versions.
Check Existing Java Installation
Before installing Java, you should check whether Java is already installed on your system. Ubuntu often comes with Java preinstalled, especially on servers that run Java-based tools.
Open your terminal and run the following command.
java -version
Once you have verified if Java is installed or not, you can proceed to the next step.
Install Default Java (OpenJDK)
The easiest way to install Java on Ubuntu is by using the APT package manager. Ubuntu provides default Java packages that automatically install the recommended OpenJDK version.
First, update your systemās package list.
apt update -y
If you only need to run Java applications, install the default JRE.
apt install default-jre -y
This installs the Java Runtime Environment without development tools.
If you want to develop or compile Java programs, install the default JDK.
apt install default-jdk -y
This installs JRE along with development tools like javac.
After installation, check the Java version.
java -version
For JDK users, also verify the compiler.
javac -version
If the commands return version details, Java is installed successfully.
Install a Specific OpenJDK Version
Sometimes, your application requires a specific Java version. Ubuntu allows you to install multiple OpenJDK versions side by side.
Install a Specific OpenJDK Version on Ubuntu 16.04
First, check which OpenJDK versions are available in the Ubuntu repository.
apt search openjdk
You will see all available Java versions.
openjdk-8-jdk openjdk-9-jdk
To install OpenJDK 8, run.
apt install openjdk-8-jdk -y
Now, verify the installation.
java -version
Output.
openjdk version "1.8.0_292" OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-0ubuntu1~16.04.1-b10) OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
Install a Specific OpenJDK Version on Ubuntu 20.04
First, check all available Java versions.
apt search openjdk
You will see all available versions in the output below.
openjdk-8-jdk openjdk-11-jdk openjdk-13-jdk openjdk-16-jdk openjdk-17-jdk openjdk-21-jdk
To install OpenJDK 8, run.
apt install openjdk-8-jdk -y
To install OpenJDK 17, run.
apt install openjdk-17-jdk -y
OpenJDK 21 is a newer LTS version suitable for modern applications. You can install it with the following command.
apt install openjdk-21-jdk -y
Verify the Java version.
java -version
Output:
openjdk version "21.0.7" 2025-04-15 OpenJDK Runtime Environment (build 21.0.7+6-Ubuntu-0ubuntu120.04) OpenJDK 64-Bit Server VM (build 21.0.7+6-Ubuntu-0ubuntu120.04, mixed mode, sharing)
Install Oracle JDK on Ubuntu
Some applications require Oracle JDK instead of OpenJDK. In such cases, you must install it manually because it is not included in Ubuntuās default repositories.
Visit the official Oracle Java download page and download the Linux .deb file for the required Java version.
wget https://download.oracle.com/java/25/archive/jdk-25_linux-x64_bin.deb
Next, install the downloaded file.
apt install ./jdk-25_linux-x64_bin.deb
Select Oracle JDK as the default version.
update-alternatives --config java
Choose the Oracle 25 as the default version:
There are 4 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/jdk-25-oracle-x64/bin/java 419430400 auto mode 1 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode 2 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 4 /usr/lib/jvm/jdk-25-oracle-x64/bin/java 419430400 manual mode Press to keep the current choice[*], or type selection number: 4
Verify Oracle JDK installation.
java -version
Output.
java version "25" 2025-09-16 LTS Java(TM) SE Runtime Environment (build 25+37-LTS-3491) Java HotSpot(TM) 64-Bit Server VM (build 25+37-LTS-3491, mixed mode, sharing)
Manage Multiple Java Versions
On Ubuntu, you can install and use multiple Java versions at the same time. This is useful when different applications require different Java releases.
To see all installed Java versions, run:
update-alternatives --list java
This shows the paths of all Java binaries available on your system.
/usr/lib/jvm/java-17-openjdk-amd64/bin/java /usr/lib/jvm/java-21-openjdk-amd64/bin/java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java /usr/lib/jvm/jdk-25-oracle-x64/bin/java
To choose the default Java version, run:
update-alternatives --config java
You will see output similar to this:
There are 4 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/jdk-25-oracle-x64/bin/java 419430400 auto mode 1 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode 2 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode * 4 /usr/lib/jvm/jdk-25-oracle-x64/bin/java 419430400 manual mode Press to keep the current choice[*], or type selection number:
Enter the selection number and press Enter.
If you are using JDK, also configure the Java compiler:
update-alternatives --config javac
You will see output similar to this:
There are 4 choices for the alternative javac (providing /usr/bin/javac). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/jdk-25-oracle-x64/bin/javac 419430400 auto mode 1 /usr/lib/jvm/java-17-openjdk-amd64/bin/javac 1711 manual mode 2 /usr/lib/jvm/java-21-openjdk-amd64/bin/javac 2111 manual mode 3 /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1081 manual mode 4 /usr/lib/jvm/jdk-25-oracle-x64/bin/javac 419430400 manual mode Press to keep the current choice[*], or type selection number:
This keeps Java and javac aligned with the same version.
Confirm the currently active version:
java -version
Set JAVA_HOME Environment Variable
Many Java-based applications rely on the JAVA_HOME environment variable to locate the Java installation. Setting it correctly helps avoid runtime errors.
First, find the path of the active Java version:
readlink -f $(which java)
Example output:
/usr/lib/jvm/jdk-25-oracle-x64/bin/java
The JAVA_HOME path is the directory above /bin/java:
/usr/lib/jvm/jdk-25-oracle-x64
To set JAVA_HOME for the current terminal session:
export JAVA_HOME=/usr/lib/jvm/jdk-25-oracle-x64/ export PATH=$PATH:$JAVA_HOME/bin
This setting resets when you close the terminal.
To make the change permanent, edit /etc/environment:
nano /etc/environment
Add the following line:
JAVA_HOME="/usr/lib/jvm/jdk-25-oracle-x64"
Save the file and reload the environment:
source /etc/environment
Check that JAVA_HOME is set correctly:
echo $JAVA_HOME
Output:
/usr/lib/jvm/jdk-25-oracle-x64/
If the correct path appears, the configuration is complete.
Test Java Installation
After installing Java and setting JAVA_HOME, you should test whether everything works correctly.
First, create a simple Java program.
nano HelloWorld.java
Add the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Java is working on Ubuntu");
}
}
Save and close the file.
Compile the program using javac:
javac HelloWorld.java
If there are no errors, the compilation is successful.
Run the compiled program:
java HelloWorld
Output:
Java is working on Ubuntu
Uninstall Java from Ubuntu
If you no longer need Java or want to remove a specific version, Ubuntu allows you to uninstall Java cleanly.
To remove the default OpenJDK packages, run:
apt remove openjdk-* -y
This removes all installed OpenJDK versions.
To remove Oracle JDK, run.
apt remove jdk-25
Remove unused dependencies.
apt autoremove -y
Confirm Java is removed:
java -version
If Java is uninstalled, you will see:
Command 'java' not found
Conclusion
Installing Java on Ubuntu is a simple process once you understand which version you need. If you only want to run Java-based applications, JRE is enough. If you plan to write, compile, or debug Java programs, you should install JDK.
Ubuntuās official repositories make it easy to install OpenJDK, which works well for most use cases. You can also install a specific Java version or use Oracle JDK when an application demands it. When multiple Java versions are installed, update-alternatives helps you switch between them without breaking your system.
Setting the JAVA_HOME environment variable and testing Java with a small program confirms that everything works as expected. If Java is no longer needed, you can remove it cleanly using APT or by deleting manual installations.