If you need Java on your Linux system, installing OpenJDK (Open Java Development Kit) is the way to go. OpenJDK is the open-source version of Java, and it’s essential for developers, system administrators, and anyone running Java applications. The good news is that installing OpenJDK on Linux is not complicated at all, no matter which distribution you’re using. In this guide, we’ll break down the installation steps for different Linux distros, so you can get started quickly without any hassle. Whether you’re setting up a new development environment or just need Java for a specific task, this guide will help you install OpenJDK on Linux with ease.
Checking If Java Is Already Installed
Before installing OpenJDK on Linux, it’s good to check whether you already have Java installed. Open a terminal and run:
java -version
If you see something like Command java not found
, then you need to install OpenJDK. Otherwise, it will show the installed version.
Installing OpenJDK on Different Linux Distros
Step 1 – Update the Package List
Before you install OpenJDK on Linux, update your package list first to ensure you have the latest information about available packages and their versions.
Ubuntu and Debian:
sudo apt update
Fedora:
sudo dnf update –y
CentOS and RHEL (Red Hat Enterprise Linux):
sudo yum update –y
Arch Linux:
sudo pacman –Sy
Step 2 – Upgrade the Package Manager
Upgrade your package manager to make sure all installed packages and dependencies are up to date, preventing potential conflicts.
Ubuntu and Debian:
sudo apt upgrade –y
Fedora:
sudo dnf upgrade –y
CentOS and RHEL (Red Hat Enterprise Linux):
sudo yum upgrade –y
Arch Linux:
sudo pacman -Syu
Step 3 – Check the Available OpenJDK Versions
Check the available OpenJDK versions that you can install.
Ubuntu and Debian:
sudo apt search openjdk
Fedora:
sudo dnf search openjdk
CentOS and RHEL (Red Hat Enterprise Linux):
sudo yum search openjdk
Arch Linux:
pacman -Ss openjdk
Step 4 – Install OpenJDK
Install the OpenJDK version of your choice (version 21, for example) using the package manager, which will download and set up the necessary Java components.
Ubuntu and Debian:
sudo apt install openjdk-21-jdk -y
Fedora:
sudo dnf install java-21-openjdk –y
CentOS and RHEL (Red Hat Enterprise Linux):
sudo yum install java-21-openjdk -y
Arch Linux:
sudo pacman -S jdk21-openjdk
Step 5 – Verify the Installation
Once the installation is complete, verify that Java has been successfully installed by checking its version.
java –version
You should see an output similar to this:
openjdk version "21.0.9" 2025-01-17
OpenJDK Runtime Environment (build 17.0.9+11-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 17.0.9+11-Ubuntu-0ubuntu120.04, mixed mode, sharing)
Setting the Default Java Version (If Multiple Versions Are Installed)
If you have multiple Java versions installed, you may need to set the default one. Use this command:
sudo update-alternatives --config java
This command will display a list of all installed Java versions on your system, allowing you to choose the default one you want to use. Choose the one you want as the default by entering the corresponding number.
To set the default javac
compiler version:
sudo update-alternatives --config javac
For Fedora/CentOS, use:
sudo alternatives --config java
Setting JAVA_HOME Environment Variable
Some applications need the JAVA_HOME variable set. Find the Java installation path with:
readlink -f $(which java)
Once you have identified the correct Java installation path, add it to your shell configuration file so that the system can recognize and use it automatically.
For Bash (~/.bashrc or ~/.bash_profile
):
echo 'export JAVA_HOME=/usr/lib/jvm/java-21-openjdk' >> ~/.bashrc
source ~/.bashrc
For Zsh (~/.zshrc
):
echo 'export JAVA_HOME=/usr/lib/jvm/java-21-openjdk' >> ~/.zshrc
source ~/.zshrc
Replace /usr/lib/jvm/java-21-openjdk
with your actual Java installation path.
Conclusion
When you install OpenJDK on Linux, you’re setting up a powerful Java environment that makes it simple to develop, test, and run Java applications. Each Linux distribution has its own method for handling package management, but once you get OpenJDK installed, you’ll have everything you need to start working with Java. Keeping your setup updated ensures you always have access to the latest features and security improvements. Now that OpenJDK is ready to go, it’s time to bring your ideas to life and start coding something awesome!