Install Java on Ubuntu 22.04: The Complete, Step-by-Step Guide
Want to run Java applications or develop Java software on your Ubuntu 22.04 server? This guide provides a comprehensive walkthrough on how to install Java on Ubuntu 22.04 using apt
, even if you're a complete beginner. Learn how to install both the JRE (Java Runtime Environment) and JDK (Java Development Kit), including OpenJDK and Oracle JDK.
Why Install Java on Ubuntu?
Java is a versatile platform powering a vast ecosystem of applications. Installing Java lets you:
- Run popular software like Tomcat, Jenkins, and Cassandra.
- Develop your own Java applications.
- Utilize Java-based tools and libraries.
Prerequisites
- An Ubuntu 22.04 server, set up with a non-root user with sudo privileges and a firewall.
Step 1: Installing Java – Choose Your Flavor
You have two main choices when you install Java on Ubuntu: OpenJDK and Oracle JDK. Here's a quick breakdown:
- OpenJDK: An open-source implementation of Java.
- Oracle JDK: The official version from Oracle.
Both are standards-compliant, but licensing and support considerations might influence your decision.
Option 1: Quick & Easy – Install the Default JRE/JDK (OpenJDK)
Ubuntu 22.04 comes with OpenJDK 11, a fully open-source Java option. Here's how to get started:
-
Update your package list:
-
Install the JRE (Java Runtime Environment): This lets you run Java applications.
-
Verify the installation:
You should see output confirming the OpenJDK version.
-
(Optional) Install the JDK (Java Development Kit): If you also need to develop Java applications, install the JDK. This will also install JRE.
-
Verify the JDK installation:
You should see output confirming the
javac
compiler version.
Option 2: Installing Oracle JDK 11
-
Create an Oracle Account: Since the Oracle agreement doesn’t allow automatic installation through the
apt
package, you will need an Oracle account to do this. Use this link to create one if you don't already have one. -
Find the Correct Installer Version: Visit the
oracle-java11-installer
page to find out what the current software version is. -
Download The Correct Version: Visit the Archive Downloads and locate the version that matches the software version you need.
-
Transfer the Downloaded File to Your Server: Use the
scp
command on your local machine to transfer the package to the home directory of yoursammy
user.scp Downloads/jdk-11.0.13_linux-x64_bin.tar.gz sammy@your_server_ip:~
-
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 List of 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
-
Create the Following Directory:
sudo mkdir -p /var/cache/oracle-jdk11-installer-local/
-
Move the Downloaded Package:
sudo cp jdk-11.0.13_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/
-
Finally, install the package:
Step 2: Managing Java Versions with update-alternatives
If you have multiple Java installations, use update-alternatives
to manage them:
-
Check installed versions:
-
Select your default Java: Choose the number corresponding to your preferred Java version.
-
Repeat for other commands (optional): You can also configure
javac
,keytool
, etc.
Step 3: Setting the JAVA_HOME
Environment Variable
Many Java programs rely on the JAVA_HOME
environment variable. Here's how to set it:
-
Determine your Java installation path: Use
sudo update-alternatives --config java
(as above) to find the path to your preferred Java installation. -
Edit
/etc/environment
: -
Add the
JAVA_HOME
variable: At the end of the file, add a line similar to this, replacing the example path with your actual path (without thebin/
part):JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
-
Reload the environment:
-
Verify:
Conclusion
Congratulations! You've successfully installed Java on Ubuntu 22.04. You can now run Java applications and develop powerful Java software on your server. Remember to choose the right Java distribution (OpenJDK or Oracle JDK) based on your needs and licensing requirements.