The Easiest Way: Install Java on Ubuntu 22.04 with Apt (OpenJDK & Oracle)
Need Java for your applications on Ubuntu 22.04? This guide provides a straightforward walkthrough to install Java using apt
, covering both OpenJDK and Oracle JDK.
Why Install Java on Ubuntu?
Java remains a cornerstone for application development and deployment. From running popular applications like Tomcat and Jenkins to developing cutting-edge software, Java's versatility is unmatched. Installing Java on Ubuntu enables you to leverage this powerful platform.
Prerequisites: Before Installing Java
Before you dive in, make sure you have:
- An Ubuntu 22.04 server with a non-root user with
sudo
privileges. - A basic firewall set up to protect your system.
Ready? Let's get started.
Step 1: Choose Your Java Flavor & Install with Apt
Installing Java on Ubuntu is a breeze with the apt
package manager. You have two primary options: OpenJDK and Oracle JDK. Let's explore each.
Option 1: Install Default OpenJDK (JRE/JDK)
Ubuntu comes pre-packaged with OpenJDK, an open-source Java implementation. Here's how to install the default OpenJDK 11.
- Update your package index: Keep your system up-to-date. Run:
- Install the JRE: The Java Runtime Environment is essential for running Java applications. Install it with:
- Verify the installation: Double-check the installation with this command: You should see output confirming the installed version.
- Need the JDK? If you're developing Java applications, install the Java Development Kit (JDK) with:
- Verify the JDK installation: Confirm successful installation by checking the
javac
version: You'll see the compiler version if the JDK installed correctly.
Option 2: Install Oracle JDK 11 – A Step-by-Step Guide
For those who prefer the official Oracle JDK, here's the process:
- Create an Oracle Account: You'll need an Oracle account to download the JDK.
- Download the Correct Oracle JDK Version: Visit the Oracle Java Archive and download the Linux x64 compressed archive (
.tar.gz
) that matches your installer script version. - Transfer the File to Your Server: Use
scp
(or your preferred method) to upload the downloaded file to your server. - Add the Third-Party Repository: This repository helps install Oracle Java.
- Prepare the Installation Directory: Create the necessary directory and move the downloaded archive.
- Install the Oracle JDK: Finally, install the package. Accept the Oracle license agreement when prompted.
Installing the right Java version on your Ubuntu server can be tricky. Make sure you pick the correct version that matches your project requirement.
Step 2: Managing Multiple Java Installations
Having multiple Java versions? Use update-alternatives
to manage them.
- Choose the default Java version: Run the following command.
- Select your preferred version: Enter the number corresponding to your desired Java version.
- Repeat for other commands: Do this for
javac
,keytool
, and other Java-related commands.
Managing Java versions the easy way helps to avoid conflict in development and production environments
Step 3: Setting the JAVA_HOME
Environment Variable
Setting JAVA_HOME
ensures Java-based programs know where to find your Java installation.
- Find the Installation Path: Use
update-alternatives
to identify the path. - Edit
/etc/environment
: Open the file withsudo nano /etc/environment
(or your preferred editor). - Add
JAVA_HOME
: Append the following line, replacing the path with your Java installation path (without thebin/
part).JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
- Apply Changes: Reload the file with
source /etc/environment
. - Verify: Confirm the variable is set correctly with
echo $JAVA_HOME
.
Congrats! You've Installed Java on Ubuntu!
You've successfully installed Java and configured your environment, whether with OpenJDK or Oracle JDK. You can now run and develop Java applications on your Ubuntu 22.04 server.