Monday, May 7, 2018

How to install Java on Ubuntu 18.04 Bionic Beaver Linux

https://linuxconfig.org/how-to-install-java-on-ubuntu-18-04-bionic-beaver-linux

Objective

The objective of this tutorial is to install Java on Ubuntu. We will be installing the latest version of Oracle Java SE Development Kit (JDK) on Ubuntu 18.04 Bionic Beaver Linux. This will be performed in three ways: Installing Java using the Ubuntu Open JDK binaries, installing Java via PPA and installing Java using the official Oracle Java binaries.

Operating System and Software Versions

  • Operating System: - Ubuntu 18.04 Bionic Beaver
  • Software: - Java(TM) SE Runtime Environment 8,9,10 or 11

Requirements

Privileged access to to your Ubuntu 18.04 Bionic Beaver Linux system is required to perform this installation.

Difficulty

EASY

Conventions

  • # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - given command to be executed as a regular non-privileged user

Instructions

Install Java using the Ubuntu Open JDK binaries

In most cases you do not need to look further to install Java on Ubuntu than Ubuntu's repository which contains an opensource version of Java runtime binaries called Open JDK.
To install Ubuntu Java Open JDK version 11 execute:
$ sudo apt install openjdk-11-jdk
To install Ubuntu Java Open JDK version 9 execute:
$ sudo apt install openjdk-9-jdk
and for Java Open JDK 8 run:
$ sudo apt install openjdk-8-jdk

Install Java on Ubuntu via PPA

Add PPA Repository

Using Webupd8 Team's PPA repository we can install Java on Ubuntu automatically using the apt command. Webupd8 Team currently maintains Oracle Java 8 and Oracle Java 9 PPA repositories for Ubuntu 18.04 Bionic Beaver.

Let's start by adding a PPA repository:
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt update

Install Java on Ubuntu

After adding PPA repository we can move to installing java on Ubuntu. Executing apt search oracle-java command should now show multiple java versions available for install.

Namely they are java8 and java9.

To install Java 8 execute:
$ sudo apt install oracle-java8-set-default
To install Java 9 execute
(see the below manual Java installation intructions if version 9 is unavailable and you need java version greater than 8.) :
$ sudo apt install oracle-java9-set-default


The above commands will automatically install selected java version and set all necessary java environment variables.

$ java --version
java 9.0.4
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

Set default Java Version Automatically

It is possible to install both Oracle Java 8 and Oracle Java 9 versions at the same time.

To switch between version simply re-install oracle-java8-set-default or oracle-java9-set-default package.

Please note this will not cause apt to remove Java installer, as it will simply update all java related environmental variables and remove oracle-javaX-set-default package counterpart.

Example:

$ sudo apt install oracle-java8-set-default
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  oracle-java9-set-default
The following NEW packages will be installed:
  oracle-java8-set-default
0 to upgrade, 1 to newly install, 1 to remove and 0 not to upgrade.
Need to get 6,830 B of archives.
After this operation, 4,096 B of additional disk space will be used.
Do you want to continue? [Y/n] 
Get:1 http://ppa.launchpad.net/webupd8team/java/ubuntu bionic/main amd64 oracle-java8-set-default all 8u161-1~webupd8~0 [6,830 B]
Fetched 6,830 B in 1s (9,357 B/s)                    
(Reading database ... 99410 files and directories currently installed.)
Removing oracle-java9-set-default (9.0.4-1~webupd8~0) ...
Selecting previously unselected package oracle-java8-set-default.
(Reading database ... 99407 files and directories currently installed.)
Preparing to unpack .../oracle-java8-set-default_8u161-1~webupd8~0_all.deb ...
Unpacking oracle-java8-set-default (8u161-1~webupd8~0) ...
Setting up oracle-java8-set-default (8u161-1~webupd8~0) ...
Installing new version of config file /etc/profile.d/jdk.csh ...
Installing new version of config file /etc/profile.d/jdk.sh ...

Set default Java Version Manually

If you maintain multiple versions of java including the OpenJDK java version on your server at the same time you may need to switch between java versions manually.

Start by listing your current java environment variable settings:

$ sudo update-alternatives --get-selections | grep ^java
java                           manual   /usr/lib/jvm/java-8-oracle/jre/bin/java
javac                          manual   /usr/lib/jvm/java-8-oracle/bin/javac
javadoc                        manual   /usr/lib/jvm/java-8-oracle/bin/javadoc
javafxpackager                 manual   /usr/lib/jvm/java-8-oracle/bin/javafxpackager
javah                          manual   /usr/lib/jvm/java-8-oracle/bin/javah
javap                          manual   /usr/lib/jvm/java-8-oracle/bin/javap
javapackager                   manual   /usr/lib/jvm/java-8-oracle/bin/javapackager
javaws                         manual   /usr/lib/jvm/java-8-oracle/jre/bin/javaws
javaws.real                    auto     /usr/lib/jvm/java-9-oracle/bin/javaws.real


For more verbose version of the above command execute sudo update-alternatives --get-selections | grep java.

To set java to eg. Java 9 executable run:

$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                     Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      auto mode
* 1            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      manual mode
  2            /usr/lib/jvm/java-9-oracle/bin/java       1         manual mode

Press  to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-9-oracle/bin/java to provide /usr/bin/java (java) in manual mode
Confirm your selection:

$ sudo update-alternatives --get-selections | grep ^java
java                           manual   /usr/lib/jvm/java-9-oracle/bin/java
javac                          manual   /usr/lib/jvm/java-8-oracle/bin/javac
javadoc                        manual   /usr/lib/jvm/java-8-oracle/bin/javadoc
javafxpackager                 manual   /usr/lib/jvm/java-8-oracle/bin/javafxpackager
javah                          manual   /usr/lib/jvm/java-8-oracle/bin/javah
javap                          manual   /usr/lib/jvm/java-8-oracle/bin/javap
javapackager                   manual   /usr/lib/jvm/java-8-oracle/bin/javapackager
javaws                         manual   /usr/lib/jvm/java-8-oracle/jre/bin/javaws
javaws.real                    auto     /usr/lib/jvm/java-9-oracle/bin/javaws.real

Use the update-alternatives --config JAVA-EXECUTABLE-HERE to change the environmental path to any other java executable binaries as required.

Install Java using the Official Oracle binaries

The following section will describe a manual Oracle Java installation on Ubuntu 18.04.

Java Download

Navigate your browser to the official Oracle java download page and download the latest binaries.

We are interested in eg. jdk-10.0.1_linux-x64_bin.tar.gz file.

Download java file and save it into your home directory:

$ ls ~/jdk-10.0.1_linux-x64_bin.tar.gz 
/home/linuxconfig/jdk-10.0.1_linux-x64_bin.tar.gz

Install Java on Ubuntu 18.04

Now, that your java download is completed and you have obtained the Oracle JDK binaries, execute the following commands to perform the java ubuntu install into a /opt/java-jdk directory:

$ sudo mkdir /opt/java-jdk
$ sudo tar -C /opt/java-jdk -zxf ~/jdk-10.0.1_linux-x64_bin.tar.gz

Set Defaults

The following commands will set Oracle JDK as system wide default. Amend the below commands to suit your installed version:

$ sudo update-alternatives --install /usr/bin/java java /opt/java-jdk/jdk-10.0.1/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /opt/java-jdk/jdk-10.0.1/bin/javac 1

Confirm Java Installation

What remains is to check for installed java version:

$ java --version
java 10.0.1 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
$ javac --version
javac 10.0.1

No comments:

Post a Comment