The author selected the Open Internet/Free Speech Fund to receive a $100 donation as part of the Write for DOnations program.
Java and the JVM (Java’s virtual machine) are required for many kinds of software, including Tomcat, Jetty, Glassfish, Cassandra and Jenkins.
In this guide, you will install various versions of the Java Runtime Environment (JRE) and the Java Developer Kit (JDK) using apt
. You’ll install OpenJDK as well as the official JDK from Oracle. You’ll then select the version you wish to use for your projects. When you’re finished, you’ll be able to use the JDK to develop software or use the Java Runtime to run software.
To follow this tutorial, you will need:
The easiest option for installing Java is to use the version packaged with Ubuntu. By default, Ubuntu 18.04 includes Open JDK 11, which is an open-source variant of the JRE and JDK.
To install this version, first update the package index:
Next, check if Java is already installed:
If Java is not currently installed, you’ll see the following output:
OutputCommand 'java' not found, but can be installed with:
sudo apt install default-jre
sudo apt install openjdk-11-jre-headless
sudo apt install openjdk-8-jre-headless
Execute the following command to install the default Java Runtime Environment (JRE), which will install the JRE from OpenJDK 11:
The JRE will allow you to run almost all Java software.
Verify the installation with:
You’ll see output similar to the following:
Outputopenjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.18.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.18.04, mixed mode, sharing))
You may need the Java Development Kit (JDK) in addition to the JRE in order to compile and run some specific Java-based software. To install the JDK, execute the following command, which will also install the JRE:
Verify that the JDK is installed by checking the version of javac
, the Java compiler:
You’ll see the following output:
Outputjavac 11.0.11
Next, let’s look at how to install Oracle’s official JDK and JRE.
Oracle’s licensing agreement for Java doesn’t allow automatic installation through package managers. To install the Oracle JDK, which is the official version distributed by Oracle, you must create an Oracle account and manually download the JDK to add a new package repository for the version you’d like to use. Then you can use apt
to install it with help from a third party installation script.
The version of Oracle’s JDK you’ll need to download must match version of the installer script. To find out which version you need, visit the oracle-java11-installer
page.
Locate the package for Bionic, as shown in the following figure:
In this image, the version of the script is 11.0.7
. In this case, you’ll need Oracle JDK 11.0.7. You don’t need to download anything from this page; you’ll download the installation script through apt
shortly.
Then visit the Downloads page and locate the version that matches the one you need.
Click the JDK Download button and you’ll be taken to a screen that shows the versions available. Click the .tar.gz
package for Linux.
You’ll be presented with a screen asking you to accept the Oracle license agreement. Select the checkbox to accept the license agreement and press the Download button. Your download will begin. You may need to log in to your Oracle account one more time before the download starts.
Once the file has downloaded, you’ll need to transfer it to your server. On your local machine, upload the file to your server. On macOS, Linux, or Windows using the Windows Subsystem for Linux, use the scp
command to transfer the file to the home directory of your sammy
user. The following command assumes you’ve saved the Oracle JDK file to your local machine’s Downloads
folder:
Once the file upload has completed, return to your server and add the third-party repository that will help you install Oracle’s Java.
Install the software-properties-common
package, which adds the add-apt-repository
command to your system:
Next, import the signing key used to verify the software you’re about to install:
You’ll see this output:
Outputgpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" imported
gpg: Total number processed: 1
gpg: imported: 1
Then use the add-apt-repository
command to add the repo to your list of package sources:
You’ll see this message:
Output Oracle Java 11 (LTS) and 12 installer for Ubuntu, Linux Mint and Debian.
Java binaries are not hosted in this PPA due to licensing. The packages in this PPA download and install Oracle Java 11, so a working Internet connection is required.
The packages in this PPA are based on the WebUpd8 Oracle Java PPA packages: https://launchpad.net/~webupd8team/+archive/ubuntu/java
Created for users of https://www.linuxuprising.com/
Installation instructions (with some tips), feedback, suggestions, bug reports etc.:
. . .
Press [ENTER] to continue or ctrl-c to cancel adding it
Press ENTER
to continue the installation. You may see a message about no valid OpenPGP data found
, but you can safely ignore this.
Update your package list to make the new software available for installation:
The installer will look for the Oracle JDK you downloaded in /var/cache/oracle-jdk11-installer-local
. Create this directory and move the Oracle JDK archive there:
Finally, install the package:
The installer will first ask you to accept the Oracle license agreement. Accept the agreement, then the installer will extract the Java package and install it.
Now let’s look at how to select which version of Java you want to use.
You can have multiple Java installations on one server. You can configure which version is the default for use on the command line by using the update-alternatives
command.
This is what the output would look like if you’ve installed both versions of Java in this tutorial:
OutputThere are 2 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-11-oracle/bin/java 1091 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Choose the number associated with the Java version to use it as the default, or press ENTER
to leave the current settings in place.
You can do this for other Java commands, such as the compiler (javac
):
Other commands for which this command can be run include, but are not limited to: keytool
, javadoc
and jarsigner
.
JAVA_HOME
Environment VariableMany programs written using Java use the JAVA_HOME
environment variable to determine the Java installation location.
To set this environment variable, first determine where Java is installed. Use the update-alternatives
command:
This command shows each installation of Java along with its installation path:
OutputThere are 2 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-11-oracle/bin/java 1091 manual mode
Press <enter> to keep the current choice[*], or type selection number:
In this case the installation paths are as follows:
/usr/lib/jvm/java-11-openjdk-amd64/bin/java.
/usr/lib/jvm/java-11-oracle/jre/bin/java
.Copy the path from your preferred installation. Then open /etc/environment
using nano
or your favorite text editor:
At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path, but do not include the bin/
portion of the path:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Modifying this file will set the JAVA_HOME
path for all users on your system.
Save the file and exit the editor.
Now reload this file to apply the changes to your current session:
Verify that the environment variable is set:
You’ll see the path you just set:
Output/usr/lib/jvm/java-11-openjdk-amd64
Other users will need to execute the command source /etc/environment
or log out and log back in to apply this setting.
In this tutorial you installed multiple versions of Java and learned how to manage them. You can now install software which runs on Java, such as Tomcat, Jetty, Glassfish, Cassandra or Jenkins.
Get Ubuntu on a hosted virtual machine in seconds with DigitalOcean Droplets. Simple enough for any user, powerful enough for fast-growing applications or businesses.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Thanks Koen,
But the $JAVA_HOME should be
/usr/lib/jvm/java-11-openjdk-amd64/
So in case of using Oracle Java 8 it would beExactly! “bin” should be removed. Smth like: export JAVA_HOME=“/usr/lib/jvm/java-11-openjdk-amd64/”
its a good and very useful thank you!
Thanks. Very useful article
Thanks! It is very useful.
it works thank you
This happened. Not sure how to proceed.
–2018-10-16 15:01:26-- http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.tar.gz?AuthParam=1539716606_e0a913d67b440504da28a61c62775de6 Connecting to download.oracle.com (download.oracle.com)|23.36.32.122|:80… connected. HTTP request sent, awaiting response… 404 Not Found 2018-10-16 15:01:26 ERROR 404: Not Found.
download failed Oracle JDK 8 is NOT installed. dpkg: error processing package oracle-java8-installer (–configure): installed oracle-java8-installer package post-installation script subprocess returned error exit status 1 E: Sub-process /usr/bin/dpkg returned an error code (1)
Cheers, Steve
thanks
I have downloaded, extracted and set path variable for Zulu Java platform, but when i run $sudo update-alternatives --config java there is no Zulu version. Do you know what could i have done wrong?
PATH=“/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/zulu8zulu8.28.0.1-jdk8.0.163-linux_x64/bin” JAVA_HOME=“/usr/lib/jvm/zulu8.28.0.1-jdk8.0.163-linux_x64”
There are 2 choices for the alternative java (providing /usr/bin/java).
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
This article fails to mention a default install of Ubuntu 18.04 will not source /etc/environment by default.
You need to add the following line to .bashrc source /etc/environment
Please add to the guide.
I have added source /etc/environment at the start of bash.bashrc file but again cant recognize Zulu. Thanks.
Thanks, that’s pretty helpful
The WebUpd8 PPA for Oracle Java in this tutorial is no longer supported since Oracle changed licensing and requires a login for downloads.
To that end, that PPA now 404s if anyone adds it. This tutorial is thus EXTREMELY out of date and needs updated with manual installation mechanisms for Oracle Java.
awesome post
Thanks! It helps me a lot!
Oracle has discontinued the ppa. So getting the following issue:
Thanks,save my valuable time.
Please update this tutorial. The installation of
oracle-java8-installer
is super outdated and not working.Not working getting error
Any other way to install java on ubuntu 18.04. I just want to intall elasticsearch
I’m not able to install Oracle’s official JDK dist.
Tutorial needs to be updated) Oracle changed PPA)
This PPA is outdated. If you’re trying to install Elasticsearch and run into the
E: Package 'oracle-java8-installer' has no installation candidate
message. The following will help you:The output should be something like:
Cheers!
Thanks for this comment! This tutorial definitely is due for an update.
The WebUpd8 PPA for Oracle Java in this tutorial is no longer supported since Oracle changed licensing and requires a login for downloads.
To that end, that PPA now 404s if anyone adds it. This tutorial is thus EXTREMELY out of date and needs updated with manual installation mechanisms for Oracle Java.
awesome post
Thanks! It helps me a lot!
Oracle has discontinued the ppa. So getting the following issue:
Thanks,save my valuable time.
Please update this tutorial. The installation of
oracle-java8-installer
is super outdated and not working.Not working getting error
Any other way to install java on ubuntu 18.04. I just want to intall elasticsearch
I’m not able to install Oracle’s official JDK dist.
Tutorial needs to be updated) Oracle changed PPA)
This PPA is outdated. If you’re trying to install Elasticsearch and run into the
E: Package 'oracle-java8-installer' has no installation candidate
message. The following will help you:The output should be something like:
Cheers!
Thanks for this comment! This tutorial definitely is due for an update.
I have downloaded, extracted and set path variable for Zulu Java platform, but when i run $sudo update-alternatives –config java there is no Zulu version. Do you know what could i have done wrong?
Oracle still supports JDK 8, and the latest version as of now is jdk-8u261-linux-x64.tar.gz. Does the installer support this package?
If yes, is it looking under /var/cache/oracle-jdk8-installer-local/ ?
And should I install oracle-java8-installer-local ?
I tried both and I am getting: E: Unable to locate package oracle-java8-installer-local E: Unable to locate package oracle-jdk8-installer-local
Getting below error :
locale: Cannot set LC_ALL to default locale: No such file or directory Before installing this package, please download the Oracle JDK 11 .tar.gz file with the same version as this package (version 11.0.4), and place it in /var/cache/oracle-jdk11-installer-local,
E.g.: sudo mkdir -p /var/cache/oracle-jdk11-installer-local sudo cp jdk-11.0.4_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/ sha256sum mismatch jdk-11.0.10_linux-x64_bin.tar.gz Oracle JDK 11 is NOT installed. dpkg: error processing package oracle-java11-installer-local (–configure): installed oracle-java11-installer-local package post-installation script subprocess returned error exit status 1 Errors were encountered while processing: oracle-java11-installer-local E: Sub-process /usr/bin/dpkg returned an error code (1)
Just kill me already!
-I cant install Oracle JDK 11 or run Elasticsearch!
-The package for Bionic thats listed on the oracle-java-installer page is 11.0.13 but the only option available for download is jdk-11.0.14_linux-x64_bin.tar.gz.
-I ran this anyway: $sudo mkdir -p /var/cache/oracle-jdk11-installer-local/ $sudo cp jdk-11.0.14_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/ $sudo apt install oracle-java11-installer-local
-Output: Preparing to unpack …/oracle-java11-installer-local_11.0.13-1~linuxuprising0_amd64.deb … oracle-license-v1-2 license has already been accepted Unpacking oracle-java11-installer-local (11.0.13-1~linuxuprising0) over (11.0.13-1~linuxuprising0) … Setting up oracle-java11-installer-local (11.0.13-1~linuxuprising0) … Before installing this package, please download the Oracle JDK 11 .tar.gz file with the same version as this package (version 11.0.4), and place it in /var/cache/oracle-jdk11-installer-local,
E.g.: sudo mkdir -p /var/cache/oracle-jdk11-installer-local sudo cp jdk-11.0.4_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/ sha256sum mismatch jdk-11.0.13_linux-x64_bin.tar.gz Oracle JDK 11 is NOT installed. dpkg: error processing package oracle-java11-installer-local (–configure): installed oracle-java11-installer-local package post-installation script subprocess returned error exit status 1 Processing triggers for shared-mime-info (1.9-2) … Processing triggers for mime-support (3.60ubuntu1) … Errors were encountered while processing: oracle-java11-installer-local E: Sub-process /usr/bin/dpkg returned an error code (1)
-I ran this next but it didnt help: $ sudo rm /var/lib/dpkg/info/oracle-java11-installer-local.postinst -f $ sudo dpkg --configure -a Setting up oracle-java11-installer-local (11.0.13-1~linuxuprising0) …
-and this: $ sudo update-alternatives --config java There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-11-openjdk-amd64/bin/java Nothing to configure.
-I followed the rest of the tutorial.
-Elasticsearch will not start: $ systemctl status elasticsearch.service ● elasticsearch.service - Elasticsearch Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Wed 2022-04-13 02:09:32 UTC; 38s ago Docs: https://www.elastic.co Process: 19504 ExecStart=/usr/share/elasticsearch/bin/systemd-entrypoint -p ${PID_DIR}/elasticsearch.pid --quiet (code=exited, status=1/FAILURE) Main PID: 19504 (code=exited, status=1/FAILURE)
Apr 13 02:09:32 endCiv systemd-entrypoint[19504]: error: Apr 13 02:09:32 endCiv systemd-entrypoint[19504]: OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000e7600000, 413138944, 0) failed; error=‘Not enough space’ ( Apr 13 02:09:32 endCiv systemd-entrypoint[19504]: at org.elasticsearch.tools.launchers.JvmOption.flagsFinal(JvmOption.java:119) Apr 13 02:09:32 endCiv systemd-entrypoint[19504]: at org.elasticsearch.tools.launchers.JvmOption.findFinalOptions(JvmOption.java:81) Apr 13 02:09:32 endCiv systemd-entrypoint[19504]: at org.elasticsearch.tools.launchers.JvmErgonomics.choose(JvmErgonomics.java:38) Apr 13 02:09:32 endCiv systemd-entrypoint[19504]: at org.elasticsearch.tools.launchers.JvmOptionsParser.jvmOptions(JvmOptionsParser.java:135) Apr 13 02:09:32 endCiv systemd-entrypoint[19504]: at org.elasticsearch.tools.launchers.JvmOptionsParser.main(JvmOptionsParser.java:86) Apr 13 02:09:32 endCiv systemd[1]: elasticsearch.service: Main process exited, code=exited, status=1/FAILURE Apr 13 02:09:32 endCiv systemd[1]: elasticsearch.service: Failed with result ‘exit-code’. Apr 13 02:09:32 endCiv systemd[1]: Failed to start Elasticsearch.