// Tutorial //

How To Install Java with Apt-Get on Ubuntu 16.04

Published on April 23, 2016
English
How To Install Java with Apt-Get on Ubuntu 16.04
Not using Ubuntu 16.04?Choose a different version or distribution.
Ubuntu 16.04

Introduction

Java and the JVM (Java’s virtual machine) are widely used and required for many kinds of software. This article will guide you through the process of installing and managing different versions of Java using apt-get.

Prerequisites

To follow this tutorial, you will need:

Installing the Default JRE/JDK

The easiest option for installing Java is using the version packaged with Ubuntu. Specifically, this will install OpenJDK 8, the latest and recommended version.

First, update the package index.

  1. sudo apt-get update

Next, install Java. Specifically, this command will install the Java Runtime Environment (JRE).

  1. sudo apt-get install default-jre

There is another default Java installation called the JDK (Java Development Kit). The JDK is usually only needed if you are going to compile Java programs or if the software that will use Java specifically requires it.

The JDK does contain the JRE, so there are no disadvantages if you install the JDK instead of the JRE, except for the larger file size.

You can install the JDK with the following command:

  1. sudo apt-get install default-jdk

Installing the Oracle JDK

If you want to install the Oracle JDK, which is the official version distributed by Oracle, you will need to follow a few more steps.

First, add Oracle’s PPA, then update your package repository.

  1. sudo add-apt-repository ppa:webupd8team/java
  2. sudo apt-get update

Then, depending on the version you want to install, execute one of the following commands:

Oracle JDK 8

This is the latest stable version of Java at time of writing, and the recommended version to install. You can do so using the following command:

  1. sudo apt-get install oracle-java8-installer

Oracle JDK 9

This is a developer preview and the general release is scheduled for March 2017. It’s not recommended that you use this version because there may still be security issues and bugs. There is more information about Java 9 on the official JDK 9 website.

To install JDK 9, use the following command:

  1. sudo apt-get install oracle-java9-installer

Managing Java

There can be multiple Java installations on one server. You can configure which version is the default for use in the command line by using update-alternatives, which manages which symbolic links are used for different commands.

  1. sudo update-alternatives --config java

The output will look something like the following. In this case, this is what the output will look like with all Java versions mentioned above installed.

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

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      auto mode
  1            /usr/lib/jvm/java-6-oracle/jre/bin/java          1         manual mode
  2            /usr/lib/jvm/java-7-oracle/jre/bin/java          2         manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  4            /usr/lib/jvm/java-8-oracle/jre/bin/java          3         manual mode
  5            /usr/lib/jvm/java-9-oracle/bin/java              4         manual mode

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

You can now choose the number to use as a default. This can also be done for other Java commands, such as the compiler (javac), the documentation generator (javadoc), the JAR signing tool (jarsigner), and more. You can use the following command, filling in the command you want to customize.

  1. sudo update-alternatives --config command

Setting the JAVA_HOME Environment Variable

Many programs, such as Java servers, use the JAVA_HOME environment variable to determine the Java installation location. To set this environment variable, we will first need to find out where Java is installed. You can do this by executing the same command as in the previous section:

  1. sudo update-alternatives --config java

Copy the path from your preferred installation and then open /etc/environment using nano or your favorite text editor.

  1. sudo nano /etc/environment

At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path.

/etc/environment
JAVA_HOME="/usr/lib/jvm/java-8-oracle"

Save and exit the file, and reload it.

  1. source /etc/environment

You can now test whether the environment variable has been set by executing the following command:

  1. echo $JAVA_HOME

This will return the path you just set.

Conclusion

You have now installed Java and know how to manage different versions of it. You can now install software which runs on Java, such as Tomcat, Jetty, Glassfish, Cassandra, or Jenkins.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors

Default avatar

staff technical writer

hi! i write do.co/docs now, but i used to be the senior tech editor publishing tutorials here in the community.


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Hi there.

“ppa:webupd8team/java” is not Oracle PPA, it’s a normal PPA made by a 3rd-party user (the creator of the website webupd8.org) and recommending installing Java from such repository claiming that it’s from Oracle is a huge security danger, please fix this.

Thank you.

This no longer works, the webupd8team/java PPA has been discontinued. https://launchpad.net/~webupd8team/+archive/ubuntu/java

I had issues with installing java on desktop. But this one https://serverclear.com/install-java-ubuntu/ worked for ubuntu 16.04 server.

This is the official way to install Oracle Java, but it is also the worst way to install Oracle Java.

The Debian package is only a wrapper installer, which then fetches an Oracle tarball from affiliated third party, and occasionally these third party vendors may have an outage, and the installation fails.

It would be nice to make this downloaded package available for the rest of the network, so we can install it on other servers, such as caching the URL Oracle uses to fetch a third party link, and then replay that link always returns a local location, like GCS or S3 bucket. Alternatively, just not use the Oracle installer that comes with Debian package.

I just want to thank you from the bottom of my heart for this great, clear and working tutorial. Thank you very much. Keep up the good work.

When I run the second command sudo apt-get install default-jdk I’m getting this error.

Reading package lists... Done
Building dependency tree       
Reading state information... Done
default-jdk is already the newest version (2:1.8-59ubuntu1).
The following package was automatically installed and is no longer required:
  gir1.2-appindicator3-0.1
Use 'sudo apt autoremove' to remove it.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
5 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
Setting up oracle-java8-installer (8u151-1~webupd8~0) ...
Using wget settings from /var/cache/oracle-jdk8-installer/wgetrc
Downloading Oracle Java 8...
--2018-01-17 12:06:17--  http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz
Resolving download.oracle.com (download.oracle.com)... 96.6.64.51
Connecting to download.oracle.com (download.oracle.com)|96.6.64.51|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz [following]
--2018-01-17 12:06:17--  https://edelivery.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz
Resolving edelivery.oracle.com (edelivery.oracle.com)... 118.214.58.117, 2402:d000:130:289::2d3e, 2402:d000:130:281::2d3e
Connecting to edelivery.oracle.com (edelivery.oracle.com)|118.214.58.117|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz?AuthParam=1516171097_63a41b59787c22485318681ff0208111 [following]
--2018-01-17 12:06:19--  http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz?AuthParam=1516171097_63a41b59787c22485318681ff0208111
Connecting to download.oracle.com (download.oracle.com)|96.6.64.51|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2018-01-17 12:06:19 ERROR 404: Not Found.

download failed
Oracle JDK 8 is NOT installed.
dpkg: error processing package oracle-java8-installer (--configure):
 subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of default-jre:
 default-jre depends on openjdk-8-jre; however:
  Package openjdk-8-jre is not installed.
  Package oracle-java9-installer which provides openjdk-8-jre is not installed.
  Package oracle-java8-installer which provides openjdk-8-jre is not configured yet.

dpkg: error processing package default-jre (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of default-jdk:
 default-jdk depends on default-jre (= 2:1.8-59ubuntu1); however:
  Package default-jre is not configured yet.
  Package oracle-java9-installer which provides default-jre is not installed.
  Version of default-jre on system, provided by oracle-java8-installer:all, is <none>.
 default-jdk depends on openjdk-8-jdk; however:
  Package openjdk-8-jdk is not installed.
  Package oracle-java9-installer which provides openjdk-8-jdk is not installed.
  Package oracle-java8-installer which provides openjdk-8-jdk is not configuNo apport report written because the error message indicates its a followup error from a previous failure.
                      No apport report written because the error message indicates its a followup error from a previous failure.
                                                No apport report written because MaxReports is reached already
                              No apport report written because MaxReports is reached already
            red yet.

dpkg: error processing package default-jdk (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of default-jre-headless:
 default-jre-headless depends on openjdk-8-jre-headless; however:
  Package openjdk-8-jre-headless is not installed.
  Package oracle-java9-installer which provides openjdk-8-jre-headless is not installed.
  Package oracle-java8-installer which provides openjdk-8-jre-headless is not configured yet.

dpkg: error processing package default-jre-headless (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of default-jdk-headless:
 default-jdk-headless depends on default-jre-headless (= 2:1.8-59ubuntu1); however:
  Package default-jre-headless is not configured yet.
  Package oracle-java9-installer which provides default-jre-headless is not installed.
  Version of default-jre-headless on system, provided by oracle-java8-installer:all, is <none>.
 default-jdk-headless depends on openjdk-8-jdk-headless; however:
  Package openjdk-8-jdk-headless is not installed.
  Package oracle-java9-installer which provides openjdk-8-jdk-headless is not installed.
  Package oracle-java8-installer which provides openjdk-8-jdk-headless is not configured yet.

dpkg: error processing package default-jdk-headless (--configure):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 oracle-java8-installer
 default-jre
 default-jdk
 default-jre-headless
 default-jdk-headless
E: Sub-process /usr/bin/dpkg returned an error code (1)

How can I fix this and install jdk on my Ubuntu 17.10

Was just writing a tutorial on how to install Java on Ubuntu on my site, so I found this tutorial too.

Just as an update: Java 9 is now the stable release and only supports 64-bit systems. Maybe an update is due, or a new tutorial.

Great post thanks !

can be better to add JAVA_HOME environment variable in /etc/bash.bashrc file or ~/.bashrc file

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel