Installing Java on Ubuntu 18.04

How to install JRE and JDK versions using apt.

Java is a widely used, object-oriented programming language originally developed by Sun Microsystems. One of its core design principles is platform independence — Java applications run inside the Java Virtual Machine (JVM), which also enforces strict security and execution controls.

Java is commonly used for backend services, web applications, and development tooling.

Install the default OpenJDK

Ubuntu 18.04 includes OpenJDK (an open-source implementation of Java). By default, this installs OpenJDK 11.

Start by updating the package index:

sudo apt update

Check if Java is already installed:

java -version

If it’s not installed, you’ll see a message like:

Command 'java' not found, but can be installed with:
apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headless

Install the JRE (Java Runtime Environment) — this is enough to run most Java applications:

sudo apt install default-jre

Verify the installation:

java -version

Example output:

openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing)

If you need to compile Java applications, install the JDK (includes JRE and development tools):

sudo apt install default-jdk

Check the compiler:

javac -version

Example output:

javac 11.0.6

Install specific OpenJDK versions

If you need a specific version, you can install it directly.

OpenJDK 8:

sudo apt install openjdk-8-jdk    # полный JDK
sudo apt install openjdk-8-jre    # только JRE

OpenJDK 11:

sudo apt install openjdk-11-jdk   # полный JDK
sudo apt install openjdk-11-jre   # только JRE

Install Oracle JDK

To install Oracle JDK, add a third-party repository:

sudo add-apt-repository ppa:webupd8team/java
sudo apt update
sudo apt install oracle-java8-installer

During installation, you’ll be prompted to accept the Oracle license agreement.

Note: this repository may be deprecated or unavailable. In most cases, OpenJDK is the recommended option.

Switch between multiple Java versions

If you have multiple Java versions installed, you can choose the default one using update-alternatives:

sudo update-alternatives --config java

You’ll see a list like:

There are 2 choices for the alternative java (providing /usr/bin/java).

Selection    Path                                            Priority   Status

0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode
1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Enter the number of the version you want, or press Enter to keep the current one.

You can switch the compiler the same way:

sudo update-alternatives --config javac

Set the JAVA_HOME variable

Many applications rely on the JAVA_HOME environment variable to locate your Java installation.

First, find the correct path:

sudo update-alternatives --config java

Copy the path (for example, /usr/lib/jvm/java-11-openjdk-amd64/bin/java) and open the environment file:

sudo nano /etc/environment

Add this line at the end (remove /bin/java from the path):

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

Save the file using Ctrl + O → Enter → Ctrl + X.

Apply the changes:

source /etc/environment

Verify that the variable is set:

echo $JAVA_HOME

Expected output:

/usr/lib/jvm/java-11-openjdk-amd64

Other users on the system will need to run source /etc/environment as well to apply the changes.

Useful notes

  • OpenJDK is the default and recommended choice for most workloads — it’s actively maintained and fully compatible with the Java ecosystem.
  • Avoid mixing multiple package sources for Java unless necessary — it can lead to conflicts during updates.
  • If Java commands don’t work after installation, check your PATH and JAVA_HOME variables.
  • For production environments, keep Java versions consistent across all nodes to avoid runtime issues.

Help

If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!

Need help?Our engineers will help you free of charge with any question in minutesContact us