Ubuntu, a popular Linux distribution, is a powerhouse for developers and enthusiasts alike, offering a robust and flexible environment for various programming tasks. Among the multitude of programming languages and tools, Java stands out as one cornerstone, enabling the creation of everything from enterprise-level applications and Android mobile apps to complex web services and desktop utilities. For anyone looking to develop, deploy, or run Java-based applications on their Ubuntu system, installing Java is a fundamental first step. This comprehensive guide will walk you through the process, covering the different installation methods, how to verify your setup, and essential configuration steps to ensure your Java environment is optimized for your needs.

Understanding Java and Its Importance for Ubuntu Users
Before diving into the technical steps, it’s beneficial to understand why Java remains so relevant and why having it properly installed on your Ubuntu system is crucial.
Why Java on Ubuntu?
Java’s enduring popularity stems from several key characteristics: its platform independence (write once, run anywhere), robust security features, object-oriented nature, and a vast ecosystem of libraries and frameworks. For Ubuntu users, this translates into a powerful combination:
- Development Powerhouse: Ubuntu is a preferred OS for many developers due to its stability, strong community support, and access to a wide array of development tools. Installing Java on Ubuntu transforms it into an ideal machine for Java development using IDEs like IntelliJ IDEA, Eclipse, or NetBeans.
- Android Development: Android applications are primarily built using Java (or Kotlin, which compiles to Java bytecode). If you plan on developing Android apps, Java is an absolute necessity.
- Enterprise Applications: Many large-scale enterprise systems, web servers (like Apache Tomcat, WildFly, Jetty), and big data tools (like Apache Hadoop, Apache Spark) are built with Java. Running or contributing to these projects requires a functional Java installation.
- Cross-Platform Compatibility: Java applications are designed to run on any platform with a compatible Java Virtual Machine (JVM). This means applications developed on Windows or macOS can run seamlessly on Ubuntu, provided Java is installed.
- Active Community and Resources: Both Java and Ubuntu boast massive, active communities. This means a wealth of documentation, tutorials, and forums are available to help you troubleshoot issues and learn new techniques.
In essence, installing Java on Ubuntu is not just about installing a piece of software; it’s about unlocking a world of development possibilities and ensuring compatibility with a vast range of applications.
JRE vs. JDK: What’s the Difference?
When you hear about Java, you’ll often encounter two acronyms: JRE and JDK. Understanding the distinction is vital for choosing the right installation.
- JRE (Java Runtime Environment): This is what you need to run Java applications. It consists of the Java Virtual Machine (JVM) and the core libraries and other components required to execute compiled Java code. If you’re solely an end-user who needs to run Java-based software, the JRE is sufficient. It does not include development tools like compilers.
- JDK (Java Development Kit): This is what you need to develop Java applications. The JDK includes everything in the JRE, plus development tools such as the Java compiler (
javac), debugger (jdb), archiver (jar), and other utilities. If you plan to write, compile, and debug Java code, you will need the JDK.
Most developers will opt for the JDK as it encompasses the JRE’s functionality while adding the essential tools for creation. When installing via package managers, you’ll often find separate packages for JRE and JDK, or a single JDK package that implicitly includes the JRE.
Preparing Your Ubuntu System for Java Installation
Before you begin the Java installation process, it’s always good practice to ensure your Ubuntu system is up-to-date. This helps prevent conflicts, resolves potential dependency issues, and ensures you have access to the latest security patches and package versions.
Updating Your System’s Package List
The first step is to update your local package list and upgrade any installed packages to their latest versions. Open your terminal (you can usually find it by pressing Ctrl + Alt + T or searching for “Terminal” in your applications menu) and execute the following commands:
sudo apt update
sudo apt upgrade -y
sudo apt update: This command fetches the latest package information from the configured repositories. It refreshes the list of available software and their versions but does not install or upgrade any packages.sudo apt upgrade -y: This command then upgrades all your currently installed packages to their newer versions based on the information fetched byapt update. The-yflag automatically confirms any prompts, allowing the upgrade process to run non-interactively.
This process might take a few minutes, depending on the number of packages needing updates and your internet connection speed. Once complete, your system is ready for the Java installation.
Ensuring Prerequisites Are Met
Generally, the only real prerequisite is sudo access, which most Ubuntu users have by default. sudo allows you to execute commands with superuser privileges, necessary for installing software system-wide. If you’re using a fresh Ubuntu installation or have a non-standard setup, ensure your user account has sudo privileges.
Installing OpenJDK: The Recommended Approach
OpenJDK is the free and open-source implementation of the Java Platform, Standard Edition (Java SE). It is the reference implementation of Java and is fully compatible with Oracle JDK. For most users and developers on Ubuntu, OpenJDK is the recommended choice due to its open-source nature, active community development, and ease of installation through Ubuntu’s default repositories.
Installing the Default JRE and JDK
Ubuntu’s repositories often provide a “default” version of Java, which is usually the latest stable OpenJDK release available at the time of the Ubuntu version’s release. This is the simplest way to get Java up and running.
To install the default JRE (for running Java applications):
sudo apt install default-jre
To install the default JDK (for developing and running Java applications):
sudo apt install default-jdk
When you install default-jdk, it will typically include default-jre as a dependency, so you generally only need to install default-jdk if you plan to develop.
Installing Specific OpenJDK Versions (e.g., Java 11, 17, 21)
While the default-jdk package is convenient, you might need a specific Java version (e.g., Java 11 for an older project, Java 17 for a Long-Term Support release, or Java 21 for the very latest features). Ubuntu repositories usually offer several OpenJDK versions.
To list available OpenJDK packages, you can use:
apt search openjdk | grep -E 'jdk|jre'
This command will show you a list of available OpenJDK packages, often including versions like openjdk-11-jdk, openjdk-17-jdk, openjdk-21-jdk, and their respective JRE counterparts.
To install a specific version, such as OpenJDK 17 (a current LTS release):
sudo apt install openjdk-17-jdk
If you only need the JRE for a specific version:
sudo apt install openjdk-17-jre
Repeat this command for any other specific versions you need (e.g., openjdk-11-jdk, openjdk-21-jdk). Installing multiple versions is common for developers working on different projects with varying Java requirements. Ubuntu’s update-alternatives system (discussed later) makes it easy to switch between them.
The system will prompt you to confirm the installation and download necessary packages. Type Y and press Enter to proceed.
Verifying Your Java Installation
After installing Java, it’s crucial to verify that it has been installed correctly and that your system recognizes the chosen version.
Checking Java Runtime Environment (JRE)
To check the version of the Java Runtime Environment installed and used by default, open your terminal and type:
java -version
You should see output similar to this (the version number will vary depending on what you installed):

openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment (build 17.0.9+9-Ubuntu-123.04.1)
OpenJDK 64-Bit Server VM (build 17.0.9+9-Ubuntu-123.04.1, mixed mode, sharing)
This output confirms that Java is installed and indicates the version number, runtime environment, and VM details.
Confirming Java Development Kit (JDK)
If you installed the JDK, you also have the Java compiler (javac). To verify the JDK installation, check its version:
javac -version
You should see output similar to:
javac 17.0.9
If both java -version and javac -version commands return version information without errors, your Java installation is successful. If you only installed the JRE, javac will not be found, which is expected.
Managing Multiple Java Versions and Setting JAVA_HOME
For developers, it’s common to have multiple Java versions installed simultaneously to support different projects. Ubuntu’s update-alternatives system and setting the JAVA_HOME environment variable are key to managing these setups.
Switching Between Installed Java Versions
The update-alternatives command in Ubuntu manages which executables are used when multiple programs provide the same functionality. This is particularly useful for Java, allowing you to easily switch between different installed JDKs or JREs.
To configure which java executable is used system-wide:
sudo update-alternatives --config java
You will see a list of installed Java versions, each with a selection number. For example:
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1700 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1100 manual mode
2 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1700 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Enter the Selection number corresponding to the Java version you want to set as the default, then press Enter. The asterisk (*) indicates the currently selected version.
You should do the same for javac (the Java compiler) and potentially jar (the Java archiver) if you have multiple JDKs:
sudo update-alternatives --config javac
sudo update-alternatives --config jar
Always verify your changes after switching by running java -version and javac -version.
Setting the JAVA_HOME Environment Variable
While update-alternatives sets the default java and javac executables, some applications and build tools (like Maven, Gradle, or certain IDEs) rely on the JAVA_HOME environment variable to locate the Java installation directory. This variable points to the root directory of your chosen JDK installation.
First, determine the path to your desired JDK. You can find this by listing the contents of /usr/lib/jvm/:
ls -l /usr/lib/jvm/
You’ll see directories like java-11-openjdk-amd64, java-17-openjdk-amd64, etc. Choose the one you want to set as JAVA_HOME. For instance, if you want to use OpenJDK 17, the path might be /usr/lib/jvm/java-17-openjdk-amd64.
To set JAVA_HOME for your current user, you should edit your shell’s configuration file, typically ~/.bashrc (for Bash shell) or ~/.zshrc (for Zsh shell).
Open the file with a text editor (e.g., nano or gedit):
nano ~/.bashrc
Scroll to the end of the file and add the following lines, replacing /usr/lib/jvm/java-17-openjdk-amd64 with your actual JDK path:
# Set JAVA_HOME environment variable
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
export PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME="...": This line defines theJAVA_HOMEvariable.export PATH=$PATH:$JAVA_HOME/bin: This line adds thebindirectory of yourJAVA_HOMEto your system’sPATH. This ensures thatjava,javac, and other commands located in$JAVA_HOME/binare found by your system, even ifupdate-alternativespoints to a different version. This is important for consistency and for applications that specifically look for$JAVA_HOME/binexecutables.
Save the file (if using nano, press Ctrl + O, then Enter, then Ctrl + X).
For the changes to take effect in your current terminal session, you need to source the file:
source ~/.bashrc
You can verify that JAVA_HOME is set correctly by running:
echo $JAVA_HOME
This should output the path you just set.
For a system-wide JAVA_HOME setting (affecting all users), you would edit /etc/environment instead. This file is specifically designed for system-wide environment variables.
sudo nano /etc/environment
Add or modify the JAVA_HOME line:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
After saving, the changes will typically take effect after a reboot or by logging out and back in. Note that modifications to /etc/environment do not require export.
Choosing between ~/.bashrc and /etc/environment depends on whether you want JAVA_HOME set for a single user or globally for all users and processes. For most personal development setups, ~/.bashrc is sufficient and more flexible.

Conclusion: Empowering Your Development Environment
You’ve successfully navigated the process of installing Java on your Ubuntu system, covering everything from understanding its importance to managing specific versions and configuring environment variables. By following these steps, you’ve equipped your Ubuntu machine with the necessary tools to run and develop a vast array of Java applications, from simple command-line utilities to complex enterprise systems and cutting-edge Android apps.
With Java now robustly integrated into your development environment, you are ready to explore the extensive Java ecosystem. Whether your journey takes you into web development with Spring Boot, mobile app creation with Android Studio, or big data processing with Apache Spark, your well-configured Java setup on Ubuntu will serve as a stable and powerful foundation for your projects. Remember to keep your system updated, and happy coding!
aViewFromTheCave is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. Amazon, the Amazon logo, AmazonSupply, and the AmazonSupply logo are trademarks of Amazon.com, Inc. or its affiliates. As an Amazon Associate we earn affiliate commissions from qualifying purchases.