Java remains one of the most pervasive and influential programming languages in the world, powering everything from enterprise-grade applications and mobile platforms to scientific tools and vast cloud infrastructures. For developers, system administrators, and even end-users who rely on Java-based applications, having a correctly configured Java environment on their Linux Ubuntu system is not merely convenient, but often essential. This comprehensive guide will walk you through the process of installing and managing Java on Ubuntu, ensuring your system is ready for development, deployment, or simply running your favorite Java applications.
The versatility of Java stems from its “write once, run anywhere” philosophy, a promise delivered by the Java Virtual Machine (JVM). Whether you’re a seasoned developer setting up a new development environment or a curious user looking to run a specific piece of software, understanding the nuances of Java installation on a Linux distribution like Ubuntu is a critical skill. We will delve into the various flavors of Java available, the recommended installation methods, and crucial post-installation configurations, all presented with clarity and a professional, insightful tone.

Understanding the Java Ecosystem on Ubuntu
Before diving into the installation steps, it’s vital to grasp the different components and versions of Java commonly encountered, especially within the Linux ecosystem. This foundational understanding will help you choose the right tools for your specific needs.
OpenJDK vs. Oracle JDK
Historically, Oracle JDK was the de facto standard, but with changes in its licensing model (commercial use requiring a subscription), OpenJDK has emerged as the preferred, open-source, and free alternative for most users and enterprises.
-
OpenJDK: This is the free and open-source implementation of the Java Platform, Standard Edition (Java SE). It is developed and maintained by Oracle and the Java community. OpenJDK is what you’ll typically find in the default repositories of most Linux distributions, including Ubuntu. It offers full compatibility with the Java SE specification and is widely used for development, production, and learning. For the vast majority of users, OpenJDK is the recommended choice due to its open licensing, active development, and ease of installation via package managers.
-
Oracle JDK: While also developed by Oracle, the Oracle JDK is a commercial product. While free for personal use, development, and testing, its usage in production environments often requires a paid license. It occasionally includes proprietary features not found in OpenJDK (though this gap has significantly narrowed over time). For most Linux users, particularly those on Ubuntu, opting for OpenJDK simplifies licensing and maintenance, making it the practical default.
JDK vs. JRE
Within both OpenJDK and Oracle JDK, you’ll encounter two primary components: the Java Runtime Environment (JRE) and the Java Development Kit (JDK).
-
JRE (Java Runtime Environment): This package contains all the necessary components to run Java applications. It includes the Java Virtual Machine (JVM), core classes, and supporting files. If you only need to execute Java applications (e.g., a Java-based game, a utility tool), the JRE is sufficient. It’s lighter and requires fewer resources than the JDK.
-
JDK (Java Development Kit): This is a superset of the JRE. In addition to everything included in the JRE, the JDK provides tools for developing Java applications. Key components of the JDK include the Java compiler (
javac), debugging tools, archiving tools (jar), and documentation generators. If you plan to write, compile, or debug Java code, you will need the JDK.
For most developers and users looking for a complete Java environment on Ubuntu, installing the JDK is the most practical approach as it includes the JRE and all development tools.
Preparing Your Ubuntu System for Java Installation
Before installing Java, it’s a good practice to ensure your Ubuntu system is up-to-date and to address any potential conflicts from previous Java installations. This minimizes issues and ensures a smooth setup.
Updating System Packages
Always begin by updating your package lists and upgrading any existing packages to their latest versions. This ensures you’re pulling the most recent versions of software from the repositories and have a stable base system.
sudo apt update
sudo apt upgrade
Run these commands in your terminal. sudo apt update refreshes the list of available packages and their versions, while sudo apt upgrade installs newer versions of the packages you have installed.
Removing Previous Java Installations (Optional but Recommended)
If you’ve previously attempted to install Java or have an old version lingering on your system, it’s often best to remove it to avoid conflicts. You can typically remove old Java installations using the apt remove command. For instance, to remove a generic default JDK or JRE:
sudo apt autoremove --purge openjdk-*
sudo apt autoremove --purge oracle-java*
The --purge flag ensures that configuration files associated with the packages are also removed. If you know the specific version (e.g., openjdk-8-jdk), you can specify it. This step helps ensure a clean installation of the new Java version.
Installing OpenJDK – The Recommended Approach
For most Ubuntu users, OpenJDK is the preferred and easiest way to get Java up and running. Ubuntu’s default repositories provide several stable and well-maintained OpenJDK versions.
Installing the Default OpenJDK (JRE and JDK)
Ubuntu maintains a default-jdk meta-package that points to the latest Long Term Support (LTS) version of OpenJDK available in its repositories. This is the simplest way to get started.
To install the default JRE (for running Java applications):
sudo apt install default-jre
To install the default JDK (for developing and running Java applications):
sudo apt install default-jdk
After installation, you can verify that Java is correctly installed and accessible system-wide.
Installing Specific OpenJDK Versions (e.g., OpenJDK 11, 17, 21)
While default-jdk is convenient, you might need a specific Java version for compatibility with certain projects or tools. Java LTS versions like 11, 17, and 21 are particularly popular.
First, you can search for available OpenJDK packages in the Ubuntu repositories:
apt search openjdk
This command will list all OpenJDK-related packages. Look for openjdk-XX-jdk where XX is the version number.
To install OpenJDK 11 (an LTS release):
sudo apt install openjdk-11-jdk
To install OpenJDK 17 (another LTS release):
sudo apt install openjdk-17-jdk
To install OpenJDK 21 (the latest LTS release as of this writing):
sudo apt install openjdk-21-jdk
You can install multiple JDK versions if needed.
Verifying the Installation
After installing any Java package, always verify that it’s correctly installed and the system recognizes it.
Check the Java runtime version:
java -version
This command should output information about the installed JRE, including its version number. For example:
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment (build 17.0.8+7-Ubuntu-123.04.1)
OpenJDK 64-Bit Server VM (build 17.0.8+7-Ubuntu-123.04.1, mixed mode, sharing)
If you installed the JDK, also check the Java compiler version:

javac -version
This should show the compiler version, which should match your java -version output.
Managing Multiple Java Versions
In a development environment, it’s common to have multiple Java versions installed to support different projects. Ubuntu provides a robust mechanism to switch between these versions seamlessly.
Using update-alternatives
The update-alternatives command is a powerful tool on Debian-based systems like Ubuntu that manages symbolic links for commands that might have multiple implementations. This allows you to set which version of Java (or javac, jar, etc.) is the default for your system.
To configure the default Java runtime:
sudo update-alternatives --config java
This command will present a list of all installed Java environments, allowing you to choose the default by entering its corresponding number.
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 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
Press <enter> to keep the current choice[*], or type selection number:
Similarly, to configure the default Java compiler (javac):
sudo update-alternatives --config javac
After making your selection, verify the change with java -version and javac -version.
Setting Up the JAVA_HOME Environment Variable
While update-alternatives sets the system’s default Java executable, many Java-based applications, build tools (like Apache Maven or Gradle), and application servers (like Apache Tomcat) rely on the JAVA_HOME environment variable. This variable points to the root directory of your Java Development Kit installation.
Why JAVA_HOME is Important
JAVA_HOME provides a standardized way for other software to locate the Java installation, regardless of which version is currently active via update-alternatives. Without it, these applications might fail to run or compile correctly.
Finding the Java Installation Path
Before setting JAVA_HOME, you need to know the exact path to your desired JDK installation. You can find this by checking where the current java executable points:
update-alternatives --query java
Look for the “Value:” field, which will point to a path like /usr/lib/jvm/java-XX-openjdk-amd64/bin/java. The JAVA_HOME path will be the parent directory of bin, so in this example, it would be /usr/lib/jvm/java-XX-openjdk-amd64.
Alternatively, you can use readlink:
readlink -f /usr/bin/java
This will give you the full path to the java executable. Trim the /bin/java part to get your JAVA_HOME. For instance, if the output is /usr/lib/jvm/java-17-openjdk-amd64/bin/java, then JAVA_HOME should be /usr/lib/jvm/java-17-openjdk-amd64.
Configuring JAVA_HOME Globally
For system-wide availability, it’s best to set JAVA_HOME in a global configuration file. A common and robust method is to create a new script in /etc/profile.d/ or edit /etc/environment.
Method 1: Using /etc/profile.d/ (Recommended for dynamic JAVA_HOME)
Create a new file, for example, /etc/profile.d/jdk.sh:
sudo nano /etc/profile.d/jdk.sh
Add the following content to the file, replacing /usr/lib/jvm/java-17-openjdk-amd64 with your actual JDK path:
export JAVA_HOME=$(update-alternatives --query java | sed -n 's/Value: //p' | sed -n 's//bin/java$//p')
export PATH=$JAVA_HOME/bin:$PATH
This script dynamically sets JAVA_HOME based on your update-alternatives configuration for java, which is often more robust if you switch versions frequently.
Save and exit (Ctrl+X, Y, Enter for Nano). For the changes to take effect, either log out and log back in, or source the file:
source /etc/profile
Method 2: Using /etc/environment (Recommended for static JAVA_HOME)
Edit the /etc/environment file:
sudo nano /etc/environment
Add the JAVA_HOME line at the end, replacing the path with your specific JDK installation path:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64" # Replace with your actual path
Save and exit. For changes to take effect, either log out and log back in, or execute:
source /etc/environment
Verifying JAVA_HOME
After setting JAVA_HOME, always verify that it’s correctly recognized by your shell:
echo $JAVA_HOME
This command should output the path you just configured. If it’s empty or incorrect, review your configuration steps.

Conclusion
Installing Java on Linux Ubuntu, particularly OpenJDK, is a straightforward process when you follow the recommended steps. By leveraging Ubuntu’s robust package management system (apt), you can quickly get a functional Java environment for both running and developing applications.
We’ve covered the crucial distinction between OpenJDK and Oracle JDK, the roles of JDK and JRE, and the step-by-step process for installing default and specific OpenJDK versions. Furthermore, we explored how to manage multiple Java installations using update-alternatives and, critically, how to set the JAVA_HOME environment variable, which is essential for many professional Java tools and frameworks.
With Java successfully installed and configured, your Ubuntu system is now poised to embark on a vast array of possibilities, from compiling your first “Hello, World!” program to deploying complex enterprise solutions. Remember to keep your system and Java installations updated, and your development journey on Ubuntu will be both productive and secure.
aViewFromTheCave is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. Amazon, the Amazon logo, AmazonSupply, and the AmazonSupply logo are trademarks of Amazon.com, Inc. or its affiliates. As an Amazon Associate we earn affiliate commissions from qualifying purchases.