Dec 22, 2025
Ariffud M.
5min Read
Java is a versatile and popular object-oriented programming language known for its platform-independent nature. Installing Java on Ubuntu offers various development possibilities, from running web servers to building Android applications.
In this article, you’ll learn how to install Java on an Ubuntu server. Whether you’re new to Java programming or seeking to refine your existing setup, our guide provides you with all the necessary tools and knowledge for your forthcoming Java projects.
Before you start installing Java on Ubuntu, ensure you meet the following prerequisites:
This section will explain how to install and configure Java for Ubuntu using a Hostinger VPS plan.

For a smooth Java installation, ensure your Ubuntu system is up-to-date. Start by securely logging into your server using an SSH client like PuTTY and entering your credentials to access the system.
Refresh your system’s connection to the APT repository by updating the package indexes:
sudo apt update sudo apt upgrade
The sudo apt update command contacts the package repository to retrieve the latest list of available packages and their versions without installing or upgrading any software.
Following this, sudo apt upgrade installs the available updates, which may include security patches and improved versions of packages, ensuring your system’s security and compatibility.
If you’re primarily focused on running Java programs, you’ll only need to install the Java Runtime Environment (JRE) on Ubuntu. Here’s the command you should execute:
sudo apt install default-jre
Facilitated by the APT package manager, this command installs the default JRE package. This default version is chosen for its stability and comprehensive support within the Ubuntu distribution.
It includes the Java Virtual Machine (JVM), core classes, and supporting libraries required to run Java applications.
For software development tasks, installing the Java Development Kit (JDK) on Ubuntu is essential. The JDK includes the JRE and key development tools like the Java compiler (javac) and debugger, which are necessary for building Java applications.
Installing the Default JDK
The OpenJDK package is recommended for most development needs due to its ease of installation and open-source licensing. To install the default version of JDK, use:
sudo apt install default-jdk
Installing Specific Versions of OpenJDK
You may need a specific Java version for compatibility purposes. For a particular OpenJDK installation on Ubuntu, first, search for the available versions with the following command:
apt search openjdk
Then, install your desired OpenJDK version, such as OpenJDK 11, using:
sudo apt install openjdk-11-jdk
Installing Oracle JDK
If your preference leans towards Oracle Java on Ubuntu due to its specific features or application compatibility, be aware that it requires manual installation, as Oracle JDK cannot be installed through package managers.
Oracle’s JDK 21 is the latest long-term support release of the Java SE Platform as of this writing. To install Oracle JDK, follow these steps:
scp /path/to/jdk-archive.tar.gz username@server_ip:/tmp
tar -xzf <filename>.tar.gz
sudo mv <extracted-folder-name> /usr/lib/jvm/oracle-jdk-<version>
After installing Java, it’s crucial to confirm that the installation was successful and that your system is using the correct version.
To check the installed version of the JRE, run the following command:
java -version
To verify the Java Development Kit version, including the compiler, enter:
javac -version
Output and Interpretation
The output should display the version number and build information. For example:
java version "21.0.2" 2024-01-15 Java(TM) SE Runtime Environment (build 21.0.2+10-LTS) Java HotSpot(TM) 64-Bit Server VM (build 21.0.2+10-LTS, mixed mode)
This confirms Java 21.0.2 is the installed Java version, with mixed mode indicating the JVM’s ability to run both just-in-time compiled and interpreted code, optimizing performance.
Configuring the Default Java Version
Ubuntu allows for multiple Java installations. If you have several installed, set the default version with the update-alternatives command:
sudo update-alternatives --config java
This command lists all installed Java versions, allowing you to perform the Ubuntu Java version switch by entering its selection number.
Updating Java
To update Java on Ubuntu, run the update and upgrade commands with apt:
sudo apt update && sudo apt upgrade
Removing/Uninstalling Java
To remove a particular version of Java, first identify its package name with the grep command:
apt list --installed | grep java
Then, use the apt remove command followed by the package name:
sudo apt remove openjdk-21-jdk
This removes the specified Java version but keeps configuration files. To completely remove Java and its configurations, use apt purge instead:
sudo apt remove openjdk-21-jdk
The JAVA_HOME environment variable is a crucial system setting that indicates the Java installation directory. This variable is used by Java-based applications and development tools to ensure their proper operation.
To configure JAVA_HOME, follow these steps:
ls /usr/lib/jvm/
Write down the Java installation location that you intend to use as JAVA_HOME.
nano ~/.bashrc
For system-wide changes, use this command instead:
sudo nano /etc/profile
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
source ~/.bashrc
Otherwise, log out and back in to apply system-wide changes.
In this guide, you’ve successfully installed Java on your Ubuntu server. Whether you chose the JRE to execute Java programs like Apache Tomcat or the JDK to develop Java apps for Android devices, this tutorial has provided you with the essential tools to get started.
Remember, understanding and managing your Java installation is crucial for a seamless development experience on Ubuntu. Best wishes on your upcoming Java-based projects!
This section will answer the most common questions about how to install Java on Ubuntu.
Ubuntu is compatible with various Java versions, including OpenJDK and Oracle JDK. The most recent Ubuntu LTS version typically supports the latest version of Java. However, you can install other versions on your system if needed.
In Ubuntu, the JDK is typically installed in the /usr/lib/jvm/ directory. This location can contain multiple JDK installations, each within its subdirectory. To view the specific installation paths and identify the JDK versions installed on your system, run ls /usr/lib/jvm/.
Yes, you can have multiple Java versions installed on Ubuntu simultaneously. To manage and switch between installed versions, use the update-alternatives system, which configures the default Java version for use in the terminal and applications requiring Java.