Report this

What is the reason for this report?

How To Install Java with Apt on Ubuntu

Updated on February 5, 2026
English

Not using Ubuntu 20.04?
Choose a different version or distribution.
Ubuntu 20.04
How To Install Java with Apt on Ubuntu

Introduction

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:

  • Ubuntu 24.04 uses OpenJDK 21 as the default Java version, and it is the recommended Long-Term Support release for most users.
  • The default-jdk package installs both the Java runtime and development tools and is the best choice for most systems.
  • The default-jre package installs only the runtime and is suitable for systems that run prebuilt Java applications.
  • Ubuntu allows multiple Java versions to be installed side by side without conflicts.
  • The update-alternatives system controls which Java version is used by default for both java and javac.
  • Setting the JAVA_HOME environment variable helps prevent issues with build tools and application servers.
  • OpenJDK and Oracle JDK are compatible for most use cases, but Oracle JDK requires manual installation and license review.
  • Common Java installation issues are usually caused by version mismatches, missing JDK tools, or incorrect configuration paths.

Prerequisites

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.

Understanding default-jdk vs default-jre

Before 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.

JRE vs JDK: what is the difference?

At a high level, Java is split into two parts:

  • Java Runtime Environment (JRE): The JRE allows your system to run Java applications. It includes the Java Virtual Machine (JVM) and the core class libraries required to execute compiled Java programs.
  • Java Development Kit (JDK): The JDK includes everything in the JRE, plus development tools. These tools allow you to build, compile, debug, and package Java applications. The most common of these tools is the Java compiler, javac.

If you only have the JRE installed, you can run existing .jar files, but you cannot compile Java source code.

What Ubuntu’s default-* packages provide

Ubuntu 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.

When should you install default-jre?

Installing default-jre makes sense only in limited cases, such as:

  • A minimal system that only runs a single Java application
  • A container image where reducing package size matters
  • A production server with a locked-down runtime and no build steps

Even in these cases, many administrators still choose the JDK to avoid future limitations.

Why default-jdk is usually the better choice

For most users, default-jdk is the correct option, even if you do not plan to write Java code immediately for the following reasons:

  • Many tools assume javac is available, even when compiling happens indirectly
  • Build systems such as Maven and Gradle require the JDK
  • CI jobs can fail if only the JRE is installed
  • The JDK does not add meaningful overhead compared to the JRE

Installing 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:

  1. sudo apt update

Next, check whether Java is already installed:

  1. java -version

If Java is not installed, you will see a message indicating that the command cannot be found.

Output
Command '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:

  1. sudo apt install default-jdk

This installs OpenJDK 21 along with the required runtime libraries.

To verify the installation, run:

  1. java -version

You should see output similar to the following (the exact version may differ):

Output
openjdk 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:

  1. javac -version

Expected output:

Output
javac 21.0.9

Your system now has a complete Java development environment installed.

Installing a Specific OpenJDK Version

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.

Installing OpenJDK 21 explicitly

If you want to install OpenJDK 21 directly, rather than relying on default-jdk, run:

  1. sudo apt install openjdk-21-jdk

This installs the Java runtime and development tools for Java 21.

Installing OpenJDK 17 alongside 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:

  1. 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-amd64

Installing an additional Java version does not change the active version automatically. Ubuntu continues using the current default until you switch it.

Checking installed and active versions

To see which Java versions are installed, run:

  1. ls /usr/lib/jvm

To check which version is currently in use, run:

  1. java -version

Switching between installed versions is handled using update-alternatives, which is covered in the next section.

Managing Multiple Java Versions with update-alternatives

When 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.

Viewing available Java versions

To see which Java runtimes are available and which one is currently selected, run:

  1. sudo update-alternatives --config java

You will see output similar to the following:

Output
There 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.

Switching the default Java runtime

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:

  1. java -version

This command reports the Java version that is currently in use, not just the versions installed on the system.

Switching the Java compiler

If you are compiling Java code, you should also switch the Java compiler to match the runtime:

  1. sudo update-alternatives --config javac

Keeping java and javac aligned helps avoid confusing build or runtime issues, especially when working with multiple projects.

When to use update-alternatives

You typically need update-alternatives when:

  • You have multiple Java versions installed
  • An application requires a specific Java version
  • You are testing or upgrading Java across environments

If only one Java version is installed, update-alternatives is still present but does not require manual changes.

Setting the JAVA_HOME Environment Variable

Many 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.

Why JAVA_HOME matters

The 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.

Finding the correct Java installation path

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:

  1. 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

Setting JAVA_HOME system-wide

If 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:

  1. 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:

  1. source /etc/environment

You can verify that the variable is set correctly by running:

  1. echo $JAVA_HOME

The command should print the path you configured.

Setting JAVA_HOME for a single user

If 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.

Installing Oracle JDK (Optional)

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.

Downloading Oracle JDK

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.

Extracting and installing Oracle JDK

Change to the directory where the archive is located and extract it:

  1. 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:

  1. sudo mkdir -p /usr/lib/jvm
  2. sudo mv jdk-21* /usr/lib/jvm/

Placing Oracle JDK in this directory keeps it consistent with OpenJDK installations and simplifies version management.

Registering Oracle JDK with update-alternatives

To allow Ubuntu to manage Oracle JDK alongside OpenJDK, you must register it with update-alternatives.

Register the Java runtime:

  1. sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-21/bin/java 3000

Register the Java compiler:

  1. 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.

Selecting Oracle JDK as the default

If you want to use Oracle JDK as the active Java version, select it using update-alternatives:

  1. sudo update-alternatives --config java
  2. sudo update-alternatives --config javac

After making your selection, verify the active version:

  1. java -version
  2. 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.

Setting JAVA_HOME for Oracle JDK

If 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.

Ongoing maintenance considerations

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.

Common Errors and Troubleshooting

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 found

This 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:

  1. java -version

If the command is not found, install the default JDK:

  1. sudo apt update
  2. 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:

  1. sudo update-alternatives --config java

Select a valid Java path and verify again.

Java is installed, but the wrong version is used

This issue occurs when multiple Java versions are installed and the system is using a different version than expected.

Check the active Java version:

  1. java -version

If the version is not the one you want, change it using update-alternatives:

  1. sudo update-alternatives --config java
  2. sudo update-alternatives --config javac

Ensure that both java and javac point to the same version to avoid compatibility problems.

javac: command not found

This error means that only the Java Runtime Environment is installed, not the Java Development Kit.

Confirm whether the JDK is installed:

  1. javac -version

If the command is not found, install the JDK:

  1. 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 location

Some tools fail to start if JAVA_HOME is missing or incorrectly configured.

Check whether JAVA_HOME is set:

  1. echo $JAVA_HOME

If the output is empty or incorrect, determine the correct Java path:

  1. 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.

UnsupportedClassVersionError

This 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:

  1. 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.

Package installation errors with apt

If apt fails to install Java packages, the package index may be outdated or partially broken.

Update the package index:

  1. sudo apt update

If the problem persists, fix broken dependencies:

  1. sudo apt --fix-broken install

Then retry the installation.

Oracle JDK is installed but not detected

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:

  1. sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-21/bin/java 3000
  2. sudo update-alternatives --config java

After selecting Oracle JDK, verify the change using java -version.

Verifying your Java setup

When troubleshooting is complete, always verify your setup using these commands:

  1. java -version
  2. javac -version
  3. echo $JAVA_HOME

All three should reflect the Java version you intend to use.

FAQs

1. Which Java version should I install on Ubuntu 24.04?

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.

2. What is the difference between JDK and JRE?

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.

3. Is OpenJDK the same as Oracle Java?

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.

4. How do I check my Java version on Ubuntu?

You can check the installed and active Java version by running the following command:

  1. java -version

This command displays the Java runtime version currently in use. To check the compiler version, you can run javac -version.

5. Can I install multiple Java versions at once?

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.

6. How do I change the default Java version?

You can change the default Java version using the update-alternatives system. To select the default Java runtime, run:

  1. sudo update-alternatives --config java

To select the matching Java compiler, run:

  1. sudo update-alternatives --config javac

You should ensure that both java and javac point to the same version to avoid build or runtime issues.

7. Do I need 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.

8. How do I uninstall Java from Ubuntu?

To remove a specific OpenJDK package, you can use apt. For example, to remove OpenJDK 21, run:

  1. sudo apt remove openjdk-21-jdk

If you want to remove unused dependencies as well, run:

  1. 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.

Conclusion

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.

Learn more about our products

About the author(s)

Brian Hogan
Brian Hogan
Author
See author profile

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.

Manikandan Kurup
Manikandan Kurup
Editor
Senior Technical Content Engineer I
See author profile

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.

Still looking for an answer?

Was this helpful?


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

Thank you for writing this article, helpful :)

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.