Java’s ubiquitous presence in the technology landscape makes it an essential tool for developers, system administrators, and even hobbyists. Whether you’re building enterprise-level applications, developing Android apps, or working with intricate backend systems, having a robust Java Development Kit (JDK) installed and properly configured on your Linux Ubuntu system is paramount. This guide provides a comprehensive, step-by-step walkthrough to ensure a smooth and successful Java installation, covering various methods and best practices.
Understanding Java Versions and Editions
Before diving into the installation process, it’s crucial to understand the different editions and versions of Java available, as your choice will dictate the installation method and subsequent usage.

Java Development Kit (JDK) vs. Java Runtime Environment (JRE)
The primary distinction lies between the JDK and the JRE.
- Java Runtime Environment (JRE): This is the core component required to run Java applications. It includes the Java Virtual Machine (JVM), essential class libraries, and other supporting files. If your primary goal is to execute existing Java programs, the JRE is sufficient.
- Java Development Kit (JDK): This package is designed for developing Java applications. It encompasses the JRE, along with a suite of development tools, including a compiler (
javac), a debugger (jdb), and various utilities for creating and managing Java code. For anyone looking to write, compile, and test Java programs, the JDK is the necessary choice.
Oracle JDK vs. OpenJDK
The Java ecosystem features two prominent implementations: Oracle JDK and OpenJDK.
- Oracle JDK: This is the official, commercial build from Oracle. It has historically been the go-to choice for many, offering a feature-rich and well-supported platform. However, licensing changes in recent years have made its use in commercial environments subject to specific terms and potential costs.
- OpenJDK (Open Java Development Kit): This is the free and open-source reference implementation of the Java Platform, Standard Edition. It forms the basis for most other Java implementations, including those provided by Linux distributions. OpenJDK is generally the recommended choice for most users on Linux due to its open-source nature, ease of installation via package managers, and absence of licensing concerns.
Long-Term Support (LTS) Versions
Oracle and many OpenJDK distributions offer Long-Term Support (LTS) versions. These releases are maintained for an extended period, receiving security updates and bug fixes for several years. For stability and predictable maintenance, choosing an LTS version (e.g., Java 11 LTS, Java 17 LTS) is highly advisable for production environments.
Installing Java using Ubuntu’s Package Manager (Recommended)
For most Ubuntu users, the most straightforward and recommended method for installing Java is through the built-in Advanced Package Tool (APT). This approach leverages Ubuntu’s repositories, ensuring that the installed Java version is compatible with your system and can be easily updated.
Installing OpenJDK
Ubuntu’s default repositories typically contain recent versions of OpenJDK. This method is ideal for most users due to its simplicity and integration.
Finding Available OpenJDK Versions
Before installation, you can check which OpenJDK versions are available in your system’s repositories. Open a terminal and execute the following command:
sudo apt update
apt search openjdk
This command will list all available OpenJDK packages. You’ll likely see packages named openjdk-X-jdk, where X represents the Java version (e.g., openjdk-11-jdk, openjdk-17-jdk).
Installing the Default OpenJDK Version
To install the default OpenJDK version that Ubuntu recommends, simply run:
sudo apt install default-jdk
This command installs the JRE and the development tools for the version designated as the default in your Ubuntu repositories.
Installing a Specific OpenJDK Version
If you need a particular version of OpenJDK (e.g., OpenJDK 11), you can specify it:
sudo apt install openjdk-11-jdk
Replace 11 with your desired version number.
Verifying the Installation
After the installation completes, you can verify that Java has been installed correctly and check its version:
java -version
javac -version
The java -version command will display the installed JRE version, while javac -version will show the compiler version. If both commands output version information, your OpenJDK installation is successful.
Installing Oracle JDK (Less Common via APT)
While OpenJDK is readily available via APT, installing Oracle JDK directly through Ubuntu’s standard repositories is less common and often involves adding third-party Personal Package Archives (PPAs). This method is generally discouraged for production systems due to potential stability and maintenance issues associated with PPAs. However, if you have a specific need for Oracle JDK, here’s a general approach:
Note: The availability and reliability of PPAs can change. Always exercise caution and research any PPA before adding it to your system.
-
Add a PPA (Example – may be outdated or unavailable):
You might find PPAs that package Oracle JDK. For instance, you might use something like:sudo add-apt-repository ppa:webupd8team/java sudo apt updateDisclaimer: This PPA is illustrative and may no longer be maintained or functional.
-
Install Oracle JDK:
Once the PPA is added and the package list is updated, you would typically install a specific Oracle JDK version:sudo apt install oracle-java11-installerFollow the on-screen prompts, which may include accepting the Oracle license agreement.
-
Verify the Installation:
Similar to OpenJDK, verify the installation:java -version javac -version
Managing Multiple Java Versions
In development environments, it’s often necessary to switch between different Java versions. This is particularly true when working on projects that require specific JDK versions or when testing compatibility. Ubuntu provides a tool called update-alternatives to manage these symbolic links efficiently.
How update-alternatives Works
The update-alternatives system allows you to configure which executable (from multiple installed versions) is considered the “default” for a given command. It works by creating symbolic links in standard system directories (like /usr/bin/java) that point to the actual executable files of your installed Java versions.
Configuring update-alternatives
- List Installed Java Alternatives:
First, you need to register your installed Java versions withupdate-alternatives. If you installed Java usingapt, this is often done automatically. To see the currently registered Java alternatives, run:

```bash
sudo update-alternatives --config java
```
This command will display a list of all Java executables recognized by `update-alternatives`, along with their corresponding selection numbers.
-
Select the Default Java Version:
From the list presented by the command above, enter the number corresponding to the Java version you wish to set as the default and press Enter. For example, if you want to use OpenJDK 11, and it’s listed as option2, you would type2and press Enter. -
Configure the Java Compiler (
javac):
Thejavaccompiler is also managed byupdate-alternatives. To configure which compiler is used, run:sudo update-alternatives --config javacFollow the same procedure as with the
javacommand to select your preferred compiler. -
Registering a New Java Installation Manually (Advanced):
If you installed Java manually (e.g., downloaded a tarball), you’ll need to register it withupdate-alternatives. Assuming your JDK is installed in/usr/lib/jvm/my-custom-jdk-1.8.0:sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/my-custom-jdk-1.8.0/bin/java 1 sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/my-custom-jdk-1.8.0/bin/javac 1The
1at the end is the priority. Higher numbers indicate higher priority. You can then use--configas described above to switch between registered versions.
Setting the JAVA_HOME Environment Variable
Many Java-based applications, build tools (like Maven and Gradle), and application servers rely on the JAVA_HOME environment variable to locate the Java installation. Setting this variable correctly is crucial for their proper functioning.
What is JAVA_HOME?
JAVA_HOME is an environment variable that points to the root directory of your Java Development Kit installation. For example, if you installed OpenJDK 11 in the default Ubuntu location, JAVA_HOME might be set to /usr/lib/jvm/java-11-openjdk-amd64.
Setting JAVA_HOME System-Wide
The most robust way to set JAVA_HOME is to do it system-wide, affecting all users and services.
-
Identify your Java Installation Path:
Use theupdate-alternativescommand to find the correct path for your desired Java version. For example, after configuring your default Java, you can find its installation path:update-alternatives --display java | grep "link currently points to"This will output something like
/usr/lib/jvm/java-11-openjdk-amd64/bin/java. TheJAVA_HOMEpath is the directory above thebinfolder. So, in this example,JAVA_HOMEwould be/usr/lib/jvm/java-11-openjdk-amd64. -
Edit the
JAVA_HOME.shfile:
Ubuntu uses a standard location for environment configuration. Create or edit theJAVA_HOME.shfile in the/etc/profile.d/directory:sudo nano /etc/profile.d/JAVA_HOME.sh -
Add the
JAVA_HOMEExport:
Inside this file, add the following lines, replacing the path with your actual Java installation directory:export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 export PATH=$PATH:$JAVA_HOME/binThe second line ensures that the Java executables (
java,javac, etc.) are available in your system’s PATH. -
Make the Script Executable:
Ensure the script has execute permissions:sudo chmod +x /etc/profile.d/JAVA_HOME.sh -
Apply the Changes:
For the changes to take effect immediately in your current terminal session, you can either log out and log back in, or source the script:source /etc/profile.d/JAVA_HOME.sh -
Verify
JAVA_HOME:
Check if the variable is set correctly:echo $JAVA_HOME
Setting JAVA_HOME for a Specific User (Less Recommended for System-Wide Tools)
If you only need JAVA_HOME for your user account, you can set it in your ~/.bashrc or ~/.profile file.
-
Edit
~/.bashrc:nano ~/.bashrc -
Add the Export:
Append the following lines to the end of the file, adjusting the path as necessary:export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 export PATH=$PATH:$JAVA_HOME/bin -
Apply Changes:
Source the file:source ~/.bashrc
Troubleshooting Common Issues
While the installation process is generally straightforward, you might encounter a few issues.
“Command not found” Errors
If you receive errors like java: command not found or javac: command not found after installation, it typically means that the Java executables are not in your system’s PATH, or the JAVA_HOME variable is not set correctly.
- Check
PATH: Ensure that$JAVA_HOME/binis included in your system’s PATH. The system-wide installation using/etc/profile.d/JAVA_HOME.shusually handles this. - Verify
update-alternatives: Double-check that you have selected the correct Java version usingsudo update-alternatives --config javaandsudo update-alternatives --config javac. - Restart Terminal/Log Out: Sometimes, terminal sessions do not pick up environment variable changes. Try closing and reopening your terminal, or logging out and back into your Ubuntu session.
Permission Denied Errors
If you encounter “Permission denied” errors when trying to run Java commands, it might be due to incorrect file permissions on the Java installation directory. This is less common with APT installations but can occur with manual installations.
- Check Ownership and Permissions: Use
ls -lto inspect permissions. Ensure the Java directory and its contents are owned by root or the user who needs to access them.

Incorrect Java Version Being Used
If java -version shows an older or different version than you expect, it’s likely an issue with update-alternatives or your PATH configuration.
- Re-run
update-alternatives --config java: Ensure you’ve selected the desired version and that the symbolic link in/usr/bin/javapoints to the correct executable. - Check
PATHOrder: If you have multiple Java installations and multiple entries in yourPATH, the order matters. The firstjavaexecutable found in thePATHwill be used. Ensure your intendedJAVA_HOME/binappears before other Java paths if it should take precedence.
By following these steps, you can reliably install and configure Java on your Linux Ubuntu system, ensuring a smooth and efficient development or operational environment. Whether you opt for the simplicity of OpenJDK via APT or manage multiple versions with update-alternatives, understanding these core concepts is key to harnessing the power of Java.
