How to Install Java with Apt-Get on Ubuntu 16.04

To Install Java with Apt-Get on Ubuntu 16.04

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


Installing the Default JRE/JDK

The easiest option for installing Java is using the version packaged with Ubuntu. While using this package, it also installs the latest and recommended version of OpenJDK 8.

Initialte the installation process by updating the package index first. Use the following command for the same process.

root@linuxhelp:~# apt-get update
Hit:1 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://security.ubuntu.com/ubuntu xenial-security InRelease                                            
Hit:3 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease                                           
Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease                     
Hit:5 http://repository.spotify.com stable InRelease                                     
Reading package lists... Done


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

root@linuxhelp:~# apt-get install default-jre
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  ca-certificates-java default-jre-headless java-common libbonobo2-0 libbonobo2-common libgnome-2-0
  libgnome2-common libgnomevfs2-0 libgnomevfs2-common liborbit-2-0 openjdk-8-jre openjdk-8-jre-headless
Suggested packages:
  default-java-plugin libbonobo2-bin desktop-base libgnomevfs2-bin libgnomevfs2-extra gamin | fam
  gnome-mime-data icedtea-8-plugin openjdk-8-jre-jamvm fonts-ipafont-gothic fonts-ipafont-mincho
  ttf-wqy-microhei | ttf-wqy-zenhei fonts-indic
The following NEW packages will be installed:
  ca-certificates-java default-jre default-jre-headless java-common libbonobo2-0 libbonobo2-common
  libgnome-2-0 libgnome2-common libgnomevfs2-0 libgnomevfs2-common liborbit-2-0 openjdk-8-jre
  openjdk-8-jre-headless
0 upgraded, 13 newly installed, 0 to remove and 494 not upgraded.
Need to get 27.7 MB of archives.
After this operation, 104 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
.
.
Setting up default-jre (2:1.8-56ubuntu2) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...

There is another default Java installation called the JDK (Java Development Kit). You can install the JDK with the following command:

The JDK is needed only 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.

root@linuxhelp:~# apt-get install default-jdk
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  default-jdk-headless libice-dev libsm-dev libxt-dev openjdk-8-jdk openjdk-8-jdk-headless
Suggested packages:
  libice-doc libsm-doc libxt-doc openjdk-8-demo openjdk-8-source visualvm
The following NEW packages will be installed:
  default-jdk default-jdk-headless libice-dev libsm-dev libxt-dev openjdk-8-jdk openjdk-8-jdk-headless
0 upgraded, 7 newly installed, 0 to remove and 494 not upgraded.
Need to get 9,105 kB of archives.
After this operation, 41.4 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
.
.
Setting up default-jdk (2:1.8-56ubuntu2) ...
Setting up libice-dev:amd64 (2:1.0.9-1) ...
Setting up libsm-dev:amd64 (2:1.2.2-1) ...
Setting up libxt-dev:amd64 (1:1.1.5-0ubuntu1) ...

Installing the Oracle JDK
If you want to install the Oracle JDK, the official version distributed by Oracle, you have to follow few more steps.
First, add Oracle' s PPA, then update your package repository.
Webupd8 Team is maintaining a PPA repository with installer scripts for the latest Java 8 and 9, that automatically downloads the Java archive from Oracle website and sets up everything for you.

root@linuxhelp:~# add-apt-repository ppa:webupd8team/java
 Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK7 / JDK8 / JDK9). There are no actual Java files in this PPA.

More info (and Ubuntu installation instructions):
- for Oracle Java 7: http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
- for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html

Debian installation instructions:
- Oracle Java 7: http://www.webupd8.org/2012/06/how-to-install-oracle-java-7-in-debian.html
- Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html
.
.
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

Once it is done, use the following command to update the apt sources-list.

root@linuxhelp:~# apt-get update
Hit:1 http://security.ubuntu.com/ubuntu xenial-security InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu xenial InRelease          
Get:3 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease [17.6 kB]             
Hit:4 http://repository.spotify.com stable InRelease                                                      
Hit:5 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease                                        
Hit:6 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease                
Get:7 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main amd64 Packages [2,860 B]
Get:8 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main i386 Packages [2,860 B]
Get:9 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main Translation-en [1,260 B]
Fetched 24.5 kB in 2s (12.2 kB/s)          
Reading package lists... Done

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.

root@linuxhelp:~# apt-get install oracle-java8-installer
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  gsfonts-x11 oracle-java8-set-default
Suggested packages:
  binfmt-support visualvm ttf-baekmuk | ttf-unfonts | ttf-unfonts-core ttf-kochi-gothic
  | ttf-sazanami-gothic ttf-kochi-mincho | ttf-sazanami-mincho ttf-arphic-uming
The following NEW packages will be installed:
  gsfonts-x11 oracle-java8-installer oracle-java8-set-default
0 upgraded, 3 newly installed, 0 to remove and 495 not upgraded.
Need to get 36.7 kB of archives.
After this operation, 211 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
.
.
Setting up gsfonts-x11 (0.24) ...
Setting up oracle-java8-set-default (8u121-1~webupd8~0) ...

If you want to view the version of Java, you can use the following command.

root@linuxhelp:~# java -version
java version " 1.8.0_121" 
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
Tag : Java
FAQ
Q
which command to check the java version in ubuntu?
A
you want to view the version of Java, you can use the following command
# java -version
Q
which command to add the repo for installing oracle JDK in ubuntu?
A
add Oracle' s PPA, then update your package repository by following steps
# add-apt-repository ppa:webupd8team/java
# apt-get update
Q
how to install java runtime environment on ubuntu?
A
this command installs the Java Runtime Environment (JRE).
# apt-get install default-jre
Q
what is the purpose of Install Java on Ubuntu?
A
Java and the JVM (Java' s virtual machine) are widely used on many kinds of software. This article will guide you through the process of installing and managing different versions of Java using apt-get.
Q
How do I save and exit out of the repo once i have entered the content in?
A
Press [Esc] key and type Shift+ZZ to save file.