Install Java on Ubuntu
To install Java on Ubuntu 24.04, run sudo apt update and then sudo apt install default-jdk, which installs OpenJDK 21 with the javac compiler. Confirm it with java -version and javac -version. Use openjdk-21-jre-headless on servers that only run jars, and set JAVA_HOME to /usr/lib/jvm/java-21-openjdk-amd64.
For developers and server admins setting up Java on an Ubuntu VPS for Spring Boot, Tomcat, Jenkins or a game server.
knowledge
- Read time
- 4 min
- Sections
- 8
- Updated
- 14 Nov 2026
- Written by
- WebJi® team
Last editorial update: 14 November 2026. Prices follow the dated sources on this page; checkout confirms current charges.
Which Java package should you install on Ubuntu?
Install default-jdk when the server must compile or build Java, and a headless runtime such as openjdk-21-jre-headless when it only runs a ready-made jar or WAR. Both come from Ubuntu's own repositories; no PPA is needed.
| Package | Contains | Choose it when |
|---|---|---|
default-jdk | The release's default JDK: OpenJDK 21 on 24.04, OpenJDK 11 on 22.04 | You want Ubuntu's recommended version and javac |
openjdk-21-jdk | OpenJDK 21 runtime, compiler and tools | The app or build tool needs exactly Java 21 |
openjdk-17-jdk | OpenJDK 17, the previous LTS | An older Spring Boot 2.x or vendor app requires 17 |
openjdk-21-jre-headless | Runtime only, no compiler, no GUI libraries | A server that only runs java -jar or Tomcat |
Oracle JDK is a separate download under Oracle's licence and is not in apt; install it only if your vendor contract names it. A JRE cannot compile, so a deploy script that calls javac or Maven fails with "command not found" until a JDK package is present.
How do you install OpenJDK on Ubuntu 24.04?
Refresh the package index, then install the package; on Ubuntu 24.04 default-jdk pulls OpenJDK 21. Run these over SSH as a user with sudo rights.
sudo apt update
sudo apt install -y default-jdk
# runtime-only server instead:
sudo apt install -y openjdk-21-jre-headless
# see exactly what landed
dpkg -l | grep -i openjdk
Check your release first with lsb_release -a. On 22.04, default-jdk installs Java 11, so name openjdk-21-jdk or openjdk-17-jdk explicitly if the app needs a newer version. No sudo user yet? Create one before installing anything as root.
How do you check which Java version is active?
Run java -version and javac -version; on a 24.04 default install both report 21.0.x. If javac is missing while java works, only a JRE is installed.
java -version
javac -version
readlink -f "$(which java)"
# /usr/lib/jvm/java-21-openjdk-amd64/bin/java (arm64 servers: java-21-openjdk-arm64)
The readlink line shows which JVM folder the java command really resolves to, which you need for JAVA_HOME below.
How do you run Java 17 and Java 21 side by side on Ubuntu?
Install each version by package name and choose the active one with update-java-alternatives, which switches java, javac and the other tools together. Both versions can stay installed on one server.
sudo apt install -y openjdk-17-jdk
update-java-alternatives -l
# java-1.17.0-openjdk-amd64 1711 /usr/lib/jvm/java-1.17.0-openjdk-amd64
# java-1.21.0-openjdk-amd64 2111 /usr/lib/jvm/java-1.21.0-openjdk-amd64
sudo update-java-alternatives -s java-1.17.0-openjdk-amd64
If you use sudo update-alternatives --config java instead, repeat it for javac; switching only one leaves the compiler and runtime on different versions, and the app fails at start with UnsupportedClassVersionError. For a single service, it is often simpler to leave the system default alone and point that service's JAVA_HOME at the version it needs.
How do you set JAVA_HOME on Ubuntu for shells and systemd services?
Set JAVA_HOME to the JVM folder, such as /usr/lib/jvm/java-21-openjdk-amd64, in /etc/environment for login shells and in the unit file for a systemd service. These are separate: systemd services do not read /etc/environment.
# 1. find the folder
ls -d /usr/lib/jvm/*/
# 2. login shells: add this line to /etc/environment (no "export", no $VARS)
JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"
# 3. a service: sudo systemctl edit myapp.service, then add
[Service]
Environment="JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64"
# 4. apply and confirm
sudo systemctl daemon-reload && sudo systemctl restart myapp
systemctl show myapp -p Environment
/etc/environment is read by PAM at login, so open a new SSH session before testing with echo $JAVA_HOME. Tomcat setups covered in deploying a WAR file to Tomcat use the same Environment= approach.
How do you fix common Java install errors on Ubuntu?
Most failures are a disabled repository, a dpkg lock held by automatic updates, or two JVMs fighting over the same command. None of them needs a reinstall of the server.
| Message | Cause | Fix |
|---|---|---|
| Unable to locate package openjdk-21-jdk | The universe component is disabled (common on minimal images) or the index is stale | sudo add-apt-repository universe && sudo apt update |
| Could not get lock /var/lib/dpkg/lock-frontend | unattended-upgrades is running | Wait a few minutes; never delete the lock file |
| dpkg: error processing package | An earlier install was interrupted | sudo dpkg --configure -a, then retry |
| java -version shows the old version | Alternatives still point at another JVM | sudo update-java-alternatives -s … |
| UnsupportedClassVersionError | Code compiled for a newer Java than the runtime | Run on the same or newer major version |
If add-apt-repository itself is missing, install software-properties-common first.
How much server does a Java application need?
The JVM is light; the application and its heap decide the RAM, and by default the JVM sizes its maximum heap at a quarter of the machine's memory. Set the heap explicitly, for example -Xms512m -Xmx1g, so Java, the database and the page cache all fit.
A Spring Boot API plus MySQL or PostgreSQL is comfortable on a VPS with 2 vCPU and 4 GB RAM, which matches the VPS-Start sample on WebJi® VPS hosting, from ₹499 a month before 18% GST. If you would rather have Tomcat, Nginx on port 443 and the restart path set up for you, Java and Tomcat hosting is quoted in INR. For help, email support@webji.services with the output of lsb_release -a and the exact error; the team replies 24/7 in Hindi and English, on average within 60 minutes.
Frequently asked questions
Which Java LTS version should a new project use on Ubuntu?
Java 21 is the LTS that Ubuntu 24.04 ships as its default, and current Spring Boot 3 releases support it. Choose 17 only if a dependency or vendor app still requires it, and plan to move up when that dependency allows.
How do I uninstall OpenJDK from Ubuntu?
Remove the version by name, for example sudo apt purge openjdk-17-jdk openjdk-17-jre-headless, then run sudo apt autoremove to clear dependencies. Check java -version afterwards; alternatives will point to whichever JVM remains.
Do I need Java to run Tomcat, Jenkins or Minecraft on Ubuntu?
Yes, all three run on the JVM. A headless runtime is enough for Tomcat and a Minecraft server; Jenkins also needs only a runtime unless it compiles Java builds on the same machine. Check each project's supported Java version before installing.
Is OpenJDK free for commercial use?
Yes. Ubuntu's OpenJDK packages are licensed under the GPL with the Classpath Exception, so businesses can run commercial applications on them without a licence fee. Oracle JDK is a different product with its own licence terms.
Commands checked on Ubuntu 24.04 LTS and 22.04 LTS package lists on 14 November 2026 · Reviewed by the WebJi® team, Jaipur.
Ready to get started with WebJi®?
Join 12,000+ businesses across India and overseas. INR pricing, 18% GST shown before payment and 24/7 email support in Hindi and English — average first reply under 60 minutes.