Installing Java on Ubuntu 22.04 – Quick Start for the Everyday Developer
If you’re trying to run a Spring Boot app, compile a Gradle project, or just want to play with Java on your desktop, installing Java on Ubuntu 22.04 is easier than it looks. This guide shows how to pull the right JDK from the official repos, double‑check that everything works, and gives you a quick trick for juggling multiple versions if you’re into that.
Choose the Right JDK
Most people are happy with OpenJDK because it’s free, open source, and fully supported by Ubuntu. I’ve seen folks run into license headaches when they accidentally install Oracle JDK from the vendor site; the installer is clunky and requires a separate key, so skip that unless you really need a specific Oracle feature.
Install Java via APT
sudo apt update sudo apt install -y default-jdk
The first command refreshes your package list, ensuring you pull the latest security patches. The second installs the “default” JDK – on 22.04 that’s OpenJDK 17, which is LTS and works with almost every Java framework out there. If you need a different version, replace default-jdk with openjdk-11-jdk, openjdk-18-jdk, etc.
Verify the Installation
java -version javac -version
You should see output that looks something like:
openjdk 17.0.8 2023‑07‑19 LTS OpenJDK Runtime Environment (build 17.0.8+7-LTS) OpenJDK 64‑bit Server VM (build 17.0.8+7-LTS, mixed mode, sharing)
If java or javac isn’t found, double‑check that the install succeeded and that /usr/bin is in your $PATH. A quick which java should return /usr/bin/java.
Set Up Multiple Versions (Optional)
For power users who need Java 8 for a legacy project and Java 17 for new code, Ubuntu’s update-alternatives makes it painless:
sudo update-alternatives --config java sudo update-alternatives --config javac
A numbered menu will appear; pick the one you want in your current shell session. If you need a more permanent switch, edit /etc/profile.d/java.sh to point JAVA_HOME at the version of your choice.
That’s it! You’re now ready to compile Java code on Ubuntu 22.04 – enjoy your new development environment.