In this tutorial, we'll focus on how to install Java 8 on Ubuntu 18.04 from the terminal. We will begin by installing the default JDK/JRE.
Java is an application that is a prerequisite in a wide range of software such as Tomcat Web server, Netbeans IDE, Glassfish server to mention just but a few.
How to install default JDK/JRE
This is the easiest option when installing Java since it comes packaged with Ubuntu system. This will install OpenJDK 8 which is the recommended version. First off, let's update the system. Log in as root user and run the command below
apt update
Next, Install Java Runtime Environment
apt-get install default-jre
Sample Output
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: ca-certificates-java default-jre-headless fonts-dejavu-extra java-common libatk-wrapper-java libatk-wrapper-java-jni libgif7 openjdk-11-jre openjdk-11-jre-headless Suggested packages: default-java-plugin fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei The following NEW packages will be installed: ca-certificates-java default-jre default-jre-headless fonts-dejavu-extra java-common libatk-wrapper-java libatk-wrapper-java-jni libgif7 openjdk-11-jre openjdk-11-jre-headless
On the other hand, there exists a default Java installation popularly known as JDK, short for Java Development Kit. This is commonly used with IDE's such as Eclipse and Netbeans for compiling Java programs. It already comes with JRE, so there's no need of installing JRE once you've installed JDK.
To install JDK, run
apt-get install default-jdk
Sample Output
The following additional packages will be installed: default-jdk-headless libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libx11-doc libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-11-jdk openjdk-11-jdk-headless x11proto-core-dev x11proto-dev xorg-sgml-doctools xtrans-dev Suggested packages: libice-doc libsm-doc libxcb-doc libxt-doc openjdk-11-demo openjdk-11-source visualvm The following NEW packages will be installed: default-jdk default-jdk-headless libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libx11-doc libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-11-jdk openjdk-11-jdk-headless x11proto-core-dev x11proto-dev xorg-sgml-doctools xtrans-dev
How to install Oracle JDK
If you want to be more specific and install Oracle JDK, you'll be required to add Oracle's PPA.
add-apt-repository ppa:webupd8team/java
Sample Output
Get:1 http://ppa.launchpad.net/webupd8team/java/ubuntu bionic InRelease [15.4 kB] Hit:2 http://ke.archive.ubuntu.com/ubuntu bionic InRelease Hit:3 http://ke.archive.ubuntu.com/ubuntu bionic-updates InRelease Hit:4 http://ke.archive.ubuntu.com/ubuntu bionic-backports InRelease Hit:5 http://linux.teamviewer.com/deb stable InRelease Hit:6 http://linux.teamviewer.com/deb preview InRelease Get:7 http://ppa.launchpad.net/webupd8team/java/ubuntu bionic/main i386 Packages [1,556 B] Get:8 http://ppa.launchpad.net/webupd8team/java/ubuntu bionic/main amd64 Packages [1,556 B] Get:9 http://ppa.launchpad.net/webupd8team/java/ubuntu bionic/main Translation-en [928 B]
Next, update the system.
apt update
Install Oracle JDK 8 which is the most stable version by running the command below
apt-get install oracle-java8-installer
The following pop up will be displayed. Press 'Enter'.
You'll then be prompted to accept the license agreement. Use the Cursor Keys and Press 'Enter' on 'Yes'.
Sample Output
Preconfiguring packages ... Selecting previously unselected package oracle-java8-installer. (Reading database ... 131873 files and directories currently installed.) Preparing to unpack .../oracle-java8-installer_8u171-1~webupd8~0_all.deb ... Unpacking oracle-java8-installer (8u171-1~webupd8~0) ... Setting up oracle-java8-installer (8u171-1~webupd8~0) ... No /var/cache/oracle-jdk8-installer/wgetrc file found. Creating /var/cache/oracle-jdk8-installer/wgetrc and using default oracle-java8-installer wgetrc settings for it. Downloading Oracle Java 8...
Checking Java version
To verify that we have Java installed and check out the version, run the command below.
java -version
Sample Output
java version "1.8.0_171" Java(TM) SE Runtime Environment (build 1.8.0_171-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
How to manage Java
As seen above we have a few Java installation in our system, and as good practice, It's advisable to set the default Java version. To achieve this, e have to run the command below
update-alternatives --config java
Sample Output
Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 manual mode * 2 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode Press to keep the current choice[*], or type selection number:
Scroll using the cursor keys and Press Enter on your preferred java installation.
Finally, we need to set the JAVA_HOME environment variable.
How to set up the JAVA_HOME environment variable
To do so, Open /etc/environment
nano /etc/environment
Add the path to your preferred java installation
JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre/bin/java "
Press 'CTRL + O' to Save and CTRL + X' to exit nano editor.
Reload by running
source /etc/environment
To verify that all went well run the following command.
echo $JAVA_HOME
This should display the path specified
Sample Output
/usr/lib/jvm/java-8-oracle/jre/bin/java
How to uninstall Oracle Java
To completely remove Oracle Java from your system, first, run the commands below to check which version is installed
dpkg --list | grep -i jdk
Output
ii oracle-java8-installer 8u171-1~webupd8~0 all Oracle Java(TM) Development Kit (JDK) 8 ii oracle-java8-set-default 8u171-1~webupd8~0 all Set Oracle JDK 8 as default Java
To remove Oracle Java 8, run
apt-get purge oracle-java8-installer
Sample Output
Reading package lists... Done Building dependency tree Reading state information... Done The following package was automatically installed and is no longer required: gsfonts-x11 Use 'sudo apt autoremove' to remove it. The following packages will be REMOVED: oracle-java8-installer* oracle-java8-set-default* 0 upgraded, 0 newly installed, 2 to remove and 382 not upgraded. After this operation, 138 kB disk space will be freed. Do you want to continue? [Y/n]
Recommended read:
- How to Install Java 8 on Debian GNU/Linux 9 (Stretch)
- How Install java and Set Environment Variables RHEL / CentOS
In this tutorial, we have outlined the steps on how you can install Java on Ubuntu 18.04. Java is crucial for a number of software applications as mentioned earlier such as Tomcat server. Feel free to try these steps out. Stay tuned for more insightful articles from us.