Installing Java on Your Ubuntu Machine: A Step-by-Step Breakdown
So, you've decided to dive into the world of Java development or need to run a Java-powered application on your Ubuntu system. Great choice! Ubuntu, a popular Linux distribution, is a fantastic platform for running Java, and installing it is a straightforward process. This guide will walk you through everything you need to know, from choosing the right Java version to making sure it's set up correctly. We'll be using the terminal for most of these steps, which is a powerful tool that many Ubuntu users rely on for efficient system management.
Understanding Java Development Kit (JDK) vs. Java Runtime Environment (JRE)
Before we start installing, it's helpful to understand the difference between two key components:
- Java Runtime Environment (JRE): This is the essential part for running Java applications. If you just need to execute Java programs, the JRE is sufficient.
- Java Development Kit (JDK): This includes the JRE plus development tools like the compiler (javac), debugger, and other utilities needed to write and compile Java code. If you plan to develop Java applications, you'll need the JDK.
For most users, especially those new to Java on Ubuntu, installing the JDK is the most common and recommended approach as it covers both running and developing.
Step 1: Update Your Package Lists
Before installing any new software on Ubuntu, it's always a good practice to update your system's package lists. This ensures you're getting the latest available versions of software and that your system knows where to find them.
Open your terminal by pressing Ctrl + Alt + T. Then, type the following command and press Enter:
sudo apt update
You'll be prompted to enter your user password. Type it in and press Enter. You won't see any characters appear as you type; this is normal for security.
Step 2: Install OpenJDK (The Recommended Free and Open-Source Option)
Ubuntu offers OpenJDK, a free and open-source implementation of the Java Platform, Standard Edition (Java SE). It's widely used and well-supported.
Installing the Latest Default Version
To install the default version of OpenJDK that Ubuntu provides, which is usually the latest stable release, use this command:
sudo apt install default-jdk
This command will install both the JRE and the development tools. If you only need to run Java applications and don't plan to develop them, you can install just the JRE with:
sudo apt install default-jre
However, as mentioned, installing default-jdk is generally more useful for most users.
Installing a Specific Version of OpenJDK
If you need a particular version of Java for compatibility with older applications or specific development requirements, you can search for available OpenJDK packages and install them individually. To see a list of available OpenJDK versions, you can use:
sudo apt search openjdk-
This will show you a long list of packages. You'll typically look for packages named like openjdk-11-jdk, openjdk-17-jdk, or openjdk-21-jdk, representing different Java versions.
For example, to install OpenJDK 17, you would use:
sudo apt install openjdk-17-jdk
And to install OpenJDK 21:
sudo apt install openjdk-21-jdk
Remember to replace the version number (e.g., 17 or 21) with the one you need.
Step 3: Verify the Installation
Once the installation is complete, it's crucial to verify that Java has been installed correctly and to check which version is now active on your system.
In your terminal, type the following command:
java -version
If the installation was successful, you should see output similar to this (the exact version numbers may vary):
openjdk version "17.0.9" 2026-10-17
OpenJDK Runtime Environment (build 17.0.9+9-Ubuntu-122.04)
OpenJDK 64-Bit Server VM (build 17.0.9+9-Ubuntu-122.04, mixed mode, sharing)
You can also verify the Java compiler version (if you installed the JDK):
javac -version
This should also display the installed Java version.
Step 4: Managing Multiple Java Versions (If Necessary)
If you have installed multiple versions of Java, you might need to tell your system which one to use by default. Ubuntu provides a utility called update-alternatives to manage this.
To list all installed Java versions and select the one you want to be the default, run:
sudo update-alternatives --config java
The terminal will present you with a list of available Java executables, each with a corresponding number. You'll be prompted to enter the number associated with the Java version you want to set as the default. For example, if you want to use OpenJDK 17, you would enter its corresponding number and press Enter.
You can do the same for the Java compiler:
sudo update-alternatives --config javac
Step 5: Setting the JAVA_HOME Environment Variable (Important for Development)
Many Java-based applications and development tools rely on the JAVA_HOME environment variable. This variable points to the root directory of your Java installation. Setting it correctly is vital for many build tools and IDEs.
First, you need to find the installation path of your desired Java version. You can often find this information using the update-alternatives command itself. For example, if you chose OpenJDK 17 as your default, you can find its path by running:
sudo update-alternatives --query java
Look for the "current link" or similar entries to find the path. It will typically look something like /usr/lib/jvm/java-17-openjdk-amd64.
Now, you need to add this to your shell's profile. For most users, editing the ~/.bashrc file is the way to go. Open it with a text editor:
nano ~/.bashrc
Scroll to the very bottom of the file and add the following lines, replacing /usr/lib/jvm/java-17-openjdk-amd64 with the actual path you found for your Java installation:
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
export PATH=$PATH:$JAVA_HOME/bin
Press Ctrl + X to exit nano, then Y to confirm saving, and Enter to accept the filename.
To apply these changes without logging out and back in, you can "source" your .bashrc file:
source ~/.bashrc
To verify that JAVA_HOME is set correctly, you can echo it:
echo $JAVA_HOME
This should output the path you just set.
Installing Oracle JDK (For Specific Licensing or Features)
While OpenJDK is excellent, some users might need to install Oracle's official JDK. This often comes down to licensing requirements for commercial use or if a specific application explicitly requires Oracle JDK. Installation of Oracle JDK is a bit more manual as it's not typically available directly through Ubuntu's default repositories.
Here's a general overview of the process:
- Download the Oracle JDK: Go to the official Oracle Java downloads page and download the Linux x64 Compressed Archive (
.tar.gz) for your desired version. - Extract the Archive: Create a directory for Java installations, for example,
/usr/lib/jvm/, and extract the downloaded archive there. You'll likely needsudofor this. - Configure
update-alternatives: Similar to OpenJDK, you'll usesudo update-alternatives --installto register the Oracle JDK with the system's alternatives system. This is a more complex command that requires specifying the executable path and a priority. - Set
JAVA_HOME: Manually set theJAVA_HOMEenvironment variable in your~/.bashrcfile to point to your Oracle JDK installation directory.
Due to the manual nature and potential licensing complexities, we recommend sticking with OpenJDK unless you have a specific, unavoidable reason to use Oracle JDK.
Troubleshooting Common Issues
"Command 'java' not found": This usually means Java is not installed, or its directory is not in your system's PATH. Double-check your installation steps and ensure JAVA_HOME is correctly set and sourced.
Permission Denied: Make sure you are using sudo for commands that require administrator privileges, such as installing packages or modifying system files.
Incorrect Version Running: If you have multiple versions installed, use sudo update-alternatives --config java to set the correct one as the default.
By following these steps, you should have a fully functional Java environment set up on your Ubuntu machine. Happy coding or running your Java applications!
Frequently Asked Questions (FAQ)
How do I check which version of Java is currently active?
You can easily check the active Java version by opening your terminal and typing java -version. This command will display the version number of the Java Runtime Environment that your system is currently using.
Why should I install the JDK instead of just the JRE?
Installing the JDK (Java Development Kit) is generally recommended for most users because it includes the JRE (Java Runtime Environment) along with development tools like the compiler. This means you can both run Java applications and develop your own Java code without needing to install additional software later.
What is the `JAVA_HOME` environment variable for?
The `JAVA_HOME` environment variable is crucial for many Java-based applications and development tools. It specifies the directory where your Java Development Kit is installed. Many build tools, IDEs, and application servers use this variable to locate Java executables and libraries, ensuring they can function correctly.
Can I install multiple versions of Java on Ubuntu?
Yes, you can absolutely install multiple versions of Java on Ubuntu. This is often necessary for compatibility with different projects or applications that require specific Java versions. Ubuntu's `update-alternatives` system allows you to manage these multiple installations and choose which one your system uses as the default.

