How to Install Java on Ubuntu

Thu Feb 27, 2025

In this document, we will go through a step-by-step guide on installing Java on Ubuntu OS.

Step 1: Launch a Ubuntu Instance:

Launch an Ubuntu instance and log in to it.

Step 2: Check if Java is Installed: Run the following command to check if Java is already installed:

java -version
If Java is not installed, you will see an error message similar to:
Command 'java' not found, but can be installed with: sudo apt install openjdk-11-jre-headless # version 11.0.20.1+1-0ubuntu1~22.04 sudo apt install default-jre # version 2:1.11-72build2 sudo apt install openjdk-17-jre-headless # version 17.0.8.1+1~us1-0ubuntu1~22.04 sudo apt install openjdk-18-jre-headless # version 18.0.2+9-2~22.04 sudo apt install openjdk-19-jre-headless # version 19.0.2+7-0ubuntu3~22.04 sudo apt install openjdk-8-jre-headless # version 8u382-ga-1~22.04.1
Step 3: Update the Repository: Before installing Java, update the package repository by running:
sudo apt-get update
Step 4: Install Java: Run the following command to install OpenJDK 17 (recommended):
sudo apt-get install openjdk-17-jre
If prompted, press Y to confirm the installation.

Step 5: Verify the Installation: After installation, verify that Java is properly installed by running:

java -version
If Java has been installed successfully, you should see an output similar to:
openjdk version "17.0.10" 2024-01-16 OpenJDK Runtime Environment (build 17.0.10+7-Ubuntu-122.04.1) OpenJDK 64-Bit Server VM (build 17.0.10+7-Ubuntu-122.04.1, mixed mode, sharing)
Step 6: Uninstall Java (Optional): If you need to uninstall Java, use the following command:
sudo apt-get purge openjdk-\*
If prompted, press Y to confirm the removal.

Chinmay Biswal
Solution Architect