Java powers a wide range of applications. Tools such as Tomcat, Cassandra, Jenkins, and GlassFish all depend on it. On Ubuntu, the fastest way to get Java running is to use the apt install java command in your terminal. This guide covers OpenJDK, Oracle JDK, version switching, and JAVA_HOME configuration.

What You Need Before Running Apt Install Java

You need an Ubuntu machine with sudo privileges and terminal access. Make sure your firewall and user account are properly configured before starting.

How to Apt Install Java on Ubuntu Using OpenJDK

OpenJDK ships directly from Ubuntu’s default repositories. It is free, open-source, and functionally identical to Oracle’s version since Java 11.

Start by refreshing your package list:

sudo apt update

Check whether Java already exists on your machine:

java -version

If nothing comes back, you can resolve this in the same way as other missing apt-get binary issues — verify your package list is current, then proceed. Run the install:

sudo apt install default-jre

This pulls down the Java Runtime Environment. Confirm with:

java -version

Installing the JDK for Java Development on Ubuntu

If you need the compiler as well — for building Java programs rather than just running them — install the full development kit:

sudo apt install default-jdk

Verify the compiler installed correctly:

javac -version

If you need a specific release rather than the latest default, the process for pinning a package to a particular version works the same way here — append the version string to the package name.

OpenJDK vs Oracle JDK: Key Differences

Distribution Comparison
OpenJDK
Oracle JDK
License
Open-source (GPL)
Commercial license
Installation
Ubuntu repositories
Manual download required
Separate JRE Package
Available
Bundled with JDK
Community Patches
Yes
No
Functionality (Java 11+)
Identical
Identical

How to Get Oracle JDK on Ubuntu

Oracle’s licensing does not permit direct access to package managers. You must download the archive from Oracle’s site, transfer it to your server, and rely on a third-party installer script.

After downloading the .tar.gz file, use scp to move it to your server, import the signing key, add the repository, then run the installer with apt. Oracle JDK bundles its own runtime, so a separate JRE package is not needed.

Switching Between Multiple Java Versions on Ubuntu

Ubuntu supports running multiple Java versions at the same time. Use this command to pick your default:

sudo update-alternatives --config java

A numbered list appears. Type the number matching your preferred version and press Enter. You can repeat the same process for javac, keytool, and javadoc.

Setting the JAVA_HOME Variable After Apt Install Java

Many Java-based programs read the JAVA_HOME environment variable to locate your installation. First, find the path:

sudo update-alternatives --config java

Copy the directory shown — excluding the bin/ portion at the end. Then open /etc/environment:

sudo nano /etc/environment

Append this line:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Reload the file and confirm:

source /etc/environment
echo $JAVA_HOME

For a full breakdown of how persistent environment variables work across sessions on Linux, including system-wide vs. user-level scope, that guide covers both approaches in detail.

Other users on the system will see the updated value after logging out and back in.

JRE vs JDK: Which One to Install

Capability Coverage
JDK JRE
Run Java apps
JDK ✓
Run Java apps
JRE ✓
Compile source
JDK ✓
Compile source
JRE ✕
Debugger tools
JDK ✓
Debugger tools
JRE ✕
Disk footprint
Larger
Disk footprint
Smaller

Fixing Common Problems with Apt Install Java

Error Message Fix
command not found for java Add the JDK bin folder to your PATH. See the guide on extending the Linux executable search path for the correct syntax.
JAVA_HOME is not set Export JAVA_HOME pointing to the JDK directory, excluding /bin.
Cannot locate JDK or JRE Verify your PATH includes the correct bin directory for the installed version.
Permission denied during setup Prefix your command with sudo.

FAQs

What is the command to apt install Java on Ubuntu?

Run sudo apt update first, then sudo apt install default-jre for the runtime or sudo apt install default-jdk for the full development kit including the compiler.

What is the difference between JRE and JDK on Ubuntu?

The JRE runs Java programs. The JDK includes the JRE plus a compiler, debugger, and other development tools. Install the JDK if you are writing or compiling Java code.

How do I switch Java versions using apt on Ubuntu?

Run sudo update-alternatives --config java, then enter the number corresponding to the version you want. Repeat for javac if needed.

Does apt install Java set JAVA_HOME automatically?

No. You must set it manually by adding JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64" to /etc/environment, then running source /etc/environment.

Can I install Oracle JDK with apt on Ubuntu?

Not directly. Oracle’s licensing requires a manual download from Oracle’s website and a third-party installer script. OpenJDK is functionally identical for most use cases since Java 11.

Willie has over 15 years of experience in Linux system administration and DevOps. After managing infrastructure for startups and enterprises alike, he founded Command Linux to share the practical knowledge he wished he had when starting out. He oversees content strategy and contributes guides on server management, automation, and security.