Install Java On Ubuntu: A Beginner’s Guide

Posted on

Java stands out as a highly favored programming language for creating a wide array of web applications. Its adaptability and rich feature set make it a preferred choice for both aspiring and seasoned developers. For websites hosted on an Ubuntu VPS, installing Java is a crucial starting point for developing and running Java-based applications.

Whether Java is needed for software development or to operate Java-based tools, Ubuntu simplifies the installation process. This article will guide you through the steps to install Java on Ubuntu, ensuring that your system is properly configured for Java development and execution.

Prerequisites

An Ubuntu 22.04 server with a sudo non-root user and a firewall enabled.

Step 1: Installing Java

The initial step involves installing Java, which includes two primary components: the JDK and the JRE. The JDK provides essential development tools, like a compiler and debugger. Oracle develops and maintains these versions, while OpenJDK versions benefit from community contributions due to their open-source nature.

Installing the Default JRE/JDK

The first installation option is to use the Java version bundled with Ubuntu. Ubuntu 22.04 includes OpenJDK 11, an open-source version of the JRE and JDK. To begin, update your apt package index, then install the OpenJDK version of Java.

sudo apt update

Next, verify if Java is installed with this command:

java –version

If Java is not installed, you will see the following output:

Output

Command 'java' not found, but can be installed with:

sudo apt install default-jre         # version 2:1.11-72build1, or

sudo apt install openjdk-11-jre-headless  # version 11.0.14+9-0ubuntu2

sudo apt install openjdk-17-jre-headless  # version 17.0.2+8-1

sudo apt install openjdk-18-jre-headless  # version 18~36ea-1

sudo apt install openjdk-8-jre-headless   # version 8u312-b07-0ubuntu1

To install the JRE from OpenJDK 11, run the following:

sudo apt install default-jre

The JRE allows for the execution of almost all Java software.

Confirm the installation with:

java –version

You should see an output similar to:

Output

openjdk version "11.0.14" 2022-01-18

OpenJDK Runtime Environment (build 11.0.14+9-Ubuntu-0ubuntu2)

OpenJDK 64-Bit Server VM (build 11.0.14+9-Ubuntu-0ubuntu2, mixed mode, sharing)

Some Java applications may also require the JDK. Install the JDK with the following, which includes the JRE:

sudo apt install default-jdk

Check the installation of the JDK via the version of Javac, the Java compiler:

javac -version

You’ll see an output like:

Output

javac 11.0.14

Installing Oracle JDK 11

Oracle’s website provides downloads of the JDK directly when you create an account. This method involves adding a package repository for your desired version.

You can use apt to install it via a third-party installation script. Oracle JDK includes the JRE so a separate JRE installation is not needed.

The specific version of Oracle’s JDK you download must align with the installer script’s requirements. Check the oracle-java11-installer page to determine the correct version.

oracle-java11-installer page

The image shows JavaScript version 11.0.13 so you’ll need Oracle JDK 11.0.13, which can vary based on the software you are using. You can find these versions in the Archive Downloads. It’s not necessary to download from this page itself as you can do it through apt.

JavaScript version 11.0.13 script

From the list below, select the Linux x64 compressed archive .tar.gz package:

Linux x64 compressed archive .tar.gz package

Clicking ‘Download’ after accepting the Oracle license agreement by ticking the checkbox will start the download.

After downloading the file, upload it to your server. Use the `scp` command to transfer the file to the home directory of your user. On MacOS, Linux, or Windows with the WSL (Windows Subsystem for Linux), the command will look like this if the file is in your Downloads folder:

scp Downloads/jdk-11.0.13_linux-x64_bin.tar.gz sammy@your_server_ip:~

Once the files are uploaded, return to your server and add the third-party repository, allowing you to install Oracle’s Java.

Import the signing key for verification of the new software:

sudo gpg --homedir /tmp --no-default-keyring --keyring /usr/share/keyrings/oracle-jdk11-installer.gpg --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A

You will see the following output:

Output
gpg: keybox '/usr/share/keyrings/oracle-jdk11-installer.gpg' created
gpg: /tmp/trustdb.gpg: trustdb created
gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" imported
gpg: Total number processed: 1
   gpg:           imported: 1

Next, include the repository in the package sources list:

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/oracle-jdk11-installer.gpg] https://ppa.launchpadcontent.net/linuxuprising/java/ubuntu jammy main" | sudo tee /etc/apt/sources.list.d/oracle-jdk11-installer.list > /dev/null

Update your package list to make the software available for installation:

sudo apt update

The installer looks for the downloaded Oracle JDK in `/var/cache/oracle-jdk11-installer-local`. Create this directory and move the Oracle JDK archive there:

sudo mkdir -p /var/cache/oracle-jdk11-installer-local/
sudo cp jdk-11.0.13_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/

Finally, install the package:

sudo apt install oracle-java11-installer-local

Step 2: Managing Java

Multiple Java installations can exist on a single server, with `update-alternatives` command used to configure which version is default.

sudo update-alternatives --config java

If both versions of Java are installed, you’ll see an output like:

Output

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     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:

To set a default Java version, input the number associated with it or press ‘Enter’ without making a selection. For other Java commands, like the compiler (`javac`), you should do the same.

sudo update-alternatives --config javac

Step 3: Setting the JAVA_HOME Environment Variable

Certain Java programs rely on the JAVA_HOME environment variable to specify the location of the Java installation. Locate the installation path by running the following:

sudo update-alternatives --config java

This command will output all of your Java installations and their paths:

Output

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     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:

Based on the above, the installation paths are:

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.

Open the `/etc/environment` file with `nano` or another text editor and, copy the path for your chosen installation.

Add the line below to the end of this file, making sure to use your preferred location. Do not include the `bin/` portion of path:

/etc/environment

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

Modifying this file establishes the JAVA_HOME path for all users on your system.

Save and close the file.

Apply these changes to your current sessions:

source /etc/environment

To verify this, run:

echo $JAVA_HOME

You will see the path you previously set:

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

For other users to use this setting, they will need to execute the command `source /etc/environment`, or logout and login again.

Conclusion

Installing Java on Ubuntu prepares your system for many development tasks. By following this guide, you can easily install either the Java Runtime Environment (JRE) or the Java Development Kit (JDK) based on your requirements.

With Java set up, you can start developing and running Java applications smoothly. Ensure you keep your Java version updated for the latest benefits and better security. Any problems that may arise with this installation should easily be solved with the help of the Ubuntu community.

Leave a Reply

Your email address will not be published. Required fields are marked *