Install Java on Ubuntu 16.04
Updated by Phil Zona Contributed by Phil Zona
Java is one of the world’s most popular programming languages. Java can be used to create anything from software to basic web applications.
In this guide, we’ll install the Oracle Java development kit for building Java applications. We’ll also cover OpenJDK, an open-source alternative to the Oracle Java development kit.
Before You Begin
Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.
This guide will use
sudowherever possible. Complete the sections of our Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services.
Update your system:
sudo apt-get update && sudo apt-get upgrade
Install Oracle JDK
The Oracle JDK, includes a development environment for building applications with the Java programming language. Please be aware that some elements of the Oracle JDK are proprietary, meaning that there may be licensing implications with respect to applications you develop with it.
software-properties-commonpackage if you don’t already have it. This provides an easier way to add new repositories:
sudo apt-get install software-properties-common
Add the Java PPA:
sudo add-apt-repository ppa:webupd8team/java
NoteThis repository is not maintained by Oracle. It does not contain actual Java files, but does allow us to download installers for Oracle Java software. Before using the installers, you’ll be prompted to accept a license agreement, which can be found in its entirety here.
Update the local package cache:
sudo apt-get update
Install the metapackage:
sudo apt-get install oracle-java8-installer
This package will run an installer for The Oracle JDK 8, which is the current stable release. You may also replace
java8in the package name with
java9to install different versions, although these releases are not recommended for development.
Verify that Java and the Java compiler have been properly installed:
java -version javac -version
As of this publication, these commands should return the following:
java version "1.8.0_131" Java(TM) SE Runtime Environment (build 1.8.0_131-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode) javac 1.8.0_131
Since the PPA only provides an installer, and not updates for the JDK itself, you may want to delete it when you’re finished in order to keep your repositories organized:
sudo add-apt-repository -r ppa:webupd8team/java
Set Java Home Environment
Many applications include code or configuration that references the
JAVA_HOME environment variable. This variable points them to the Java binary file, allowing them to run Java code.
To set the variable for your system:
echo "JAVA_HOME=$(which java)" | sudo tee -a /etc/environment
Reload your system’s environment variables:
Verify the variable was set correctly:
This should return the path to the Java binary.
The above installation methods allow you to use the Oracle JDK, which is be bound by licensing terms and includes proprietary components. OpenJDK provides an open-source alternative that is just as easy to install.
To install OpenJDK:
sudo apt-get install openjdk-8-jdk
The installation will provide you with the OpenJDK, which includes a runtime environment and compiler. This allows you to develop your own Java applications and run them on your Linode.
However, if you only need to run applications that you’ve already downloaded, you can save a bit of disk space by installing the OpenJRE (Java runtime environment):
sudo apt-get install openjdk-8-jre
Note that this is unnecessary if you’ve installed OpenJDK, since it includes the JRE.
CautionOpenJDK and Oracle Java are not identical. There may be licensing, performance, and stability differences, and this should be considered carefully when developing production applications.
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
Join our Community
This guide is published under a CC BY-ND 4.0 license.