The command is sudo apt install default-jdk and it finishes in under a minute. The part that actually matters is the three decisions around it: OpenJDK versus Oracle, which version to pin, and whether you need the full JDK or just the runtime. On a server, get those wrong and you either ship a licensing headache, build against a JDK your production box does not have, or install a 300 MB toolchain to run a single jar. Nobody tells you those parts, so they are where people lose the afternoon.
Installing Java is easy. Installing the right Java, so that the version you build against and the version that runs your app are the same one, is the bit worth thinking about for ten seconds.
Table of contents
- The one command, and what it installs
- OpenJDK or Oracle: why default-jdk is almost always right
- Picking a version: 17, 21, or whatever default gives you
- JRE vs JDK on a server
- Setting JAVA_HOME and switching versions
- How this fits the rest of the stack
- FAQ
The one command, and what it installs
For most people, this is the whole job:
sudo apt update
sudo apt install default-jdk
default-jdk is a meta-package that pulls in the OpenJDK version Ubuntu considers current for your release - OpenJDK 21 on Ubuntu 24.04, OpenJDK 17 on 22.04. It includes both the compiler (javac) and the runtime (java), so you can build and run.
Verify it landed:
java -version
javac -version
Both should print the same major version. If java works but javac does not, you installed a JRE-only package somewhere along the way - install default-jdk and the two line up.
That is the entire happy path. The rest of this is about not painting yourself into a corner with it.
OpenJDK or Oracle: why default-jdk is almost always right
There are two Javas you can install, and the difference is licensing, not the language.
- OpenJDK is the open-source reference implementation. It is what
aptgives you, it is free for any use including production and commercial, and it is what the vast majority of the industry runs. This is the correct default. - Oracle JDK requires downloading from Oracle, agreeing to their licence, and - depending on the version and how you use it - potentially paying. There is almost no technical reason a normal web app needs it.
Unless a specific vendor tells you in writing that they only support Oracle JDK, install OpenJDK and never think about it again. The performance is the same; they share most of the same code. Choosing Oracle JDK to “be safe” is how teams end up with a licence-compliance conversation they did not need to have.
Picking a version: 17, 21, or whatever default gives you
Java versions matter because the JVM is strict about it: a jar compiled for Java 21 will not run on a Java 17 runtime. It throws UnsupportedClassVersionError and stops.
The practical rules:
- Match production. Whatever your deployment target runs, build against the same major version. This is the single most common cause of “works on my machine”.
- Prefer an LTS release. Java 17 and Java 21 are long-term-support versions with years of updates. Java 22 and 23 are fine to develop on but drop out of support quickly.
- To install a specific version rather than the distro default:
sudo apt install openjdk-21-jdk
Swap 21 for 17 if that is what production runs. Being explicit here is better than trusting default-jdk to mean the same thing across two Ubuntu releases - it does not.
JRE vs JDK on a server
The JDK is the development kit: compiler, debugger, the full toolchain. The JRE is just the runtime - enough to run a compiled jar, nothing to build one.
On a build machine or a dev box, you want the JDK. On a plain production server whose only job is to run an already-built jar, the JRE is enough and it is smaller:
sudo apt install default-jre
In practice, most modern Java apps ship as a self-contained jar built by your CI, and the server just runs java -jar app.jar. That server needs a JRE, not a JDK. Installing the full JDK there is not wrong, it is just more surface area than the box needs. If you are running containers, this is why the -jre base images exist and are the right default for the final stage of a build.
Setting JAVA_HOME and switching versions
Many build tools (Maven, Gradle, some app servers) look for JAVA_HOME. Set it once, system-wide:
sudo nano /etc/environment
# add a line:
JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"
Log out and back in, then confirm with echo $JAVA_HOME. The exact path depends on the version you installed - ls /usr/lib/jvm/ shows what is there.
If you have several JDKs installed and need to flip the default between them:
sudo update-alternatives --config java
It prints a numbered menu and lets you pick which java the system uses. Do the same with javac. Keep the two pointed at the same version - a java on 21 and a javac on 17 is a subtle, maddening source of build errors.
How this fits the rest of the stack
Whatever you decide here, the cost of it eventually shows up as a bill. The RunxBuild hosting calculator is the right place to model that before committing: the compute, the database, the storage, the bandwidth, the worker - each one is a separate line item, and the real cost of a platform is the sum, not the headline number. The RunxBuild dashboard is where the team sees the actual usage once it is running.
Useful related references:
- How to Install Docker on Ubuntu: 2026 Guide (22.04 and 24.04)
- Install MongoDB on Ubuntu: Official Repo, systemd, and 7.0 Setup
- Install MySQL on Ubuntu: apt, mysql_secure_installation, and 8.0 Setup
- Java services on RunxBuild
- Services on RunxBuild
FAQ
How do I check which Java version is installed on Ubuntu?
Run java -version for the runtime and javac -version for the compiler. Both should report the same major version. If java works but javac is missing, you have a JRE but not the full JDK - install default-jdk to get both.
Should I install OpenJDK or Oracle JDK?
OpenJDK, unless a vendor explicitly requires Oracle. OpenJDK is free for production and commercial use, is what apt installs, and performs identically. Oracle JDK adds a licence you almost certainly do not need for a normal web application.
Which Java version should I use?
Match whatever your production or deployment target runs, and prefer a long-term-support release - Java 17 or Java 21. A jar compiled for a newer version will not run on an older runtime, so version mismatch between build and deploy is the thing to avoid.
How do I set JAVA_HOME on Ubuntu?
Add JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64" (adjust the path to your installed version) to /etc/environment, then log out and back in. Confirm with echo $JAVA_HOME. Maven and Gradle read this variable to find the JDK.
How do I uninstall Java on Ubuntu?
Remove the package you installed, for example sudo apt remove openjdk-21-jdk or sudo apt remove default-jdk, then sudo apt autoremove to clean up dependencies. If you installed several JDKs, remove each one and re-check update-alternatives --config java.