How to Install Java JDK 21 on Ubuntu and Linux
Why the ‘temurin-21-jdk’ Package Isn’t Found
When you run sudo apt install temurin-21-jdk without first adding the Adoptium repository, Ubuntu’s package manager has no idea where to find the package. Eclipse Temurin (formerly AdoptOpenJDK) is not included in Ubuntu’s default repositories, so you need to explicitly add it before installation.
Method 1: Install Eclipse Temurin JDK 21 (Recommended for Most Users)
Eclipse Temurin is a reliable, enterprise-grade build of OpenJDK maintained by the Adoptium project. Follow these steps:
- Add the Adoptium GPG key:
wget -qO - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo tee /etc/apt/keyrings/adoptium.asc - Add the Adoptium repository:
echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/adoptium.list - Update your package list and install Temurin JDK 21:
sudo apt update sudo apt install temurin-21-jdk
Note for Ubuntu 26.04 users: Adoptium has not yet published packages for Ubuntu 26.04. See Method 2 below as an alternative.
Method 2: Install OpenJDK 21 from Ubuntu Repository (Simpler Alternative)
If you prefer to use the distro-managed version of Java, Ubuntu’s official repositories include OpenJDK 21 on Ubuntu 24.04 and later:
sudo apt update
sudo apt install openjdk-21-jdk
This installs the same underlying JDK but is simpler since no additional repository configuration is needed. The trade-off is that updates come from Ubuntu’s release cycle rather than directly from the Eclipse Adoptium project.
Ubuntu 22.04 users: OpenJDK 21 is available, but only in the Universe repository. Enable it first if needed, or use Method 1 with Adoptium instead.
Verifying Your Installation
After installation completes, verify it worked by checking the Java version:
java -version
You should see output showing Java 21 and the JDK build information. You can also verify the compiler is installed:
javac -version
Setting JAVA_HOME (Optional but Recommended)
Many build tools like Maven and Gradle rely on the JAVA_HOME environment variable. Find your JDK installation path and add it to your shell profile:
export JAVA_HOME=$(update-alternatives --query java | grep 'Best:' | awk -F ' ' '{print $NF}')
export PATH="$JAVA_HOME/bin:$PATH"
Add these lines to your ~/.bashrc, ~/.zshrc, or equivalent shell profile file, then reload it with source ~/.bashrc.
Keeping Your Installation Updated
Once installed, your JDK receives security and patch updates automatically through your normal apt update and apt upgrade cycle. You can specifically update just Java with:
sudo apt install --only-upgrade temurin-21-jdk
Choosing Between Temurin and OpenJDK
Both are valid Java implementations. Choose Temurin if you want updates directly from the Adoptium project, or choose OpenJDK if you prefer Ubuntu-maintained packages with a consistent release schedule. For most development use cases, either works equally well.
Sources
- adoptium.net
- adoptium.net
- linuxcapable.com
- computingforgeeks.com
- digitalocean.com
- theserverside.com
- tecmint.com
- hostman.com
