Installing Java on Linux — OpenJDK 21 Setup Guide

2 minute read

In this chapter, you’ll install the Java Development Kit (JDK) on Linux and verify everything is working. By the end, you’ll compile and run your first Java program from the terminal.


JDK vs JRE — Which Do You Need?

Component What It Does Who Needs It
JRE (Java Runtime Environment) Runs Java programs End users
JDK (Java Development Kit) JRE + compiler + dev tools Developers (you!)

As a developer, you always install the JDK.

OpenJDK vs Oracle JDK

Feature OpenJDK Oracle JDK
Cost Free, open-source Free for dev, paid for production
Performance Identical Identical
Source Community + Oracle Oracle
Recommendation ✅ Use this Unnecessary for most developers

We’ll install OpenJDK 21 (the latest LTS version).


Installing on Ubuntu / Debian

Step 1: Update package list

$ sudo apt update

Step 2: Install OpenJDK 21

$ sudo apt install openjdk-21-jdk -y

Step 3: Verify installation

$ java -version

Expected output:

openjdk version "21.0.2" 2024-01-16
OpenJDK Runtime Environment (build 21.0.2+13-Ubuntu)
OpenJDK 64-Bit Server VM (build 21.0.2+13-Ubuntu, mixed mode, sharing)
$ javac -version

Expected output:

javac 21.0.2

java is the runtime (runs programs). javac is the compiler (compiles .java to .class). Both should show version 21.


Installing on Fedora / RHEL / CentOS

$ sudo dnf install java-21-openjdk-devel -y

Verify:

$ java -version
$ javac -version

Installing on Arch Linux

$ sudo pacman -S jdk-openjdk

Setting JAVA_HOME

Many tools (Maven, Gradle, IDEs) need the JAVA_HOME environment variable. Here’s how to set it:

Step 1: Find the JDK path

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

Example output:

/usr/lib/jvm/java-21-openjdk-amd64

Step 2: Add to your shell profile

For bash (~/.bashrc):

export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
export PATH="$JAVA_HOME/bin:$PATH"

For zsh (~/.zshrc):

export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
export PATH="$JAVA_HOME/bin:$PATH"

Step 3: Reload the profile

$ source ~/.bashrc    # or source ~/.zshrc

Step 4: Verify

$ echo $JAVA_HOME
/usr/lib/jvm/java-21-openjdk-amd64

Managing Multiple Java Versions

If you need multiple Java versions installed:

$ sudo update-alternatives --config java

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
  2            /usr/lib/jvm/java-21-openjdk-amd64/bin/java   2111      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Type the number of the version you want and press Enter.


Test with Hello World

Let’s verify everything works by compiling and running a simple program:

$ mkdir ~/java-projects && cd ~/java-projects
$ cat > HelloWorld.java << 'EOF'
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World! Java is working on Linux!");
    }
}
EOF
$ javac HelloWorld.java
$ java HelloWorld

Expected output:

Hello, World! Java is working on Linux!

If you see this, congratulations! Java is installed and working.


Common Issues and Fixes

Problem Solution
javac: command not found You installed JRE, not JDK. Install openjdk-21-jdk
java -version shows old version Run sudo update-alternatives --config java
JAVA_HOME is not set Add the export line to ~/.bashrc and run source ~/.bashrc
Permission denied Use sudo for installation commands

Summary

Step Command
Install JDK sudo apt install openjdk-21-jdk
Verify java -version and javac -version
Set JAVA_HOME Add to ~/.bashrc or ~/.zshrc
Test Compile and run HelloWorld.java

What’s Next?

Updated: