Free How to Install Java on Ubuntu 18:04 – Java is one of the most popular programming languages in the world , used to build various types of cross-platform applications.

How to install Java on Ubuntu 18
How to Install Java on Ubuntu

This tutorial explains how to install different versions of OpenJDK and Oracle Java on Ubuntu 18:04. The same instructions apply to Ubuntu 16:04 and any Ubuntu-based distribution, including Ubuntu, Linux Mint, and Elementary OS.

Requirements How to Install Java on Ubuntu

Before proceeding with the tutorial how to install Java at 18:04 ubuntu, make sure you sign in as a user with sudo privileges. Read: How to Create a User Sudo and Sudo Group in Ubuntu

variations in Java

Java is distributed in three different editions, Standard Edition (SE), Enterprise Edition (EE) and Micro Edition (ME). This tutorial covers the installation editions of Java SE (Standard Edition).

OpenJDK and Oracle Java are two main implementations of Java, with almost no difference between them except that Oracle Java has several additional commercial features.

There are two different Java packages in the Ubuntu repositories, Java Runtime Environment (JRE) and Java Development Kit (JDK).

If you only want to run Java programs, then you need a JRE that contains only the Java Runtime Environment. Java developers must install the JDK, which also includes the development / debugging tools and libraries.

I’m going to show you how to install various Java packages. If you do not know Java implementation or which version is used, the general recommendation is to keep using the default OpenJDK version available in Ubuntu 18:04.

Instal OpenJDK Default (Java 11)

At the time of writing, the latest LTS version of Java is version 11.

Follow the steps below to install the Java OpenJDK 11 on your Ubuntu system:

First, the index update package aptwith:

$ sudo apt update

Once the index is updated package, install the default OpenJDK Java packages with:

$ sudo apt install default-jdk

Verify installation, by running the following command will print the version of Java:

$ java -version

The output will look something like this:

Output
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)

And now you should have successfully install Java on your Ubuntu system.

JRE included in the JDK packages. If you only need the JRE, install the package default-jre:

Instal OpenJDK 8

Java 8 is still a Java version of the most widely used. If your application requires Java 8, you can install it by typing the following command:

$ sudo apt update
$ sudo apt install openjdk-8-jdk

Instal Oracle Java

Before installing the Oracle Java make sure you read the License Oracle JDK. The license only permits non-commercial use of the software, such as personal use and the use of development.

Oracle Java 11 can be installed on Linux PPA Uprising.

The following steps explain how to install Ubuntu Oracle Java 11 at 18:04:

Install the dependencies needed to add a new repository:

$ sudo apt install software-properties-common

Enable Linux uprising PPA by running the following command:

$ sudo add-apt-repository ppa:linuxuprising/java

After the repository is added, update the list of packages and install the package oracle-java11-installerby typing:

$ sudo apt update
$ sudo apt install oracle-java11-installer

You will be prompted to accept the license Oracle.

Verify the installation by running the following command will print version of R:

$ java -version

Output

java version "11.0.2" 2019-01-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)

Java Version Default Configuration

To check the default Java version, you can use the following command:

$ java -version

Output

openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)

If you have some Java installation to change the default version, use the tool update-alternativesas shown below:

$ sudo update-alternatives --config java

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

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

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

To change the default Java version please enter the version number (a number in the Selection) and press Enter.

Configuring Environment Variables JAVA_HOME

Some applications are written in Java using environment variables JAVA_HOMEto specify the location of the Java installation.

To set Environment variables JAVA_HOME, first, you need to know the path of the Java installation using the commandupdate-alternatives

$ sudo update-alternatives --config java

In my case, the installation path is as follows:

  • OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java
  • OpenJDK 8 is located at /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

Copy the installation path of the installation of your choice. Next, open the file /etc/environment:

$ sudo nano /etc/environment

Add the following line, at the end of the file:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Make sure you change the path with the path to the Java version of your choice.

You can log out and log in or run the following command resources to apply the changes to your current session:

$ source /etc/environment

To verify that the environment variable JAVA_HOMEis set correctly, run the following echo command:

$ echo $JAVA_HOME

Output
/usr/lib/jvm/java-11-openjdk-amd64

/etc/environmentis a system configuration file, which is used by all users. If you want to set a variable JAVA_HOMEon a per user, add a line .bashrcor other configuration files that are loaded when the user logs in.

uninstall Java

If for any reason you want to uninstall Java package, you can uninstall it like any other package installed apt.

For example, if you want to uninstall a package openjdk-8-jdk:

$ sudo apt remove openjdk-8-jdk

Conclusion

In the tutorial guide to install Java on Ubuntu 18:04, you learn how to install and manage multiple versions of Java on your Ubuntu server.

You can now install applications that run in Java, such as Tomcat, JBoss / WildFly, Apache Maven, Glassfish, Elasticsearch, Cassandra, Jenkins, Gradle … etc