Updating Java on Linux is essential to ensure you’re using the latest security features and optimizations. This guide provides an easy-to-follow process for updating Java on popular Linux distributions such as Ubuntu, Debian, CentOS, and Fedora. Whether you’re using OpenJDK or Oracle JDK, this guide will help you stay up-to-date with the latest Java version.
Step 1: Check Your Current Java Version
Before you update Java, it’s essential to know the version currently installed on your system. You can do this with the following command:
java -version
This command will display the current Java version installed on your system. For example, you may see something like:
openjdk version "11.0.11" 2021-04-20
If Java is not installed, you’ll get an error like:
bash: java: command not found
In that case, you can install Java first before attempting to update it.
Step 2: Update Java on Ubuntu and Debian
Install OpenJDK (if not already installed)
If Java is not installed or you wish to install a specific version of OpenJDK (such as Java 11 or Java 17), you can install it using the apt package manager. Here’s how you can install OpenJDK:
sudo apt update
sudo apt install openjdk-11-jdk
Replace openjdk-11-jdk with the specific version you’d like to install (e.g., openjdk-17-jdk for Java 17). To check available versions, you can run:
apt search openjdk
Update Java to the Latest Version
To update Java to the latest version available in the Ubuntu or Debian repository, run the following commands:
sudo apt update
sudo apt upgrade openjdk-11-jdk
If you want to upgrade to a newer version, you can use the following command:
sudo apt install openjdk-17-jdk
Once the installation completes, verify the update by checking the Java version again:
java -version
Set the Default Java Version
If you have multiple versions of Java installed on your system, you can set a default version using the update-alternatives command. For example, to set Java 11 as the default:
sudo update-alternatives --config java
This will prompt you to select the default Java version. Choose the number corresponding to the version you want to use, and press Enter.
Step 3: Update Java on CentOS, Fedora, or RHEL
Install OpenJDK (if not already installed)
For CentOS, Fedora, or RHEL, OpenJDK can be installed using the dnf or yum package manager:
sudo dnf install java-11-openjdk-devel
To install Java 17, use:
sudo dnf install java-17-openjdk-devel
Update Java to the Latest Version
If OpenJDK is already installed, you can upgrade it to the latest version using the following command:
sudo dnf upgrade java-11-openjdk-devel
To upgrade to a newer version of Java (such as Java 17), use:
sudo dnf install java-17-openjdk-devel
After installation, verify the Java version with:
java -version
Set the Default Java Version
To set the default Java version on CentOS, Fedora, or RHEL, use the alternatives command:
sudo alternatives --config java
You’ll be prompted to select the default version of Java. Enter the number corresponding to the version you want to use.
Step 4: Install Oracle Java (Optional)
Although OpenJDK is the default Java implementation for most Linux distributions, you might want to use Oracle’s JDK. To install the latest version of Oracle JDK, you need to manually download it from the Oracle website:
-
- Go to the Oracle JDK Downloads page.
- Download the appropriate tar.gz file for your system architecture.
- Extract the file in your preferred directory:
tar -xvzf jdk-17_linux-x64_bin.tar.gz
-
- Move it to the /usr/lib/jvm directory for system-wide use:
sudo mv jdk-17 /usr/lib/jvm/
Finally, update the alternatives system to register Oracle Java as the default:
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1
Then use the –config command to select it as the default Java:
sudo update-alternatives --config java
Step 5: Verify the Installation
Once you’ve updated Java, verify that the correct version is now active using:
java -version
If everything was successful, you should see the updated Java version. You can also check the installation path:
which java
This will show you the path to the Java executable, confirming the correct version is being used.
Common Issues and Solutions
1. Java Not Found After Installation
If you get an error saying that Java is not found after installation, make sure that the Java binary is added to the system’s PATH. You can check this by typing:
echo $PATH
If Java isn’t listed, you can manually add it to the PATH in the ~/.bashrc or ~/.bash_profile file:
export PATH=/usr/lib/jvm/jdk-17/bin:$PATH
Then reload the profile:
source ~/.bashrc
2. Multiple Java Versions Installed
If you have multiple Java versions installed, use update-alternatives or alternatives (depending on your Linux distribution) to manage which version is set as the default.
3. Error: “Failed to open JVM” (JVM Error)
If you encounter errors like “Failed to open JVM,” ensure that your Java installation is not corrupted. Try reinstalling the JDK or switching to a different version.
FAQs
1. How do I check which Java version is installed?
Use the following command:
java -version
This will show the installed Java version on your system.
2. How can I install Java on Linux?
To install Java on Linux, you can use the package manager for your distribution (apt, dnf, or yum) or manually download the Oracle JDK.
3. Can I use multiple versions of Java on Linux?
Yes, you can install multiple versions and use update-alternatives or alternatives to manage the default Java version.
4. How do I uninstall a Java version on Linux?
To uninstall Java, use the following command:
sudo apt remove openjdk-11-jdk
Replace openjdk-11-jdk with the version you wish to remove.
Conclusion
Updating Java on Linux is straightforward, and with the right steps, you can keep your system up-to-date with the latest version of Java. Whether you’re working with OpenJDK or Oracle JDK, the installation and update processes are simple to follow using your distribution’s package manager or manual installation steps. Always remember to verify your Java version after installation and use the appropriate commands to set the default Java version.