install java in ubuntu

Install Java in Ubuntu: Step-by-Step Guide

By Thomas Lee
AD

Installing Java on Ubuntu is a straightforward process, but understanding the nuances ensures a stable and efficient development environment. Whether you are setting up a server for enterprise applications or configuring a local machine for personal projects, the right Java setup is essential. This guide provides a detailed, step-by-step approach to getting Java operational on your Ubuntu system.

Understanding Java and JRE vs JDK

Before diving into the installation commands, it is critical to distinguish between the Java Runtime Environment (JRE) and the Java Development Kit (JDK). The JRE is sufficient for running Java applications, while the JDK is a superset that includes development tools like compilers and debuggers necessary for building Java software. Most Ubuntu users who plan to develop software will require the JDK.

Checking for Existing Installation

Ubuntu systems sometimes come with Java pre-installed, or you might have added it previously. It is always best practice to verify the current state before proceeding with a new installation. You can quickly check if Java is present and determine its version by executing a command in the terminal. This step saves time and prevents potential version conflicts.

AD
Java Inspires Simple way of installing Java JDK in Ubuntu(java 11)
Java Inspires Simple way of installing Java JDK in Ubuntu(java 11)
How to Install Java on Ubuntu
How to Install Java on Ubuntu
an image of how to install jbuntu on ubunt
an image of how to install jbuntu on ubunt

Verifying the Current State

Open your terminal and run the following command to check for an existing Java installation:

  • java -version

If Java is installed, the terminal will display the version number. If the system responds with an error like "command not found," it confirms that Java is not currently available on your machine, and you need to proceed with the installation.

How to Install Java on Ubuntu and Remove It When You’re Done
How to Install Java on Ubuntu and Remove It When You’re Done
How to Install Java/JDK on Ubuntu 18.04
How to Install Java/JDK on Ubuntu 18.04
How To Install Oracle Java 8 On Ubuntu Linux
How To Install Oracle Java 8 On Ubuntu Linux

Installing Java via Apt Package Manager

The recommended method for installing Java on Ubuntu is using the apt package manager, which handles dependencies automatically and ensures you get a stable version from the Ubuntu repositories. This method is ideal for production servers and users who prefer a streamlined, system-managed installation.

Step-by-Step Installation Process

To install the default JDK, follow these steps. First, update the local package index to ensure you are installing the latest available version:

AD
  • sudo apt update

Next, install the JDK package. This command pulls in the necessary runtime environment as well:

HOW TO INSTALL JAVA JRE JDK IN UBUNTU
HOW TO INSTALL JAVA JRE JDK IN UBUNTU
How to Find Out if Java is Installed in Ubuntu and How to Install It
How to Find Out if Java is Installed in Ubuntu and How to Install It
How to install Java JDK 10 on Ubuntu 18.04 LTS (Debian Linux)
How to install Java JDK 10 on Ubuntu 18.04 LTS (Debian Linux)
Installing Minecraft on Ubuntu
Installing Minecraft on Ubuntu
an orange background with a white coffee cup
an orange background with a white coffee cup
How To Install The Java Runtime Environment (JRE) In Ubuntu?
How To Install The Java Runtime Environment (JRE) In Ubuntu?
  • sudo apt install default-jdk

Managing Java Alternatives

If you have multiple Java versions installed on your system, the update-alternatives system allows you to configure which version is active by default. This management is crucial when different projects require specific Java versions to run correctly.

Command Description
sudo update-alternatives --config java Lists installed Java versions and allows switching between them.

Setting JAVA_HOME Environment Variable

Many Java-based applications and servers require the JAVA_HOME environment variable to locate the Java installation. Ubuntu’s alternative system stores Java in directories like /usr/lib/jvm/, making it necessary to define this variable explicitly for optimal compatibility.

AD

Configuring the Path

To set this variable permanently, you need to know the exact path of your Java installation. Use the update-alternatives command to find the path, then open the /etc/environment file with a text editor to append the following line, replacing the path with your specific version location:

How To Install Java with Apt-Get on Ubuntu 16.04 | DigitalOcean
How To Install Java with Apt-Get on Ubuntu 16.04 | DigitalOcean
an image of a computer screen with the word'jwa'in front of it
an image of a computer screen with the word'jwa'in front of it
How to install Java JDK and JRE on Ubuntu 16.04,17.04
How to install Java JDK and JRE on Ubuntu 16.04,17.04
How to install Jenkins on Ubuntu
How to install Jenkins on Ubuntu
Ubuntu Minecraft Server Java Edition
Ubuntu Minecraft Server Java Edition
Learn Manually Install Oracle Java JDK 12 on Ubuntu 18.04 / 16.04
Learn Manually Install Oracle Java JDK 12 on Ubuntu 18.04 / 16.04
How to Install Java 17 on Ubuntu 22.04 LTS + Set JAVA_HOME Environment Variable (2025 Guide)
How to Install Java 17 on Ubuntu 22.04 LTS + Set JAVA_HOME Environment Variable (2025 Guide)
How To Install Latest Java JDK 8 on Ubuntu 15.10 2016
How To Install Latest Java JDK 8 on Ubuntu 15.10 2016
Ubuntu 20.04にAptを使用してJavaをインストールする方法 | DigitalOcean
Ubuntu 20.04にAptを使用してJavaをインストールする方法 | DigitalOcean
Installing Node.js on Ubuntu
Installing Node.js on Ubuntu
Understand and Manage Java Settings in Windows 11/10
Understand and Manage Java Settings in Windows 11/10
Install Oracle Java 8 In Ubuntu Or Linux Mint Via PPA Repository [JDK8]
Install Oracle Java 8 In Ubuntu Or Linux Mint Via PPA Repository [JDK8]
How to install Java on Ubuntu
How to install Java on Ubuntu
How To Install Anaconda 64 bit in Windows 7, 8.1, 10, 11, Mac, Linux, and Ubuntu
How To Install Anaconda 64 bit in Windows 7, 8.1, 10, 11, Mac, Linux, and Ubuntu
  • export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Apply the changes by logging out and back in, or by sourcing the file.

Verifying the Deployment

Once the installation and configuration steps are complete, a final verification ensures that the environment is correctly set up. You should confirm both the Java version and the JAVA_HOME variable to guarantee that your applications will launch without issues.

AD

Final Checks

Run the following commands to validate your setup:

  • echo $JAVA_HOME – Outputs the path if set correctly.
  • java -version – Confirms the active runtime version.

With these outputs confirmed, your Ubuntu machine is ready for Java development and execution.

AD