How To Install Oracle Java 8 and OpenJDK 11 on Ubuntu 18.04, 18.10

This tutorial is going to show you how to install Oracle Java 8 and OpenJDK 11 on Ubuntu 18.04/Ubuntu 18.10. Java 11 has been released on September 25, 2018 and it’s the first long term support (LTS) release since Oracle changed release schedule.

Java Release Model

Previously, Oracle releases a new major version of Java every two years, a minor version every 6 months. (It took 3 years to release Java 9, because of Jigsaw.)  Some people, often company management, like this model because it makes production stable. Other people, often developers, think this is too slow. They like innovations.

After Java 9, Oracle releases a new major version of Java every 6 months to make developers happy. Every 3 years, there will be a LTS release (Long Time Support), which is supported for 8 years, to appease company management. Java 11 is the first LTS release. The next LTS will be Java 17. Java 8 will be receiving updates through 2025. Non-LTS releases will no longer receive update once the next version comes out. That means Java 9 and Java 10 have already been phased out.

OpenJDK vs Oracle JDK

Oracle started providing its own OpenJDK builds since Java 9. Closed-source features of Oracle JDK, such as Java flight recorder and Java mission control, are pushed to OpenJDK. Starting with Java 11, Oracle JDK and Oracle’s OpenJDK builds are now functionally identical and interchangeable. The differences between the two are cosmetic, packaging and license. Oracle’s OpenJDK is released under GPL. If you want commercial support, you can use Oracle JDK, which is released under OTN (Oracle Technology Network) license.

Oracle’s OpenJDK builds are available at jdk.java.net. There are other distributions of OpenJDK, such as AdoptOpenJDK and Amazon Correcto.

Installing Oracle JDK 8 on Ubuntu 18.04/Ubuntu 18.10

Oracle JDK 8 (aka 1.8) is still using BCL (Binary Code License), so you can install and use it for free. You can manually download the tar.gz file from Oracle website, but using a PPA is more convenient to install and update. Run the following two commands one by one in your terminal to add the PPA and install Oracle Java 8.

sudo add-apt-repository ppa:webupd8team/java -y

sudo apt install java-common oracle-java8-installer oracle-java8-set-default

The second command will install the Oracle Java8 installer, which in turn downloads the latest version of Oracle JDK 8 from Oracle website.

Oracle JDK 8 ubuntu 18.04

The oracle-java8-set-default package will automatically set Oracle JDK8 as default. Once the installation is complete, we can check Java version.

java -version

Output:

java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

Check Java compiler version

javac -version

Output:

javac 1.8.0_191

Setting Up Java Environment Variable

If you check the JAVA_HOME environment variable now, it’s empty.

echo $JAVA_HOME

oracle Java 8 environment variable

The oracle-java8-set-default package installs two shell scripts /etc/profile.d/jdk.csh and /etc/profile.d/jdk.sh that can be used to set Java related environment variables. You can check the content of these two files by using cat command.

cat /etc/profile.d/jdk.sh

Output:

export J2SDKDIR=/usr/lib/jvm/java-8-oracle
export J2REDIR=/usr/lib/jvm/java-8-oracle/jre
export PATH=$PATH:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
export JAVA_HOME=/usr/lib/jvm/java-8-oracle
export DERBY_HOME=/usr/lib/jvm/java-8-oracle/db

We need to reload /etc/profile to let these environment variables to take effect.

source /etc/profile

Now Java related environment variables are set and you can check these 5 environment variables like below:

echo $JAVA_HOME

java 8 ubuntu 18.04

The /etc/profile script is automatically executed with each system boot, so you don’t have to execute it again next time.

Installing Oracle’s OpenJDK 11 on Ubuntu 18.04

Since Oracle’s OpenJDK 11 build has the same feature as Oracle JDK 11, you can use OpenJDK if you don’t need Oracle’s commercial support. It’s strongly recommended that you use the OpenJDK package from your system and package manager for better integration and and ease of update.

The openjdk-11-jdk package from Ubuntu 18.04 repository actually installs OpenJDK 10, which lacks the flight recorder feature. So we have to manually download OpenJDK 11 from http://jdk.java.net/11/. If you prefer terminal, the run the following command:

wget https://download.java.net/java/GA/jdk11/13/GPL/openjdk-11.0.1_linux-x64_bin.tar.gz

Then extract it to /usr/lib/jvm/ directory.

sudo tar xvf openjdk-11.0.1_linux-x64_bin.tar.gz --directory /usr/lib/jvm/

Now check Java version.

/usr/lib/jvm/jdk-11.0.1/bin/java -version

Output:

openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)

Installing Oracle’s OpenJDK 11 on Ubuntu 18.10

Run the following command to install OpenJDK 11 on Ubuntu 18.10 from the default repository.

sudo apt install openjdk-11-jdk

This will also install the openjdk-11-jre package, which contains the Java runtime environment and is required to run Java programs. Once the installation is complete, check version number.

java -version

Output:

openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment (build 11.0.1+13-Ubuntu-2ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.1+13-Ubuntu-2ubuntu1, mixed mode, sharing)

Setting the Default JDK

If you are using Ubuntu 18.04, you need to run the following commands first to add OpenJDK 11 to the alternatives system.

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-11.0.1/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-11.0.1/bin/javac 1

To set a default JDK on Ubuntu 18.04/18.10, run the following command:

sudo update-alternatives --config java

The available options will be listed. Type a number to select a default JDK. JDK 11 has significant stability, functionality and performance improvements compared to JDK 8, so you might want to choose JDK 11.

set default jdk ubuntu 18.04

You also need to do the same for Java compiler.

sudo update-alternatives --config javac

Now you can run java -version and javac -version command to check the default JDK.

To update Java related environment variables, for example, if you want to use Java 11, then edit /etc/profile.d/jdk.sh script.

sudo nano /etc/profile.d/jdk.sh

Comment out the original lines as a backup, then copy them to new lines and update the paths.

openjdk 11 environment variable

Save and close the file, then

source /etc/profile

That’s it! I hope this tutorial helped you install Oracle JDK 8 and OpenJDK 11 on Ubuntu 18.04/18.10. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial
[Total: 6 Average: 4.2]

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • If my answer helped you, please consider supporting this site. Thanks :)