How to Install Java on Ubuntu in 2024
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.
Prerequisites for Installing Java on Ubuntu
Before you start installing Java on Ubuntu, ensure you meet the following prerequisites:
- Familiarity with the command line. It’s crucial to have a foundational knowledge of Linux commands and Ubuntu terminal navigation. This will facilitate the Java installation process and subsequent management tasks.
- Access to an Ubuntu server. If you’re using a virtual private server (VPS), ensure you have access with sudo privileges to execute administrative commands within the server. For enhanced stability and extended support, we recommend opting for recent long-term support (LTS) versions of Linux distributions, such as Ubuntu 22.04 LTS.
- Java code understanding. While not necessary for installation, having a basic knowledge of Java coding is beneficial for those looking to develop Java-based applications. As one of the most popular programming languages, Java offers extensive documentation and community support to enhance your programming skills.
How to Install Java on Ubuntu
This section will explain how to install and configure Java for Ubuntu using a Hostinger VPS plan.
1. Update the Package Index
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.
2. Install the Default JRE
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.
3. Install a Specific Java (JDK) Version
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:
- Visit the Oracle download page to find the JDK version suitable for Ubuntu (x64 Compressed Archive). You can also obtain the official JDK Debian package here.
- After accepting the license agreement, download the .tar.gz file for your desired version.
- Use an FTP client like FileZilla or the scp command to upload Oracle’s JDK to your Ubuntu server. Choose a directory like /tmp or /opt for the upload. If opting for scp, use this command, replacing username and server_ip with your actual credentials:
scp /path/to/jdk-archive.tar.gz username@server_ip:/tmp
- Navigate to the directory where you uploaded the file and extract it using:
tar -xzf <filename>.tar.gz
- Move the extracted folder to a more permanent location, such as /usr/lib/jvm/, with:
sudo mv <extracted-folder-name> /usr/lib/jvm/oracle-jdk-<version>
4. Verify the Installation
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
5. Set Up the JAVA_HOME Environment Variable
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:
- If Java was installed using apt, it is likely located in /usr/lib/jvm/. Use the following command to list the contents of this directory and identify the exact path to your Java version:
ls /usr/lib/jvm/
Write down the Java installation location that you intend to use as JAVA_HOME.
- Edit your shell profile file to include the JAVA_HOME variable. This file is typically .bashrc for bash shell users or .profile for system-wide settings. Use the following command to edit .bashrc for a single user:
nano ~/.bashrc
For system-wide changes, use this command instead:
sudo nano /etc/profile
- At the end of the file, define JAVA_HOME, substituting /path/to/java with the actual path you noted earlier. For example, if your installation path is /usr/lib/jvm/java-21-openjdk-amd64, add:
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
- Save the file and apply the changes. If you edited .bashrc, refresh the environment variables with:
source ~/.bashrc
Otherwise, log out and back in to apply system-wide changes.
Conclusion
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!
Install Java on Ubuntu FAQ
This section will answer the most common questions about how to install Java on Ubuntu.
What Version of Java Is Compatible With 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.
Where Is JDK Installed in Ubuntu?
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/.
Can I Have Multiple Versions of Java Installed on Ubuntu?
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.