How To Update Java On Linux – Using Apt Package Manager

Checking your Java version first helps you decide which update method to use on Linux. Knowing how to update Java on Linux keeps your system secure and your applications running smoothly.

Java updates are important for security patches and new features. Linux users have several ways to update Java, from package managers to manual downloads. This guide covers all major methods.

Let’s start by checking what you currently have installed.

Check Your Current Java Version

Open a terminal window. Run this command:

java -version

You’ll see output like:

openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12)
OpenJDK 64-Bit Server VM (build 17.0.1+12, mixed mode, sharing)

Write down the version number. This tells you if you have OpenJDK or Oracle Java. It also shows the major version number.

If you get a “command not found” error, Java isn’t installed yet. You can skip to the installation section.

How To Update Java On Linux

Now we cover the main update methods. Choose the one that fits your setup.

Update Java Using The Package Manager

This is the easiest method for most users. Your distribution’s package manager handles everything.

On Debian Or Ubuntu Systems

Update your package list first:

sudo apt update

Then upgrade Java:

sudo apt install --only-upgrade default-jdk

If you need a specific version, install it directly:

sudo apt install openjdk-17-jdk

Replace “17” with your desired version number. Common versions are 11, 17, and 21.

On Red Hat Or Fedora Systems

For Fedora, use dnf:

sudo dnf update java-*-openjdk

For RHEL or CentOS, use yum:

sudo yum update java-*-openjdk

To install a specific version on Fedora:

sudo dnf install java-17-openjdk

On Arch Linux Systems

Update all packages including Java:

sudo pacman -Syu

Or install a specific version:

sudo pacman -S jdk17-openjdk

Update Java Manually From The Official Website

Sometimes you need Oracle Java or a specific build. This method gives you full control.

  1. Go to the official Oracle Java downloads page.
  2. Choose the version you need (Java 17 LTS is recommended).
  3. Download the Linux tar.gz file for your architecture (x64 or ARM).
  4. Open a terminal and navigate to your Downloads folder:
cd ~/Downloads
  1. Extract the archive:
tar -xzf jdk-17_linux-x64_bin.tar.gz
  1. Move the extracted folder to /usr/lib/jvm:
sudo mv jdk-17 /usr/lib/jvm/
  1. Update the alternatives system:
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1
  1. Set it as the default:
sudo update-alternatives --config java

Select the number for your new Java version. Verify with java -version.

Using SDKMAN To Manage Java Versions

SDKMAN is a tool for managing multiple Java versions. It’s great for developers.

Install SDKMAN first:

curl -s "https://get.sdkman.io" | bash

Open a new terminal or run:

source "$HOME/.sdkman/bin/sdkman-init.sh"

List available Java versions:

sdk list java

Install a specific version:

sdk install java 17.0.9-tem

Switch between versions:

sdk use java 17.0.9-tem

Set a default version:

sdk default java 17.0.9-tem

SDKMAN makes it easy to test different Java versions without system-wide changes.

Update Java Using Flatpak Or Snap

Some distributions use Flatpak or Snap for package management.

Flatpak Method

Check if Flatpak is installed:

flatpak --version

Install the OpenJDK Flatpak:

flatpak install flathub org.openjdk.jdk

Update it:

flatpak update org.openjdk.jdk

Snap Method

Install the Snap version of Java:

sudo snap install openjdk

Update it:

sudo snap refresh openjdk

These methods keep Java isolated from your system packages.

Set The Java Home Environment Variable

Many applications need the JAVA_HOME variable set. This points to your Java installation directory.

Find your Java path:

dirname $(dirname $(readlink -f $(which java)))

This returns something like /usr/lib/jvm/java-17-openjdk-amd64.

Add it to your shell profile. For bash users:

echo 'export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64' >> ~/.bashrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

For zsh users, replace .bashrc with .zshrc.

Verify it worked:

echo $JAVA_HOME

Common Issues And Fixes

You might run into problems during the update. Here are solutions for frequent issues.

Java Command Not Found After Update

This usually means the symbolic link is broken. Reinstall Java using your package manager. Or manually update the alternatives:

sudo update-alternatives --config java

Wrong Java Version Showing

Multiple Java versions can conflict. Check all installed versions:

update-java-alternatives --list

Then set the correct one:

sudo update-java-alternatives --set java-1.17.0-openjdk-amd64

Permission Denied Errors

You need root privileges for system-wide updates. Always use sudo for package manager commands. For manual installations, ensure the files are owned by root:

sudo chown -R root:root /usr/lib/jvm/jdk-17

Java Not Working In Browser

Modern browsers don’t support Java applets. This is by design. Consider using standalone Java applications instead.

Verify The Update Was Successful

After updating, always verify the installation.

Run these commands:

java -version
javac -version

Both should show the new version number. Also check that your applications work correctly. Test a simple Java program:

public class Test {
    public static void main(String[] args) {
        System.out.println("Java works!");
    }
}

Compile and run:

javac Test.java
java Test

You should see “Java works!” printed.

Keep Java Updated Automatically

Set up automatic updates for your system. This ensures Java stays current.

On Ubuntu, enable unattended upgrades:

sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades

On Fedora, enable automatic updates:

sudo dnf install dnf-automatic
sudo systemctl enable --now dnf-automatic.timer

On Arch, use pacman’s timer:

sudo systemctl enable --now pacman-filesdb-refresh.timer

These services update all packages, including Java, automatically.

Frequently Asked Questions

How Do I Update Java On Linux Without Internet?

Download the Java package on a machine with internet. Transfer it via USB. Then install using dpkg or rpm locally. For manual installs, copy the tar.gz file and extract it.

Can I Have Multiple Java Versions On Linux?

Yes. Use update-alternatives to switch between them. SDKMAN also manages multiple versions easily. You can set different versions for different projects.

What Is The Difference Between OpenJDK And Oracle Java?

OpenJDK is open-source and free. Oracle Java has additional features and commercial support. For most users, OpenJDK works fine. Oracle Java is needed for some enterprise applications.

How Often Should I Update Java On Linux?

Update when security patches are released. This happens every 3-6 months for LTS versions. Check for updates monthly. Enable automatic updates for convenience.

Why Does My Java Update Fail On Linux?

Common reasons include: insufficient permissions, broken package manager, or conflicting repositories. Try cleaning the package cache first. On Debian: sudo apt clean. On Fedora: sudo dnf clean all.

Final Tips For Java Updates

Always backup your system before major updates. Use Timeshift or similar tools for snapshots.

Check the official Java release notes for breaking changes. Some old applications may not work with newer Java versions.

For production servers, test updates in a staging environment first. This prevents downtime.

Keep your package manager updated. Run system updates regularly to get the latest Java versions.

If you use containers, update the base image to get the new Java version. Rebuild your containers after the update.

Java updates on Linux are straightforward once you know the methods. Choose the approach that matches your workflow. Package managers are best for most users. Manual installs give you more control. SDKMAN is ideal for developers managing multiple projects.

Remember to verify your update and set JAVA_HOME correctly. This ensures all your Java applications work as expected.

Staying current with Java updates keeps your system secure and performant. Make it a regular part of your maintenance routine.