Install Java on Ubuntu 22.04: A Step-by-Step Guide (OpenJDK & Oracle)
Need Java for your applications on Ubuntu 22.04? This guide walks you through installing Java using apt
, covering both OpenJDK and Oracle JDK. Learn how to set up your environment and manage different Java versions effortlessly.
Why Install Java on Ubuntu?
Java is essential for running a wide range of applications, from web servers like Tomcat and Jetty to powerful tools, including Jenkins and Cassandra. Whether you're a developer or simply need to run Java-based software, this tutorial provides the necessary steps to get you up and running with Java on Ubuntu 22.04.
Prerequisites
Before we begin, make sure you have:
- An Ubuntu 22.04 server with a non-root user with sudo privileges and a firewall.
Installing Java: Your Options
You have two main options for installing Java:
- OpenJDK: The open-source implementation of Java, pre-packaged with Ubuntu.
- Oracle JDK: The official version from Oracle.
Both are officially recognized and largely identical since Java 11. Your choice depends on your desired licensing and whether you need to install the JRE separately (OpenJDK offers this).
Option 1: Quick Install of Default JRE/JDK (OpenJDK)
Ubuntu 22.04 defaults to OpenJDK 11. Here's how to install it:
-
Update your package index:
sudo apt update
-
Install the JRE:
sudo apt install default-jre
This lets you run almost all Java software.
-
Verify the installation:
java -version
-
Install the JDK (if needed for development):
sudo apt install default-jdk
This includes the JRE.
-
Verify the JDK installation:
javac -version
Option 2: Installing Oracle JDK 11 on Ubuntu
Installing Oracle JDK requires a few more steps due to licensing:
-
Create an Oracle Account: Go to the Oracle website and create an account.
-
Download Oracle JDK 11:
- Visit the Archive Downloads page.
- Find the version matching the installer script version (check the
oracle-java11-installer
page for the correct version). - Download the Linux x64
.tar.gz
package. Accept the license agreement before downloading.
-
Transfer the File to Your Server: Use
scp
(or your preferred method) to upload the downloaded file to your server's home directory.scp Downloads/jdk-11.0.13_linux-x64_bin.tar.gz sammy@your_server_ip:~
-
Add the Third-Party Repository:
-
Import the signing key:
sudo gpg --homedir /tmp --no-default-keyring --keyring /usr/share/keyrings/oracle-jdk11-installer.gpg --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A
-
Add the repository to your package sources:
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/oracle-jdk11-installer.gpg] https://ppa.launchpadcontent.net/linuxuprising/java/ubuntu jammy main" | sudo tee /etc/apt/sources.list.d/oracle-jdk11-installer.list > /dev/null
-
Update your package list:
sudo apt update
-
-
Prepare for Installation:
-
Create the required directory:
sudo mkdir -p /var/cache/oracle-jdk11-installer-local/
-
Move the downloaded archive:
sudo cp jdk-11.0.13_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/
-
-
Install Oracle JDK:
sudo apt install oracle-java11-installer-local
Accept the license agreement when prompted.
Managing Java Versions on Ubuntu
You can have multiple Java installations. Use update-alternatives
to switch between them:
sudo update-alternatives --config java
Choose the number corresponding to the desired Java version.
You can do this for other Java commands like javac
:
sudo update-alternatives --config javac
Setting the JAVA_HOME
Environment Variable
-
Find the Java Installation Path: Use
update-alternatives
to list your Java installations and their paths.sudo update-alternatives --config java
-
Edit
/etc/environment
: Open the file withnano
or your preferred editor:sudo nano /etc/environment
-
Add the
JAVA_HOME
Variable: Add the following line to the end, replacing the path with your Java installation path (without thebin/
part):JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
This sets the variable for all users.
-
Reload the File:
source /etc/environment
-
Verify the Variable:
echo $JAVA_HOME
Ready to Develop with Java?
You've successfully installed Java (JRE and JDK) on your Ubuntu 22.04 system! Now you can run Java applications, develop new ones, and explore the world of Java development. This includes running software like Tomcat, Jetty, or even tools like Jenkins.