
How to Install Java on Ubuntu 22.04: A Step-by-Step Guide
Java remains a cornerstone of the software development world, powering everything from enterprise-level applications and big data processing to popular Android apps. For developers and system administrators working with Ubuntu 22.04 LTS (Jammy Jellyfish), setting up the correct Java environment is a fundamental first step.
This comprehensive guide will walk you through installing various versions of OpenJDK on your Ubuntu 22.04 system, ensuring you have the tools you need for your projects.
Before we begin, it’s important to understand the two main components you’ll encounter:
- JDK (Java Development Kit): This is for developers. It includes everything you need to compile, debug, and run Java applications. It contains the JRE.
- JRE (Java Runtime Environment): This is for users who only need to run Java applications, not develop them. It includes the Java Virtual Machine (JVM) and core libraries.
This guide will focus on installing the JDK, as it is the most common requirement for development work.
Prerequisites
- A system running Ubuntu 22.04.
- Access to the terminal or command line.
- A user account with
sudoor root privileges.
Step 1: Update Your System’s Package List
Before installing any new software, it’s a crucial security and stability practice to update your package repository. This ensures you are downloading the latest and most secure versions of your software and its dependencies.
Open your terminal and run the following commands:
sudo apt update
sudo apt upgrade
Enter your password when prompted and wait for the process to complete.
Step 2: Check for an Existing Java Installation
It’s possible that Java is already installed on your system, especially if you’ve used it for other development work. You can easily check by running:
java -version
If Java is installed, you’ll see output detailing the version. If not, you’ll see a message like “Command ‘java’ not found.”
Step 3: Install the Default OpenJDK (Recommended)
Ubuntu 22.04 includes OpenJDK 11 in its default repositories. This is an LTS (Long-Term Support) version, making it a stable and reliable choice for most general-purpose development.
To install the default JDK, use this simple command:
sudo apt install default-jdk
If you only need to run Java applications and not develop them, you can install the lighter JRE package instead:
sudo apt install default-jre
Once the installation is complete, verify it by checking the version again:
java -version
You should now see output confirming that OpenJDK 11 is installed.
Step 4: Install Specific OpenJDK Versions (11, 17, 18+)
Your project might require a different version of Java. Thankfully, Ubuntu’s repositories make it easy to install other common versions side-by-side.
Installing OpenJDK 17 LTS
OpenJDK 17 is another popular LTS version offering modern features and long-term stability. To install it, run:
sudo apt install openjdk-17-jdk
Installing OpenJDK 18 (or newer)
If you need a more recent, non-LTS version for its cutting-edge features, you can install it directly as well. For OpenJDK 18, use:
sudo apt install openjdk-18-jdk
Pro Tip: You can search for all available OpenJDK packages in your repository with the following command:
apt-cache search openjdk
This will give you a list of all available JDK and JRE versions you can install.
Step 5: Manage Multiple Java Installations
If you have installed more than one version of Java, you need a way to switch between them. Ubuntu uses the update-alternatives utility for this purpose.
To see all installed Java versions and select your default, run this interactive command:
sudo update-alternatives --config java
You will see a list of your installed Java versions, each with a selection number.
There are 3 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/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:
Simply type the selection number for the version you want to set as the default and press Enter. You can also configure the default Java compiler (javac) in the same way:
sudo update-alternatives --config javac
Step 6: Set the JAVA_HOME Environment Variable
Many Java-based applications (like Maven, Jenkins, and Tomcat) rely on the JAVA_HOME environment variable to locate the Java installation directory. This is a critical final step for a properly configured development environment.
First, find the exact path of your desired Java installation. You can use the update-alternatives command from the previous step to list the paths. For example, the path for OpenJDK 11 is typically /usr/lib/jvm/java-11-openjdk-amd64.
Next, open the system-wide environment file in a text editor:
sudo nano /etc/environment
Go to the end of the file and add the following line, making sure to replace the path with the one for your chosen Java version:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Save the file and exit the editor (press CTRL + X, then Y, then Enter in nano).
To apply the changes immediately without rebooting, run:
source /etc/environment
Finally, verify that the JAVA_HOME variable is set correctly:
echo $JAVA_HOME
The output should be the path you just set. You are now fully configured and ready to build and run powerful Java applications on your Ubuntu 22.04 system.
Source: https://kifarunix.com/install-java-11-java-17-java-18-on-ubuntu/


