- Install the Java Runtime Environment
- What you’ll learn
- What you’ll need
- 2. Installing OpenJDK JRE
- 3. Installing Oracle HotSpot JRE
- Downloading the Oracle HotSpot JRE binaries
- Installing
- Post-installation steps
- Need further assistance?
- How To Install Java with Apt on Ubuntu 20.04
- Prerequisites
- Installing the Default JRE/JDK
- Installing Oracle JDK 11
- Managing Java
- Setting the JAVA_HOME Environment Variable
- Conclusion
Install the Java Runtime Environment
The Java Runtime Environment (JRE) is required to run Java programs. Nowadays there are many JRE packages available from a variety of projects and companies, but the two most popular on Ubuntu are OpenJDK and Oracle HotSpot. Using one package over the other should not create any functional difference in most applications; however, some prefer OpenJDK over Oracle HotSpot as the former does not contain closed-source components, has a much clearer licensing and support policy, and is maintained as part of the Ubuntu archive, with easier installation and upgrades.
In this guide, we’ll be going through the installation of both JRE packages. Of course, you generally only need to pick the one that best suits your needs and preferences.
What you’ll learn
What you’ll need
That’s all you need. If you have that, let’s proceed to the next step!
2. Installing OpenJDK JRE
With new versions of Java released every 6 months, there are multiple versions available for use. Nowadays, Java 11 is the current Long Term Support (LTS) version, but Java 8 is still widely used. Moreover, the non LTS versions of Java are bringing a steady stream of innovation into the language, and also see some adoption.
Ubuntu offers the default-jre package, which is regularly updated to ship the latest version of the current OpenJDK JRE in Long Term Support (LTS). The default-jre is an excellent choice for most situations, thanks to the outstanding track of backwards compatibility of the Java Virtual Machine.
(Alternatively, you may opt to use a specific Java version, using for example the openjdk-11-jre package; as updates are released for that version of the Java Virtual Machine, that packages will be updated, allowing you to stick to the latest and greatest update of one specific version of the Java language.)
To install the OpenJDK JRE, we run:
sudo apt install default-jre
We can check if OpenJDK JRE was properly installed by running:
It should output the following:
openjdk version "11.0.11" 2021-04-20 OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2) OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2, mixed mode)
(Although the output may change in the future as new Java versions are promoted to LTS status, or the current LTS version receives updates.)
In the next step we’ll install Oracle HotSpot JRE.
3. Installing Oracle HotSpot JRE
Downloading the Oracle HotSpot JRE binaries
Download JRE binaries in .tar.gz (tarball) by heading over to their website. An Oracle account is needed to download the Oracle HotSpot JRE.
Oracle does not currently offer JRE packages for Java 11 or above from their website so, for this tutorial, we will use the Oracle HotSpot JRE version to be 8u291 (Java 8, update 291).
Installing
Create a directory to install JRE in with:
Move the JRE binaries into the directory:
sudo mv jre-8u291-linux-x64.tar.gz /usr/local/java
Go into the install directory:
sudo tar zxvf jre-8u291-linux-x64.tar.gz
Post-installation steps
To save space, delete the tarball by running:
sudo rm jre-8u291-linux-x64.tar.gz
Let the system know where JRE is installed:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jre1.8.0_291/bin/java" 1
After that’s done, check the installation by running:
It should output the following:
java version "1.8.0_291" Java(TM) SE Runtime Environment (build 1.8.0_291-b10) Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
Need further assistance?
How To Install Java with Apt on Ubuntu 20.04
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.
Prerequisites
To follow this tutorial, you will need:
- One Ubuntu 20.04 server set up by following the the Ubuntu 20.04 initial server setup guide tutorial, including a sudo non-root user and a firewall.
Installing the Default JRE/JDK
The easiest option for installing Java is to use the version packaged with Ubuntu. By default, Ubuntu 20.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 openjdk-11-jre-headless # version 11.0.11+9-0ubuntu2~20.04, or sudo apt install default-jre # version 2:1.11-72 sudo apt install openjdk-13-jre-headless # version 13.0.7+5-0ubuntu1~20.04 sudo apt install openjdk-16-jre-headless # version 16.0.1+9-1~20.04 sudo apt install openjdk-8-jre-headless # version 8u292-b10-0ubuntu1~20.04
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.20.04) OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.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:
Next, let’s look at how to install Oracle’s official JDK and JRE.
Installing Oracle JDK 11
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 Focal, as shown in the following figure:
In this image, the version of the script is 11.0.7 . In this case, you would need Oracle JDK 11.0.7. Your version number may vary depending on when you’re installing the software.
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:
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:
Output Oracle Java 11 (LTS) and 16 installer for Ubuntu (21.04, 20.10, 20.04, 18.04, 16.04 and 14.04), Pop!_OS, 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, 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.
Managing Java
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 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 .
Setting the JAVA_HOME Environment Variable
Many 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 to keep the current choice[*], or type selection number:
In this case the installation paths are as follows:
- OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
- Oracle Java is located at /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.
Conclusion
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.