This guide will walk you through the process of installing Java Development Kit (JDK) and Java Runtime Environment (JRE) on Ubuntu Linux. Whether you are a budding software developer aiming to build your first Android application, a system administrator setting up a server environment, or a hobbyist exploring the vast world of open-source projects that leverage Java, having a correct and up-to-date Java installation is fundamental. Ubuntu, being a popular and versatile Linux distribution, offers several straightforward methods to achieve this, catering to different needs and preferences, from using the pre-packaged Ubuntu repositories to manually installing specific versions.

Understanding Java Installations on Ubuntu
Before diving into the installation process, it’s beneficial to understand the different components of Java and the options available on Ubuntu.
Java Development Kit (JDK) vs. Java Runtime Environment (JRE)
The distinction between JDK and JRE is crucial for understanding your installation needs.
Java Runtime Environment (JRE)
The JRE is the bare minimum required to run Java applications. It includes the Java Virtual Machine (JVM), which executes Java bytecode, along with a set of standard libraries. If your primary goal is to simply run applications written in Java, such as certain desktop applications or server-side services, the JRE is sufficient.
Java Development Kit (JDK)
The JDK, on the other hand, is a superset of the JRE and is essential for developing Java applications. It includes everything in the JRE, plus a suite of development tools. These tools are indispensable for writing, compiling, debugging, and packaging Java code. Key components of the JDK include:
javac: The Java compiler, which translates human-readable Java source code into Java bytecode.java: The application launcher, which executes Java bytecode on the JVM.jar: A utility for packaging Java classes and resources into a JAR (Java Archive) file.javadoc: A tool to generate API documentation from Java source code.- Debugger (
jdb): For identifying and fixing errors in Java programs. - Other development tools: Such as profilers and code analyzers.
On Ubuntu, you can install either the JRE or the JDK, or a bundle that includes both. For most development purposes, installing the JDK is recommended as it provides all necessary tools.
OpenJDK vs. Oracle JDK
When installing Java on Ubuntu, you’ll often encounter two primary sources: OpenJDK and Oracle JDK.
OpenJDK (Open Java Development Kit)
OpenJDK is the free and open-source implementation of the Java Platform, Standard Edition (Java SE). It is the reference implementation and is actively maintained by a large community of developers. Most Linux distributions, including Ubuntu, primarily package and distribute OpenJDK. It is generally the recommended choice for most users due to its open-source nature, free availability, and robust community support.
Oracle JDK
Oracle JDK is the commercial distribution of Java SE provided by Oracle. While it was historically the de facto standard, Oracle has shifted its licensing model. For some versions, Oracle JDK is free for development and testing but may require a commercial license for production deployment. While historically offering certain proprietary features or performance optimizations, the gap between OpenJDK and Oracle JDK has narrowed significantly, making OpenJDK a perfectly viable and often preferred choice.
This guide will primarily focus on installing OpenJDK, as it is the most common and recommended method on Ubuntu.
Installing Java via Ubuntu Repositories (OpenJDK)
The simplest and most common way to install Java on Ubuntu is by using the apt package manager. This method pulls Java packages directly from Ubuntu’s official software repositories, ensuring easy installation and updates.
Installing the Default OpenJDK Version
Ubuntu repositories usually contain a default, stable version of OpenJDK. This is often a good choice for general use.
-
Update your package list: Before installing any new software, it’s best practice to update your system’s package index to ensure you’re getting the latest available versions.
sudo apt update -
Install the default JRE: To install only the Java Runtime Environment, you can use the following command. This is suitable if you only need to run Java applications.
sudo apt install default-jre -
Install the default JDK: For development purposes, installing the Java Development Kit is recommended. This package includes the JRE and all the necessary development tools.
bash
sudo apt install default-jdk
After running these commands, Java will be installed on your system. You can verify the installation by checking the installed version.
Installing Specific OpenJDK Versions
Ubuntu repositories often provide access to multiple versions of OpenJDK, allowing you to install a specific version if your project requires it (e.g., for compatibility reasons).

-
Search for available Java packages: You can search for available Java versions using
apt search.apt search openjdkThis command will list various OpenJDK packages. Look for packages named
openjdk-X-jdkoropenjdk-X-jre, whereXrepresents the Java version number (e.g.,openjdk-11-jdk,openjdk-17-jdk). -
Install a specific JDK version: Once you’ve identified the version you need, you can install it using
apt install. For example, to install OpenJDK 17 JDK:
bash
sudo apt install openjdk-17-jdk
And to install OpenJDK 17 JRE:
bash
sudo apt install openjdk-17-jre
Verifying Your Java Installation
After installing Java, it’s essential to verify that it’s installed correctly and that your system can locate it.
-
Check the Java version: Run the following command to display the installed Java version.
java -versionThe output should show the version of the Java runtime that is currently active.
-
Check the Java compiler version: If you installed the JDK, you can also check the compiler version.
bash
javac -version
This command confirms that the Java compiler is available and shows its version.
Managing Multiple Java Versions (Java Alternatives)
If you have multiple Java versions installed on your system, you’ll need a way to manage which version is the default. Ubuntu uses the update-alternatives system to handle this. This is particularly useful for developers who work with projects that require different Java versions.
Setting the Default Java Version
The update-alternatives command allows you to configure which version of a command (like java or javac) is executed when you type it in the terminal.
-
List available Java alternatives: To see all installed Java versions that
update-alternativesis aware of, run:sudo update-alternatives --config javaThis command will display a list of available Java executables, each with an associated number. You will be prompted to enter the number corresponding to the Java version you wish to set as default.
-
Set the default Java compiler: Similarly, you can manage the default
javaccompiler:sudo update-alternatives --config javacThis will present a similar interactive prompt to select the default Java compiler.
-
Automatic mode:
update-alternativescan also be configured to automatically select the best available version. If you installed a new Java version and wantupdate-alternativesto recognize it, you might need to manually add it using--install. For example, if you installed OpenJDK 11 manually and want to add it to alternatives:
bash
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/java-11-openjdk-amd64/bin/java" 1100
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/java-11-openjdk-amd64/bin/javac" 1100
The last argument (1100in this case) is the priority; higher numbers mean higher priority. After adding, you can use--configas shown above to select it.
Setting Environment Variables (JAVA_HOME)
Many Java applications and build tools rely on the JAVA_HOME environment variable to locate the Java installation directory. While update-alternatives handles the executable path, setting JAVA_HOME explicitly is good practice.
-
Find your Java installation path: The paths shown in the
update-alternatives --configoutput usually point to the JDK installation directory. For example,/usr/lib/jvm/java-17-openjdk-amd64. -
Set JAVA_HOME temporarily: To set
JAVA_HOMEfor the current terminal session, you can use:export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64" echo $JAVA_HOMEReplace the path with your actual Java installation directory.
-
Set JAVA_HOME permanently: To make this setting persistent across reboots and terminal sessions, you should add it to your shell’s configuration file, typically
~/.bashrcor~/.profile.- Open the file in a text editor:
bash
nano ~/.bashrc
- Add the following lines at the end of the file, replacing the path with your specific installation:
bash
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
export PATH=$JAVA_HOME/bin:$PATH
Theexport PATH=$JAVA_HOME/bin:$PATHline ensures that the Java executables in your chosenJAVA_HOMEdirectory are prioritized in your system’s PATH. - Save the file and exit the editor.
- To apply the changes to your current session without logging out, run:
bash
source ~/.bashrc
- Open the file in a text editor:
Installing Java from Oracle (Optional)
While OpenJDK is generally recommended, you might have specific reasons to install Oracle JDK, such as requiring a particular feature or older compatibility. This process typically involves downloading the binaries directly from Oracle’s website and manually configuring them.
Downloading Oracle JDK
- Visit the Oracle Java SE Downloads page: Go to the official Oracle website and navigate to the Java SE Downloads section. You will need to find the download link for the Linux x64
.tar.gzarchive for the version you need.- Note: You may need to create an Oracle account and accept their licensing terms.

Manual Installation
-
Create a directory for Java: It’s common practice to install third-party software in
/opt.sudo mkdir -p /opt/jdk -
Extract the downloaded archive: Navigate to the directory where you downloaded the
.tar.gzfile and extract it into the directory you just created.cd /path/to/your/downloads sudo tar -xvzf jdk-X_x-linux-x64_bin.tar.gz -C /opt/jdk/Replace
jdk-X_x-linux-x64_bin.tar.gzwith the actual filename of the downloaded archive. -
Rename the extracted folder (optional but recommended): It’s often helpful to have a consistent naming convention.
cd /opt/jdk/ sudo mv jdk-X.X.X jdk-oracle-XReplace
jdk-X.X.Xwith the original extracted folder name andjdk-oracle-Xwith your desired name. -
Configure
update-alternatives: Similar to the repository installation, you’ll need to useupdate-alternativesto make this Oracle JDK the default.sudo update-alternatives --install "/usr/bin/java" "java" "/opt/jdk/jdk-oracle-X/bin/java" 1 sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/jdk/jdk-oracle-X/bin/javac" 1 sudo update-alternatives --install "/usr/bin/javadoc" "javadoc" "/opt/jdk/jdk-oracle-X/bin/javadoc" 1The priority
1is low, so you’ll likely need to use the--configoption to select it if you have OpenJDK already registered. -
Set the default version:
sudo update-alternatives --config java sudo update-alternatives --config javac sudo update-alternatives --config javadocSelect the number corresponding to your newly installed Oracle JDK.
-
Set JAVA_HOME: Update your
~/.bashrcor~/.profileas described in the previous section, ensuring theJAVA_HOMEpath points to your Oracle JDK installation directory (e.g.,/opt/jdk/jdk-oracle-X).
By following these steps, you can successfully install and manage Java on your Ubuntu Linux system, whether you opt for the convenience of repository packages or the control of manual installation.
