How to Install OpenJDK on Linux?

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:

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:

Fedora:

CentOS and RHEL (Red Hat Enterprise Linux):

Arch Linux:

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:

Fedora:

CentOS and RHEL (Red Hat Enterprise Linux):

Arch Linux:

Step 3 – Check the Available OpenJDK Versions

Check the available OpenJDK versions that you can install.

Ubuntu and Debian:

Fedora:

CentOS and RHEL (Red Hat Enterprise Linux):

Arch Linux:

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:

Fedora:

CentOS and RHEL (Red Hat Enterprise Linux):

Arch Linux:

Step 5 – Verify the Installation

Once the installation is complete, verify that Java has been successfully installed by checking its version.

You should see an output similar to this:

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:

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:

For Fedora/CentOS, use:

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

For Zsh (~/.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!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *