Blog · WebJi® India

Install Java on Ubuntu

Quick answer: On Ubuntu 24.04, Java installs with sudo apt update and sudo apt install default-jdk, which pulls OpenJDK 21. Check it with java -version and javac -version, then point JAVA_HOME at /usr/lib/jvm/java-21-openjdk-amd64. If apt says the package cannot be located, enable the universe repository and refresh.
✓ In business since 2005✓ INR + GST billing✓ 24/7 Email Support — avg reply < 60 min
Sealed cardboard moving boxes with a laptop in an office hallway, symbolising website migration — how to install java on ubuntu

Which Java package to install on Ubuntu

Java on Ubuntu is OpenJDK from the distribution repository. Install default-jdk when the server has to build or run a web app, and openjdk-21-jre-headless when it only has to run a jar. Neither needs a PPA.

  • default-jdk — Ubuntu 24.04 resolves it to OpenJDK 21, the runtime plus javac.
  • openjdk-21-jdk — the same version pinned by name, which you want when another server must stay on 17.
  • openjdk-21-jre-headless — runtime only, no compiler and no GUI libraries, the right pick for a jar behind nginx.
  • Oracle JDK — a licensed download that is not in apt; install it only when a contract names it.

A JRE cannot compile. If the deploy script runs javac or Maven, a runtime-only package fails the build with command not found, and the missing piece is the JDK, not the Java version.

Install OpenJDK with two apt commands

Log in to the server over SSH as a sudo user. Refresh the package index first, because a stale list is the usual reason apt cannot see the current OpenJDK.

  1. sudo apt update — pull the current package index for the release.
  2. sudo apt install default-jdk -y — installs OpenJDK 21 and javac on Ubuntu 24.04.
  3. sudo apt install openjdk-21-jre-headless -y — run this instead of step 2 on a runtime-only server.
  4. apt list --installed | grep -i openjdk — read the exact package names that landed before pinning anything.

Plan for a few hundred megabytes of disk for the JDK and less for the headless runtime. Ubuntu 22.04 still defaults to OpenJDK 11 unless you name a package, so default-jdk there is not 21.

Verify that the JDK is active

You know it worked when java -version prints openjdk version "21.0.x" and javac -version prints javac 21.0.x. A lower major number means an older JVM is still first on the path.

  1. java -version — the runtime Ubuntu will use.
  2. javac -version — proves a JDK is installed, not just a JRE.
  3. readlink -f $(which java) — should resolve to /usr/lib/jvm/java-21-openjdk-amd64/bin/java.

If javac is command not found while java works, a JRE is installed without the compiler. Add the matching -jdk package rather than a PPA, because a second provider for the same binary makes update-alternatives ambiguous later.

Install Java 17 or 21 side by side

Install the version by name and leave the default alone until the app is ready to move. Both packages can sit on one machine; update-alternatives decides which one answers.

  1. sudo apt install openjdk-17-jdk -y — adds 17 next to 21 without replacing it.
  2. sudo update-alternatives --config java — choose the number next to java-17-openjdk-amd64.
  3. sudo update-alternatives --config javac — repeat for the compiler, which is a separate link.
  4. java -version — confirm the switch in a new shell.

Skipping the third step is common. java moves to 17 while javac still compiles for 21, and the app dies at start with UnsupportedClassVersionError.

Set JAVA_HOME for shells and services

JAVA_HOME names the JVM folder, not the java binary. Build tools and Tomcat start scripts read it, and one wrong folder makes Maven report that no JDK is installed.

  1. ls -d /usr/lib/jvm/*/ — copy the exact folder name, including the version suffix.
  2. Add JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64" to /etc/environment for login shells.
  3. For a systemd unit, add Environment="JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64" under [Service], then sudo systemctl daemon-reload and restart the unit.
  4. Check where the app actually runs: echo $JAVA_HOME in a fresh SSH session, and systemctl show your-unit -p Environment for the service.

/etc/environment is read by PAM at login, not by systemd. Setting it there and restarting the unit changes nothing, which is why service logs keep showing the old Java.

Fix apt errors before you reinstall

Most install failures are package-name or repository problems. Rebuilding the server does not change either one.

MessageCauseFix
Unable to locate package openjdk-21-jdkuniverse is off, or that release pocket has no 21sudo add-apt-repository universe && sudo apt update, then retry
Could not get lock /var/lib/dpkg/lock-frontendunattended-upgrades is mid-runWait, check with ps aux | grep apt, never delete the lock
dpkg: error processing packagean earlier install was interruptedsudo dpkg --configure -a, then reinstall
java -version still shows 17another JVM earlier in PATH or alternativesreadlink -f $(which java), then reconfigure alternatives
UnsupportedClassVersionError on startjavac is newer than the runtimePoint java and javac at the same major version

A second PPA that ships the same Java package is the failure that takes longest to find. Remove it before adding another repository, so only one provider owns each binary.

How much server Java actually needs

Java is not the heavy part of the bill; the application is. A headless JRE and one small service run on 1 vCPU, 1 GB RAM and about 10 GB of disk. Add Tomcat with MySQL or PostgreSQL and the floor becomes 2 vCPU and 2 GB, or 4 GB for a busy store.

Set the heap explicitly with -Xms512m -Xmx1g and leave the rest of RAM to the page cache. A JVM told it may use the whole machine will do exactly that, and the kernel starts killing processes.

WebJi VPS hosting covers that range, and Java and Tomcat hosting covers the managed case where the JVM, the connector and the deploy are already configured. Both are quoted by email before payment.

When to email WebJi about a Java server

Email support@webji.services with the output of lsb_release -a, the exact apt or java line, and the unit name if a service fails. Support is 24/7 by email in Hindi and English, and the average first reply is under 60 minutes. The office phone is business hours only and is not a support line.

Frequently Asked Questions

How do I install Java on Ubuntu 24.04?

Run sudo apt update, then sudo apt install default-jdk. On 24.04 that installs OpenJDK 21 with javac. Confirm both with java -version and javac -version before you deploy anything.

Is the JDK the same thing as Java?

The JDK contains the JVM that runs Java plus the compiler and build tools. Installing default-jdk gives you both. A JRE alone runs programs but cannot compile them.

Why does apt say unable to locate package openjdk-21-jdk?

Either the universe repository is disabled or that Ubuntu release has no 21 in its pocket. On 24.04 run sudo add-apt-repository universe and sudo apt update. On 22.04 enable the updates pocket, then retry the install.

How do I set JAVA_HOME on Ubuntu?

Point it at the JVM folder, for example /usr/lib/jvm/java-21-openjdk-amd64, not at /usr/bin/java. Add it to /etc/environment for shells and to the unit's Environment= line for a systemd service, then reload the unit.

How do I switch between Java 17 and 21?

Run sudo update-alternatives --config java and pick the version, then repeat with --config javac. Both links must point at the same major version, or the build compiles for one JVM and runs on another.

Need help with this?

WebJi® support does it for you — free. Talk to our Jaipur team →