Introduction
Java is a widely-used and versatile programming language that powers numerous applications, from enterprise software to Android apps. Installing the latest version of Java ensures optimal performance, security, and access to the latest features. OpenJDK, an open-source implementation of the Java Platform, offers a reliable choice for Java development. This article will provide you with a step-by-step guide on how to install Java using OpenJDK 17 on Ubuntu 22.04, enabling you to harness the power of Java for your projects.
Table of Contents
Understanding the Importance of Java
Java’s significance in the world of software development cannot be overstated. It’s a versatile language known for its platform independence and extensive libraries, making it suitable for applications ranging from web development to enterprise systems. OpenJDK, the open-source alternative to Oracle’s Java Development Kit (JDK), ensures a reliable and flexible development environment.
To develop and run Java applications, you need the Java Development Kit (JDK), which includes tools like the Java compiler (javac) for converting Java source code into bytecode and the Java Runtime Environment (JRE) to run Java applications on a computer. The JDK and JRE are available for different operating systems, including Windows, macOS, and various flavors of Linux.
Prerequisites
Below are the prerequisites for installing Java on Ubuntu 22.04.
- A Ubuntu Server 22.04.
- A non-root user account with sudo privileges set up on your system. You can learn how to do this by following the Initial Server Setup Guide with Ubuntu 22.04 tutorial.
Install Java on Ubuntu 22.04
Now that you’ve met the prerequisites, let’s delve into the installation process:
Step 1: System Update
Before installing OpenJDK, updating Ubuntu to ensure all system packages are current with the “apt update
” command is essential. This helps to prevent any potential conflicts during the installation process. To update your Ubuntu system, run the following command in your terminal:
samm@java:~$ sudo apt update
samm@java:~$ sudo apt upgrade
Step 2: Install Java on Ubuntu 22.04
There are two main options for installing Java.
- Method 1 – Install OpenJDK 17 with Ubuntu Repository
- Method 2 – Install OpenJDK 17 Manually
Right off the bat, log into your server instance via SSH. To verify if Java is installed, run the following command:
samm@java:~$ java -version
Command 'java' not found, but can be installed with:
sudo apt install default-jre # version 2:1.11-72build2, or
sudo apt install openjdk-11-jre-headless # version 11.0.20+8-1ubuntu1~22.04
sudo apt install openjdk-17-jre-headless # version 17.0.8+7-1~22.04
sudo apt install openjdk-18-jre-headless # version 18.0.2+9-2~22.04
sudo apt install openjdk-19-jre-headless # version 19.0.2+7-0ubuntu3~22.04
sudo apt install openjdk-8-jre-headless # version 8u382-ga-1~22.04.1
The output clearly indicates that Java is not installed. In addition, you get a few suggestions on how to install Java.
As you have seen, there are multiple ways of installing Java. However, we recommend the installation of OpenJDK (Open Java Development Kit).
Method 1: Install OpenJDK 17 with Ubuntu Repository
The first and most recommended method for installing OpenJDK is through the default Ubuntu repository. You can search for available packages using the “grep” command. To do this, run the following command in your terminal:
samm@java:~$ apt-cache search openjdk | grep openjdk-17
Output :
openjdk-17-dbg - Java runtime based on OpenJDK (debugging symbols)
openjdk-17-demo - Java runtime based on OpenJDK (demos and examples)
openjdk-17-doc - OpenJDK Development Kit (JDK) documentation
openjdk-17-jdk - OpenJDK Development Kit (JDK)
openjdk-17-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-17-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-17-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-17-jre-zero - Alternative JVM for OpenJDK, using Zero
openjdk-17-source - OpenJDK Development Kit (JDK) source files
Depending on your version of Ubuntu, you may be able to install OpenJDK 17 JDE and JRE using the following command.
This is only applicable if these packages are available in your distribution. The easiest and quickest way is installation OpenJDK 17 on Ubuntu 22.04
samm@java:~$ sudo apt install openjdk-17-jdk
For JRE run the following commands to install
samm@java:~$ sudo apt install openjdk-17-jre
Check Java version to validate a successful installation:
samm@java:~$ java --version
Output:
openjdk 17.0.8 2023-07-18
OpenJDK Runtime Environment (build 17.0.8+7-Ubuntu-122.04)
OpenJDK 64-Bit Server VM (build 17.0.8+7-Ubuntu-122.04, mixed mode, sharing)
Updates are handled with the standard apt update and upgrade commands. However, you can remove them separately or altogether if you no longer require JDK or JRE.
samm@java:~$ sudo apt remove openjdk-17-jre openjdk-17-jdk --purge
Note : This will remove any unrequited leftover dependencies and thoroughly wipe the installation and data as much as possible from your system.
Method 2: Install OpenJDK 17 Manually
Java OpenJDK 17 is an open-source implementation of the Java SE platform. Since the OpenJDK versions available in the default repositories are not up to date, we will have to download the Open-source JDK 17 using the wget
command.
Here’s an example of downloading OpenJDK 17 version 17.0.2 for x64 (however, it is recommended to obtain a new link from the downloads page rather than copying this example):
samm@java:~$ wget https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz
Process downloading:
Resolving download.java.net (download.java.net)... 23.207.184.97
Connecting to download.java.net (download.java.net)|23.207.184.97|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 187033144 (178M) [application/x-gzip]
Saving to: ‘openjdk-17.0.2_linux-x64_bin.tar.gz’
openjdk-17.0.2_linux-x64_bin.tar.g 100%[================================================================>] 178.37M 5.81MB/s in 30s
2023-08-08 04:31:11 (5.99 MB/s) - ‘openjdk-17.0.2_linux-x64_bin.tar.gz’ saved [187033144/187033144]
Once the download process is complete, extrac the downloaded tarball.
samm@java:~$ tar xvf openjdk-17.0.2_linux-x64_bin.tar.gz
Next, navigate to the directory.
samm@java:~$ cd jdk-17.0.2/
Run the following command to install the OpenJDK 17 files.
samm@java:~/jdk-17.0.2$ sudo mkdir -p /usr/local/openjdk-17
samm@java:~/jdk-17.0.2$ sudo mv * /usr/local/openjdk-17
The next step is to set up the environment variables by adding the following lines to your .bashrc
or .bash_profile
file.
export JAVA_HOME=/usr/local/openjdk-17
export PATH=$JAVA_HOME/bin:$PATH
Source the .bashrc
or .bash_profile
file to apply the changes.
samm@java:~/jdk-17.0.2$ source ~/.bashrc
To verify that the installation was successful, run the following command.
samm@java:~$ echo $JAVA_HOME
Output:
/usr/local/openjdk-17
After the installation, verify the active version of OpenJDK using the following command:
samm@java:~$ java --version
Output:
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)
Step 3: Set Default Java Version on Ubuntu 22.04
Setting the default Java version applies in instances where you have multiple Java versions installed on your system.
First, you need to list all the available versions.
samm@java:~$ sudo update-alternatives --config java
Output:
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
2 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
As shown in the previous output, Java 17 is listed with the number “2”. To switch to this version, type the following and press the enter key and you should see a similar printout:
update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/java to provide /usr/bin/java (java) in manual mode
Once the switch is complete, you can verify the default version using the following command.
samm@java:~$ java --version
Output:
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)
To switch to any other installed versions, repeat the same process.
Conclusion
In conclusion, Java remains a cornerstone of modern software development, powering a vast array of applications. With OpenJDK 17, you have access to the latest advancements in the Java language. This guide has walked you through the installation process on Ubuntu 22.04, providing you with a solid foundation for your Java development journey. By successfully installing Java using OpenJDK 17, you’re now equipped to create, innovate, and contribute to the world of technology.
Also Read Our Other Guides :
- How To Install Jenkins on Ubuntu Server 22.04
- How To Install and Use TeamViewer on Ubuntu 22.04
- How To Install Vagrant on Ubuntu 22.04
- How To Install VirtualBox 7.0 on Ubuntu 22.04
- How To Install Python 3.11 from Source on Ubuntu 22.04
Finally, now you have learned how to Install Java using OpenJDK 17 on Ubuntu 22.04.