By Brian Hogan and Manikandan Kurup

Java is required to run and build many popular applications and services, including application servers, CI tools, and distributed systems. On Ubuntu, the recommended way to install Java is through the OpenJDK packages provided by the operating system.
In this guide, you will install Java on Ubuntu 24.04 LTS using the apt package manager. You will start with OpenJDK, which is the default and recommended Java implementation on Ubuntu. You will learn the difference between JRE and JDK, install more than one Java versions, manage multiple installations, and configure the JAVA_HOME environment variable.
By the end of this tutorial, you will have a working Java setup suitable for both running and developing Java applications.
Key Takeaways:
default-jdk package installs both the Java runtime and development tools and is the best choice for most systems.default-jre package installs only the runtime and is suitable for systems that run prebuilt Java applications.update-alternatives system controls which Java version is used by default for both java and javac.JAVA_HOME environment variable helps prevent issues with build tools and application servers.To follow this tutorial, you will need:
Note: This tutorial was tested using Ubuntu 24.04 server. Most commands should work on newer Ubuntu releases, though package versions and defaults may differ.
default-jdk vs default-jreBefore installing Java on Ubuntu, you will see two common package options: default-jdk and default-jre. While they sound similar, they serve different purposes, and choosing the right one can save time later.
At a high level, Java is split into two parts:
javac.If you only have the JRE installed, you can run existing .jar files, but you cannot compile Java source code.
default-* packages provideUbuntu simplifies Java installation by offering two convenience packages that point to the system’s default Java version.
| Package | Includes | Typical use cases |
|---|---|---|
default-jdk |
JDK and JRE | Development, build servers, CI pipelines, most servers |
default-jre |
JRE only | Running pre-built Java applications |
These packages do not contain Java themselves. Instead, they depend on the current default OpenJDK version provided by Ubuntu. On Ubuntu 24.04, that version is OpenJDK 21.
default-jre?Installing default-jre makes sense only in limited cases, such as:
Even in these cases, many administrators still choose the JDK to avoid future limitations.
default-jdk is usually the better choiceFor most users, default-jdk is the correct option, even if you do not plan to write Java code immediately for the following reasons:
javac is available, even when compiling happens indirectlyInstalling the JDK upfront avoids reinstalling Java later when a tool unexpectedly requires development utilities.
If you are unsure which package to install, choose:
sudo apt install default-jdk
This gives you a complete Java environment that works for running applications, building software, and supporting automation tools without additional setup.
For most users, the simplest and most reliable way to install Java on Ubuntu is to use the OpenJDK packages provided by the operating system. These packages are maintained by Ubuntu, receive regular security updates, and integrate cleanly with the rest of the system.
On Ubuntu 24.04 LTS, the default Java version is OpenJDK 21, which is the current Long-Term Support (LTS) release for Ubuntu 24.04. LTS releases are designed for stability and long-term maintenance, making them a good choice for servers, development machines, and production systems.
Start by updating your package index:
- sudo apt update
Next, check whether Java is already installed:
- java -version
If Java is not installed, you will see a message indicating that the command cannot be found.
OutputCommand 'java' not found, but can be installed with:
sudo apt install default-jre # version 2:1.17-75, or
sudo apt install openjdk-17-jre-headless # version 17.0.17+10-1~24.04
sudo apt install openjdk-21-jre-headless # version 21.0.9+10-1~24.04
sudo apt install openjdk-11-jre-headless # version 11.0.29+7-1ubuntu1~24.04
sudo apt install openjdk-25-jre-headless # version 25.0.1+8-1~24.04
sudo apt install openjdk-8-jre-headless # version 8u472-ga-1~24.04
sudo apt install openjdk-19-jre-headless # version 19.0.2+7-4
sudo apt install openjdk-20-jre-headless # version 20.0.2+9-1
sudo apt install openjdk-22-jre-headless # version 22~22ea-1
Run the following command to install the default JDK:
- sudo apt install default-jdk
This installs OpenJDK 21 along with the required runtime libraries.
To verify the installation, run:
- java -version
You should see output similar to the following (the exact version may differ):
Outputopenjdk version "21.0.9" 2025-10-21
OpenJDK Runtime Environment (build 21.0.9+10-Ubuntu-124.04)
OpenJDK 64-Bit Server VM (build 21.0.9+10-Ubuntu-124.04, mixed mode, sharing)
Note: The exact patch version and build number may vary depending on available security updates.
Confirm that the Java compiler is available by running:
- javac -version
Expected output:
Outputjavac 21.0.9
Your system now has a complete Java development environment installed.
In most cases, installing the default OpenJDK package is enough. However, some applications or organizations require a specific Java version, especially in long-running production environments or when matching an existing runtime.
Ubuntu allows multiple OpenJDK versions to be installed at the same time. Each version is placed in its own directory and does not overwrite the others, making it safe to install more than one.
If you want to install OpenJDK 21 directly, rather than relying on default-jdk, run:
- sudo apt install openjdk-21-jdk
This installs the Java runtime and development tools for Java 21.
Java 17 is still widely used and may be required by some applications. You can install it alongside Java 21 using the following command:
- sudo apt install openjdk-17-jdk
After installation, both versions will exist under /usr/lib/jvm, for example:
/usr/lib/jvm/java-21-openjdk-amd64/usr/lib/jvm/java-17-openjdk-amd64Installing an additional Java version does not change the active version automatically. Ubuntu continues using the current default until you switch it.
To see which Java versions are installed, run:
- ls /usr/lib/jvm
To check which version is currently in use, run:
- java -version
Switching between installed versions is handled using update-alternatives, which is covered in the next section.
update-alternativesWhen you install more than one Java version on Ubuntu, the system needs a way to decide which version is used by default. Ubuntu handles this through a mechanism called update-alternatives, which manages symbolic links for system commands.
update-alternatives manages symbolic links for common commands such as java and javac. This allows multiple versions of the same tool to coexist, while only one is active at a time.
To see which Java runtimes are available and which one is currently selected, run:
- sudo update-alternatives --config java
You will see output similar to the following:
OutputThere are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
---------------------------------------------------------------
0 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 auto mode
* 1 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Each entry shows the Java binary path and its priority. The entry marked with * is the currently selected version.
To change the active Java version, type the selection number and press ENTER. The change takes effect immediately for new shell sessions and newly started processes.
After switching, confirm the active version by running:
- java -version
This command reports the Java version that is currently in use, not just the versions installed on the system.
If you are compiling Java code, you should also switch the Java compiler to match the runtime:
- sudo update-alternatives --config javac
Keeping java and javac aligned helps avoid confusing build or runtime issues, especially when working with multiple projects.
update-alternativesYou typically need update-alternatives when:
If only one Java version is installed, update-alternatives is still present but does not require manual changes.
JAVA_HOME Environment VariableMany Java-based tools and frameworks rely on the JAVA_HOME environment variable to determine where Java is installed on the system. While some modern tools can detect Java automatically, setting JAVA_HOME explicitly avoids ambiguity and helps prevent configuration issues.
JAVA_HOME mattersThe JAVA_HOME variable points to the root directory of a Java installation. Build tools such as Maven and Gradle use this variable to locate the Java compiler and runtime. Some application servers and startup scripts also require JAVA_HOME to be set before they will run correctly.
If JAVA_HOME is missing or incorrect, Java-based tools may fail to start or may use an unexpected Java version.
Before setting JAVA_HOME, you need to determine where Java is installed on your system. You can use the update-alternatives system to identify the active Java binary.
Run the following command:
- sudo update-alternatives --config java
This command displays the available Java installations and their full paths. Each path ends with /bin/java. You should copy the directory path without the /bin/java portion.
For example, if the selected path is:
/usr/lib/jvm/java-21-openjdk-amd64/bin/java
Then the correct JAVA_HOME value is:
/usr/lib/jvm/java-21-openjdk-amd64
JAVA_HOME system-wideIf you want JAVA_HOME to be available to all users and system services, you should set it in /etc/environment.
Open the file with a text editor:
- sudo nano /etc/environment
Add the following line at the end of the file, replacing the path with your Java installation directory:
JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"
Save the file and exit the editor.
To apply the change to your current shell session, reload the environment variables, or log out and log back in:
- source /etc/environment
You can verify that the variable is set correctly by running:
- echo $JAVA_HOME
The command should print the path you configured.
JAVA_HOME for a single userIf you only need JAVA_HOME for your own user account, you can add the same line to the ~/.profile file instead of /etc/environment. This approach limits the setting to your user session and does not affect system services or other users.
After editing ~/.profile, you must log out and log back in, or reload the file, for the change to take effect.
Note: You should not include the /bin directory in the JAVA_HOME path. You should also avoid setting JAVA_HOME to a symbolic link unless you understand how it resolves. Using the full installation directory under /usr/lib/jvm provides the most predictable results.
In most situations, OpenJDK is the recommended and sufficient choice. However, some vendors, legacy applications, or commercial support agreements may require the use of Oracle JDK. In those cases, you can install Oracle JDK alongside OpenJDK and select it when needed.
Before proceeding, you should be aware that Oracle JDK is distributed under a commercial license. You should review Oracle’s licensing terms to ensure compliance, especially for production or commercial use. Oracle JDK is not available through Ubuntu’s package repositories, and it does not receive automatic updates through apt.
Start by visiting Oracle’s official Java SE download page and selecting the required JDK version for Linux. Download the .tar.gz archive that matches your system architecture.
Once the download is complete, transfer the archive to your Ubuntu system if you downloaded it on another machine.
Change to the directory where the archive is located and extract it:
- tar -xvf jdk-21_linux-x64_bin.tar.gz
Next, move the extracted directory to /usr/lib/jvm, which is the standard location for Java installations on Ubuntu:
- sudo mkdir -p /usr/lib/jvm
- sudo mv jdk-21* /usr/lib/jvm/
Placing Oracle JDK in this directory keeps it consistent with OpenJDK installations and simplifies version management.
update-alternativesTo allow Ubuntu to manage Oracle JDK alongside OpenJDK, you must register it with update-alternatives.
Register the Java runtime:
- sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-21/bin/java 3000
Register the Java compiler:
- sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-21/bin/javac 3000
These commands inform the system that Oracle JDK is an available Java option and assign it a priority.
If you want to use Oracle JDK as the active Java version, select it using update-alternatives:
- sudo update-alternatives --config java
- sudo update-alternatives --config javac
After making your selection, verify the active version:
- java -version
- javac -version
The output should indicate Oracle JDK if it is selected.
Note: You may also want to register other Java tools such as keytool or javadoc if you rely on them.
JAVA_HOME for Oracle JDKIf you plan to use Oracle JDK regularly, you should update the JAVA_HOME environment variable to point to the Oracle installation directory:
JAVA_HOME="/usr/lib/jvm/jdk-21"
You can set this value system-wide in /etc/environment or for a single user in ~/.profile, depending on your requirements.
Unlike OpenJDK installed through apt, Oracle JDK does not update automatically. You are responsible for monitoring security updates and installing new versions manually. For this reason, many users prefer OpenJDK unless Oracle JDK is explicitly required by a vendor or support agreement.
Installing Java on Ubuntu is usually straightforward, but issues can still occur due to version conflicts, configuration mistakes, or environment variables. This section covers common problems and explains how to resolve them.
java: command not foundThis error means that Java is not installed or that the Java binary is not available in your system’s PATH environment variable.
First, verify whether Java is installed:
- java -version
If the command is not found, install the default JDK:
- sudo apt update
- sudo apt install default-jdk
If Java is installed but the command still fails, the system may not be pointing to an active Java installation. In that case, check the configured alternatives:
- sudo update-alternatives --config java
Select a valid Java path and verify again.
This issue occurs when multiple Java versions are installed and the system is using a different version than expected.
Check the active Java version:
- java -version
If the version is not the one you want, change it using update-alternatives:
- sudo update-alternatives --config java
- sudo update-alternatives --config javac
Ensure that both java and javac point to the same version to avoid compatibility problems.
javac: command not foundThis error means that only the Java Runtime Environment is installed, not the Java Development Kit.
Confirm whether the JDK is installed:
- javac -version
If the command is not found, install the JDK:
- sudo apt install default-jdk
The JDK includes the compiler and is required for building Java applications.
JAVA_HOME is not set or points to the wrong locationSome tools fail to start if JAVA_HOME is missing or incorrectly configured.
Check whether JAVA_HOME is set:
- echo $JAVA_HOME
If the output is empty or incorrect, determine the correct Java path:
- sudo update-alternatives --config java
Copy the directory path without /bin/java and update JAVA_HOME accordingly. For example:
JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"
After updating the value, reload your environment or log out and log back in.
UnsupportedClassVersionErrorThis error usually means that an application was compiled with a newer Java version than the one currently installed.
The error message typically mentions a class file version number.
Check your Java version:
- java -version
If the version is older than required, install a newer OpenJDK release and switch to it using update-alternatives. For example, installing OpenJDK 21 resolves most version mismatch issues.
aptIf apt fails to install Java packages, the package index may be outdated or partially broken.
Update the package index:
- sudo apt update
If the problem persists, fix broken dependencies:
- sudo apt --fix-broken install
Then retry the installation.
If you installed Oracle JDK manually and java -version still shows OpenJDK, Oracle JDK may not be registered with update-alternatives.
Register the Oracle Java binaries and select them:
- sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-21/bin/java 3000
- sudo update-alternatives --config java
After selecting Oracle JDK, verify the change using java -version.
When troubleshooting is complete, always verify your setup using these commands:
- java -version
- javac -version
- echo $JAVA_HOME
All three should reflect the Java version you intend to use.
OpenJDK 21 is the default LTS version on Ubuntu 24.04. OpenJDK 21 provides long-term stability, regular security updates, and broad compatibility with modern Java frameworks and tools. If an application explicitly requires an older version, OpenJDK 17 is still supported and commonly used.
The Java Runtime Environment (JRE) allows you to run Java applications, but it does not include tools for building or compiling them. The Java Development Kit (JDK) includes the JRE plus development tools such as the Java compiler (javac). If you plan to develop Java applications, build projects, or run CI pipelines, you need the JDK. If you are unsure which one to choose, installing the JDK is the safer option.
OpenJDK and Oracle Java are based on the same Java specification and are functionally compatible for most use cases. OpenJDK is open source and is the version provided and maintained by Ubuntu. Oracle Java is distributed under a commercial license and may be required for certain vendor-supported products. For most users and workloads, OpenJDK is sufficient and recommended.
You can check the installed and active Java version by running the following command:
- java -version
This command displays the Java runtime version currently in use. To check the compiler version, you can run javac -version.
Yes, Ubuntu allows you to install multiple Java versions side by side. Each version is installed in its own directory under /usr/lib/jvm and does not overwrite the others. This setup is useful when different applications require different Java versions or when you want to test upgrades safely.
You can change the default Java version using the update-alternatives system. To select the default Java runtime, run:
- sudo update-alternatives --config java
To select the matching Java compiler, run:
- sudo update-alternatives --config javac
You should ensure that both java and javac point to the same version to avoid build or runtime issues.
JAVA_HOME set?Some Java-based tools and build systems rely on the JAVA_HOME environment variable to locate Java. While many modern tools can detect Java automatically, setting JAVA_HOME explicitly helps avoid configuration problems. If you use tools such as Maven, Gradle, or application servers, setting JAVA_HOME is recommended.
To remove a specific OpenJDK package, you can use apt. For example, to remove OpenJDK 21, run:
- sudo apt remove openjdk-21-jdk
If you want to remove unused dependencies as well, run:
- sudo apt autoremove
If multiple Java versions are installed, removing one version does not affect the others. You can verify the remaining installations by listing the contents of /usr/lib/jvm or checking the active version with java -version.
In this tutorial, you installed Java on an Ubuntu server using the apt package manager and learned how to manage Java installations effectively. You installed OpenJDK 21, explored the difference between the JRE and JDK, switched between Java versions using update-alternatives, configured the JAVA_HOME environment variable, and optionally installed Oracle JDK for specialized use cases.
For most users, OpenJDK provides everything needed to run and build Java applications with minimal effort and ongoing security updates. Oracle JDK remains an option when required, but it comes with additional licensing and maintenance considerations.
Your system is now ready to run Java applications and build Java projects with confidence.
For more detailed tutorials, check out the following articles:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Managed the Write for DOnations program, wrote and edited community articles, and makes things on the Internet. Expertise in DevOps areas including Linux, Ubuntu, Debian, and more.
With over 6 years of experience in tech publishing, Mani has edited and published more than 75 books covering a wide range of data science topics. Known for his strong attention to detail and technical knowledge, Mani specializes in creating clear, concise, and easy-to-understand content tailored for developers.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Thank you !
This is by far the best and most complete HowTo I have ever found on this topic. Great job Brian!
The only thing that is missing is the relation between the java versions and the javaws binary.
On my windows laptop running ‘javaws’ in a terminal gives me an info popup showing ‘Java Web Start 11.251.2.08-fcs’ (i assume that’s the version). When i do the same on my Ubuntu 20.04 VM i get ‘Java™ Web Start 1.6.0_45’
I know that the java version numbers are a bit special (how is that for an understatement ?! ;-) but i just cant get my head around the output i get from javaws.
Any ideas ?
How do you figure out the recv-keys is EA8CACC073C3DB2A?
[…] Next, import the signing key used to verify the software you’re about to install:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A
I kept getting the error mentioned here: sha256sum mismatch. When I ran the recommended command: sudo rm /var/lib/dpkg/info/oracle-java11-installer-local.postinst -f, I was finally able to follow your tutorial. So, I would echo the complimentary comments if you’re starting from scratch, but I had already attempted to install Java 11
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.