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 our products

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.

Great tut!

Thanks

To set JAVA_HOME environment variable, do the following:

Launch Terminal by pressing Ctrl+Alt+T on your keyboard. Enter the following command: $ gksudo gedit /etc/environment Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the /usr/lib/jvm/java-7-oracle directory. Scroll to the end of the file and enter the following: JAVA_HOME=/usr/lib/jvm/java-7-oracle export JAVA_HOME Save your file and exit gedit. Lastly, reload the system PATH with the following command: $ . /etc/environment The above method will save you the hassle in having to run the commands every time you log in to your computer. from https://askubuntu.com/questions/175514/how-to-set-java-home-for-java/175519#175519?newreg=fa5f6898f8c6478f8ec1f998f041d122

To automatically set JAVA_HOME simply use this export JAVA_HOME=“$(jrunscript -e ‘java.lang.System.out.println(java.lang.System.getProperty(“java.home”));’)”

Check success with echo $JAVA_HOME

Awesome. Thank you so much.

When issuing the command: sudo add-apt-repository ppa:webupd8team/java You can get: sudo: add-apt-repository: command not found To solve this, do the next: sudo apt-get install software-properties-common And then rerun adding your repository.

Noticed this line in the installer there is a dead easy way to make Oracle Java 8 the default.

sudo apt-get install oracle-java8-set-default

After it’s installed of course. Makes it so much easier :-)

One thing that was missing for me, was how to install oracle jdk headless. But there is a silent option: echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections

See: http://askubuntu.com/questions/190582/installing-java-automatically-with-silent-option

Great, Tks

sudo add-apt-repository ppa:webupd8team/java

No matter how many time i run the above command i keep getting the following error

gpg: keyring /tmp/tmpicu6zv49/secring.gpg' created gpg: keyring /tmp/tmpicu6zv49/pubring.gpg’ created gpg: requesting key EEA14886 from hkp server keyserver.ubuntu.com gpgkeys: HTTP fetch error 7: couldn’t connect: eof gpg: no valid OpenPGP data found. gpg: Total number processed: 0 gpg: keyserver communications error: keyserver unreachable gpg: keyserver communications error: public key not found gpg: keyserver receive failed: public key not found

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!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.