The Java language is used in web development, writing desktop software, creating computer and mobile games (Android), as well as in scientific research and the financial sector. Java's popularity and prevalence are due to its security, application scalability, and adaptability.
This article will describe in detail the process of installing Java on Ubuntu. You will need a server with the Ubuntu 22.04 operating system, which you can order at Hostman.
Before we proceed directly to the installation, we should talk about the existing versions of Java and its main components: JDK and JRE.
JRE is a software environment in which Java programs run. It includes JVM and Java class libraries.
JDK is a development platform for creating Java applications. It includes the JRE, compiler, debugger, and other useful developer tools.
At the time of writing, the latest version of Java is Java SE 21. It will be supported until September 2026.
Let's look at the two most popular Java builds: OpenJDK and Oracle JDK. The former is an open-source version, and the latter is proprietary, for the use of which, in commercial developments, you need to pay. There is no difference between them for the average user. When installing OpenJDK, you can only install the JRE; in the case of Oracle JDK, both the JDK and the JRE will be installed.
In this article, we will describe installing both versions. After installation, we will tell you how to configure the use of a specific version by default and how to remove unnecessary ones from those installed on the OS.
Here, we will be installing Java 19 on Ubuntu.
First, let's update the list of apt packages in our system:
sudo apt update
Now, let's check whether Java is already installed in our system:
java -version
In the output, you will see a message similar to the one below. The list of the available versions may vary.
To install Java on Ubuntu, use the command suggested by the console:
sudo apt install version_name
In our case, to download JRE from OpenJDK 19 to the server, we will enter openjdk-19-jre-headless
instead of version_name
, like this:
sudo apt install openjdk-19-jre-headless
After the installation is complete, check the Java version again:
java -version
If the system displays your Java version, you have successfully installed Java on Ubuntu.
In addition to the JRE, you can also install the JDK.
To do this, use the command mentioned above:
sudo apt install version_name
However, this time, instead of version_name
, enter openjdk-19-jdk-headless
:
sudo apt install openjdk-19-jdk-headless
Now check that the JDK was installed successfully
javac -version
The system should respond with the following line:
This completes the installation of Java 19 on Ubuntu. If you want to install a different version, for example 17, simply replace the number 19 in the commands with 17.
We will install Java 17 on Ubuntu. First, we need to go to the Oracle website and find the "Compressed Archive" for our CPU type (ARM64 or x64).
To check the CPU type of your Ubuntu server, run the command:
dpkg --print-architecture
In our case, it shows amd64
, which means we should use the x64 archive.
Copy the download link to the clipboard.
Now you can download directly to the server using the wget command:
wget your_link
So, in our case:
wget https://download.oracle.com/java/17/archive/jdk-17.0.10_linux-x64_bin.tar.gz
Next, check the checksum of the downloaded archive:
sha256sum jdk-17.0.10_linux-x64_bin.tar.gz
And compare it to the checksum on the website. To do this, click on sha256
next to the archive.
Here's the checksum we get on the website:
As you can see, the checksums match.
Now, unpack the archive into a folder on the server.
First, let's create a directory into which we will unpack the archive:
sudo mkdir -p path_to_directory
For example:
sudo mkdir -p /usr/lib/jvm
Next, unpack all the files of the downloaded archive into it.
sudo tar -zxvf jdk-17.0.10_linux-x64_bin.tar.gz -C /usr/lib/jvm
Now you can proceed to the installation.
We will perform it using PPA. To add the PPA repository we will use the command below:
sudo add-apt-repository ppa:linuxuprising/java
After this, update the list of apt
packages:
sudo apt update
And then install Oracle JDK 17 on the server:
sudo apt install oracle-java17-installer --install-recommends
When a message appears on the screen, as in the picture below, scroll down and click Ok.
Next, select Yes.
Upon completion, we will check if everything went well. To do this, enter the command we are already familiar with:
java -version
In response, you should receive a similar message:
As you can see, Oracle JDK 17 has been successfully installed to the server.
Let's also check the compiler installation:
javac -version
Installation of Java 17 on Ubuntu is complete.
In this article we install two versions of Java on a server. In practice, there may be more than two. In such cases, you can select a particular version as the default one. To do this, enter the following command into the terminal:
sudo update-alternatives --config java
As a result, the system will display all the versions on the server, as shown in the screenshot below.
To select your default version, enter its serial number in the line shown in the left column. To leave it as it is, press Enter.
This procedure can also be performed for compiler versions by replacing java
with javac
at the end of the command:
sudo update-alternatives --config javac
If, at some point, you no longer need one of the versions, you can safely delete it. Let's say you have installed the OpenJDK Java 16 on Ubuntu 22.04. To remove it, enter the following command into the terminal:
sudo apt purge openjdk-16*
If you want to remove every OpenJDK version installed on the server, use the command above without specifying the version.
In the case of Oracle Java, replace openjdk*
in the command above with oracle-java*
.
If you have installed Java 16 on Ubuntu, enter the following command into the console:
sudo apt purge oracle-java16-installer
If you need to remove another Java version, just change the version number in the command.
JAVA_HOME
is used in many programs developed in Java. It points to the directory the JDK is installed to.
To find out this directory, open the list of versions, as described in the section above:
sudo update-alternatives --config java
Now we know the location of Oracle Java 17 and OpenJDK 19. Since our example uses Oracle Java 17 by default, we copy the address specifically for it.
Now, use the copied address in the following command:
echo export JAVA_HOME="/usr/lib/jvm/java-17-oracle" >> ~/.bashrc
And then run:
source ~/.bashrc
Now let's check the changes made:
echo $JAVA_HOME
If the terminal displays the line shown below, everything was successful:
Let's check that everything is installed and working correctly. To do this, we will create a simple program displaying a greeting on the screen.
1. Create a program file:
sudo nano example.java
2. Then write the code for our program:
public class example {
public static void main(String[] args) {
System.out.println("Hello, User! Java is working successfully.");
}
}
Save the file and close the editor.
3. Now compile the program:
javac example.java
4. And launch it:
java example
The console should output the phrase that was placed in the System.out.println method. If it does, then everything works correctly.
In the article, we examined how to install Java on Ubuntu 22.04. We have shown how to install different versions of Java on the Ubuntu server and remove them.