Java is one of the most popular programming languages used for building different web applications. It possesses a versatile nature and offers different features. Hence, both beginners and experienced developers prefer this website framework. If your website is hosted on Ubuntu VPS, installing Java is a major step to start developing and running Java-based applications.
Whether you need Java for software development or to run Java-based tools, Ubuntu makes the installation process simple. In this knowledge-based article, we will walk you through the steps required to install Java on Ubuntu, ensuring that your system is set up properly for Java development and execution.
Prerequisites
Ubuntu 22.04 server set up that includes a sudo non-root user and a firewall.
Step 1: Installing Java
The first step is to install Java which is one of two main components JDK and JRE. The JDK provides essential software tools for working in Java, such as a compiler and debugger. Oracle has developed and recognized these versions. OpenJDK has the addition of community contributions due to its open-source nature.
Installing the Default JRE/JDK
The first option for installing Java is to use the version packaged with Ubuntu. By default, Ubuntu 22.04 includes Open JDK 11, which is an open-source variant of the JRE and JDK. First, update your apt package index and install the OpenJDK version of Java.
sudo apt update
Next, check the installation status of Java.
java –version
You will get the following output if Java is not installed.
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
If you want to install the JRE from OpenJDK 11, execute the following command.
sudo apt install default-jre
The JRE will allow you to run almost all Java software.
Verify the installation with:
java –version
Get a similar output to the following:
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-based software may require the JDK in addition to the JRE. Run the following command to install the JDK, which will also install the JRE:
sudo apt install default-jdk
Verify that the JDK is installed by checking the version of Javac, the Java compiler:
javac -version
You’ll see the following output:
Output
javac 11.0.14
Installing Oracle JDK 11
Oracle’s official website allows you to create an account on it and manually download the JDK. It is done to add a new package repository for the version you would like to use.
Then you can use apt to install it with help from a third-party installation script. Oracle JDK comes with the JRE included, so you don’t need to install that separately.
The version of Oracle’s JDK you’ll need to download must match the version of the installer script. To find out which version you need, visit the oracle-java11-installer page.
In this image, the JavaScript version 11.0.13 script is seen. Hence, you will be requiring Oracle JDK 11.0.13. The version number varies according to the software you are installing. However, no need to download anything from this page. You can do the same through apt shortly. Locate the version you need in the Archive Downloads.
From the given below list, you have to choose the Linux x64 compressed archive .tar.gz package:
You’ll be asked to accept the Oracle license agreement. The license agreement can be accepted by selecting the checkbox and clicking Download.
Once the file has been downloaded, you’ll need to upload it to your server. Upload the file to your server from your local machine. Using the scp command, transfer the file to the home directory of your Sammy user on macOS, Linux, or Windows using the Windows Subsystem for Linux. If you saved the Oracle JDK file to the Downloads folder on your local machine, run the following command:
scp Downloads/jdk-11.0.13_linux-x64_bin.tar.gz sammy@your_server_ip:~
After uploading the files, return to your server and add the third-party repository to install Oracle’s Java.
To verify the software you’re about to install, import the signing key:
sudo gpg --homedir /tmp --no-default-keyring --keyring /usr/share/keyrings/oracle-jdk11-installer.gpg --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A
The output is here which you can see on your screen as well:
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
Now, add the repository to your 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 new software available for installation:
sudo apt update
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:
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, you are ready to install the package by executing this command.
sudo apt install oracle-java11-installer-local
Step 2: Managing Java
On one server, you can have multiple Java installations. You can configure the version which is the default for use on the command line by leveraging the update alternatives command.
sudo update-alternatives --config java
Here is an output which would like if you have installed both versions of Java in the tutorial:
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:
Decide the number that is associated with the Java version to use 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).
sudo update-alternatives --config javac
Step 3: Setting the JAVA_HOME Environment Variable
Several programs which are written in Java use the JAVA_HOME environment variable to decide the Java installation locations. Determine where Java is installed if you want to set this environment variable. Use the update-alternatives command:
sudo update-alternatives --config java
This command shows each installation of Java along with its installation path:
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:
You can follow these installation paths in this case:
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 /etc/environment using nano or your preferred text editor, then copy the path from your chosen installation.
You need to add the following line, at the end of this file. Ensure to replace the highlighted path with your own copied path. Also not include the bin/portion of the path:
/etc/environment
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.
Next, reload this file to apply the changes to your current session:
source /etc/environment
You need to verify that the variable environment is set or not:
echo $JAVA_HOME
You’ll see the path you just set:
Output
/usr/lib/jvm/java-11-openjdk-amd64
Other users will need to run the command source /etc/environment or log out and log back in to apply this setting.
Installing Java on Ubuntu is a straightforward process that prepares your system for a wide range of programming and development tasks. By following the steps outlined in this guide, you can easily set up the Java Runtime Environment (JRE) or the Java Development Kit (JDK) based on your needs.
With Java successfully installed, you can start building and running Java applications seamlessly. Make sure to keep your Java version updated to enjoy the latest features and security enhancements. If you encounter any issues during installation, the Ubuntu community offers extensive resources and support to help you resolve them quickly.