Install Java on Linux
| Introduction | |
| Install a JDK | |
| Pick a Java version | |
| JAVA_HOME | |
| Check the version |
Introduction
On Linux the easiest JDK comes from the distribution repositories. The package is usually called something like openjdk-17-jdk or java-17-openjdk-devel, depending on the distro.
Install a JDK
Debian and Ubuntu:
sudo apt update && sudo apt install -y openjdk-17-jdk
Rocky Linux and RHEL:
sudo dnf install -y java-17-openjdk-devel
Pick a Java version
Several JDKs can live side by side. List them and switch the default with the alternatives system:
sudo update-alternatives --config java
To install an older release, for example Java 8, ask for its package explicitly instead of the default one:
sudo apt install -y openjdk-8-jdk
JAVA_HOME
Build tools (Maven, Gradle) find the JDK through the JAVA_HOME variable. Point it at the installed JDK:
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
To make it permanent, add the same line to ~/.bashrc or ~/.profile.
Check the version
java -version
A JDK answers with both the runtime and the compiler versions. javac -version must report the same release.