Install Java on Linux
Introduction
This chapter teaches you how to install JDK 21 (LTS) on Linux using distribution package managers, with optional SDKMAN for multiple versions. You will verify java and javac, set JAVA_HOME, and learn why project dependencies should not be dumped onto the system JDK. A correct Linux Java setup mirrors what you will see on servers and in CI pipelines.
Prerequisites
- A Linux machine (Ubuntu, Debian, Fedora, RHEL-family, or similar)
- Basic terminal knowledge and
sudofor package installation - Internet access to package repositories or SDKMAN
Choose the Right Java Version
Install JDK 21 (LTS) for this tutorial unless your team requires Java 17 or another LTS. Avoid legacy OpenJDK 8 for new learning unless you have a compatibility requirement.
Install a JDK package (includes javac), not a headless JRE-only package, when your distro splits them.
Install OpenJDK 21 with Your Package Manager
Debian and Ubuntu
# Update package index
sudo apt update
# Install JDK 21 (compiler + runtime)
sudo apt install -y openjdk-21-jdk
# Verify
java -version
javac -versionList installed Java alternatives (if update-alternatives is used):
update-alternatives --config java
update-alternatives --config javacFedora, RHEL, CentOS Stream, and similar
# Install JDK 21 development package
sudo dnf install -y java-21-openjdk-devel
# Verify
java -version
javac -versionOn older systems that use yum instead of dnf, replace dnf with yum if that is what your distro provides.
Tip
Best Practice
On servers, run sudo apt update or sudo dnf upgrade regularly so OpenJDK security patches stay current.
Set JAVA_HOME on Linux
Package managers install JDKs under paths such as /usr/lib/jvm/. Set JAVA_HOME to the JDK root (the directory that contains bin).
# Example: find JVM directories (Debian/Ubuntu)
ls /usr/lib/jvm/
# Set for current shell session (adjust path to your system)
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
export PATH="$JAVA_HOME/bin:$PATH"
# Persist in ~/.bashrc or ~/.zshrc
echo 'export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64' >> ~/.bashrc
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrcVerify:
echo $JAVA_HOME
java -version
javac -versionWarning
Do not install Maven/Gradle project dependencies globally into the system JDK’s extension directories. Use per-project build files (pom.xml, build.gradle) and isolated caches. Mixing global JARs with the OS Java install causes hard-to-debug classpath conflicts on shared machines.
Optional: SDKMAN for Multiple JDK Versions
SDKMAN installs user-local JDKs and switches versions without replacing the system OpenJDK used by OS tools.
# Install SDKMAN
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
# List available JDKs
sdk list java
# Install Temurin 21 (example; identifier may vary)
sdk install java 21.0.6-tem
# Use it in the current shell
sdk use java 21.0.6-tem
java -version
javac -versionSDKMAN is helpful when you maintain several projects on different LTS releases. For a single learning path, distro openjdk-21-jdk is enough.
Common Issues and Fixes
javac: command not found after installing java
- Cause: Installed a JRE or incomplete package (e.g.
default-jrewithout JDK). - Fix: Install
openjdk-21-jdk(Debian/Ubuntu) orjava-21-openjdk-devel(Fedora/RHEL).
Wrong Java version is default
- Cause: Multiple JDKs registered with
update-alternativesorPATHorder. - Fix: Run
update-alternatives --config javaand select 21, or setJAVA_HOMEexplicitly.
JAVA_HOME is empty in CI or SSH sessions
- Cause: Variables not set in non-interactive shells.
- Fix: Export
JAVA_HOMEin the CI job definition or~/.profile, not only in an interactive~/.bashrc.
What’s Next
With JDK 21 ready on Linux, Windows, or macOS, write your first program in the terminal: Write and Run Hello World.
FAQ
Should I use the system OpenJDK or SDKMAN?
For learning and most servers, the distro OpenJDK 21 package is the simplest default. Use SDKMAN when you need several JDK versions on one workstation without affecting system packages.
Is OpenJDK the same as “Oracle Java”?
OpenJDK is the open-source reference implementation. Vendors such as Eclipse Temurin and Oracle build distributions on top of it. For this tutorial, distro OpenJDK 21 and Temurin 21 are both suitable.
Why avoid global classpath hacks on Linux?
Production and CI environments expect dependencies declared per project. Global JARs create “works on my machine” failures when versions differ.
Which package name should I search for on Ubuntu?
Look for openjdk-21-jdk. Names like openjdk-21-jre alone may not include javac.
Can I develop Java in Docker instead of on the host?
Yes—many teams use container images with a fixed JDK 21. You still benefit from understanding host installation and JAVA_HOME when debugging locally.