How to Install OpenJDK on Ubuntu 22.04: A Complete Step-by-Step Guide
Java remains one of the most widely used programming languages in the world, powering everything from enterprise applications and Android development to big data pipelines and microservices. At the heart of any Java development environment is the Java Development Kit (JDK) — and OpenJDK is the gold-standard, free, open-source implementation trusted by developers globally.
This guide walks you through every step of installing, configuring, and testing OpenJDK on Ubuntu 22.04 LTS, whether you're working on a local machine or a cloud-based server. If you're running your projects on a VPS Hosting plan from AlexHost, you'll benefit from NVMe-powered storage for faster Java compilation, full root access for unrestricted configuration, and enterprise-grade DDoS protection to keep your applications secure.
Why Use OpenJDK on Ubuntu 22.04?
OpenJDK (Open Java Development Kit) is the official reference implementation of Java SE (Standard Edition). Unlike proprietary alternatives, OpenJDK is:
- Completely free and open-source — no licensing fees or usage restrictions
- Actively maintained — backed by Oracle, Red Hat, Amazon, and the broader community
- Fully compatible — with virtually all Java frameworks including Spring Boot, Quarkus, Micronaut, and Apache Maven
- Available in multiple LTS versions — including OpenJDK 11 and OpenJDK 17, both of which are natively supported in Ubuntu 22.04's official repositories
Ubuntu 22.04 LTS (Jammy Jellyfish) is an ideal base OS for Java development due to its long-term support lifecycle, stability, and excellent package availability. Pair it with a high-performance VPS Hosting environment, and you have a production-ready Java platform from day one.
Prerequisites
Before you begin, make sure you have the following:
- A server or VPS running Ubuntu 22.04 LTS
- A user account with sudo privileges (or root access)
- An active internet connection
- Basic familiarity with the Linux command line
> AlexHost Tip: All AlexHost VPS plans ship with Ubuntu 22.04 as an available OS image, full root access, and SSH enabled by default — making them perfect for this type of setup.
Step 1: Update and Upgrade Your System
Before installing any new software, it is essential to refresh your package index and apply all pending upgrades. This prevents dependency conflicts and ensures you are working with the latest, most secure versions of system packages.
Open a terminal and run the following two commands:
sudo apt update
sudo apt upgrade -ysudo apt update— Fetches the latest package lists from all configured repositoriessudo apt upgrade -y— Upgrades all installed packages to their newest available versions
Wait for the process to complete before proceeding. If a kernel upgrade is included, you may be prompted to reboot — do so if necessary.
Step 2: Check Available OpenJDK Versions
Ubuntu 22.04's official repositories include several versions of OpenJDK. To see the full list of available versions before installing, run:
sudo apt search openjdkThis will return a list of all installable OpenJDK packages. The most relevant ones you'll encounter are:
| Package | Description |
|---|---|
openjdk-11-jdk | OpenJDK 11 — Long-Term Support (LTS) |
openjdk-17-jdk | OpenJDK 17 — Latest LTS as of Ubuntu 22.04 |
openjdk-18-jdk | OpenJDK 18 — Short-term release |
openjdk-11-jre | Java Runtime Environment only (no compiler) |
openjdk-17-jre | Java Runtime Environment only (no compiler) |
JDK vs JRE — What's the Difference?
- JDK (Java Development Kit): Includes the Java compiler (
javac), debugger, and all development tools. Install this if you are writing or compiling Java code. - JRE (Java Runtime Environment): Includes only what is needed to *run* pre-compiled Java applications. Install this if you are deploying an application but not developing it.
For development purposes, always install the JDK package.
Step 3: Install OpenJDK
Option A — Install OpenJDK 11 (LTS)
OpenJDK 11 is a Long-Term Support release that remains widely used in enterprise environments and is required by many legacy frameworks and tools.
sudo apt install openjdk-11-jdk -yOption B — Install OpenJDK 17 (Recommended LTS)
OpenJDK 17 is the current recommended LTS version for new projects. It includes significant performance improvements, new language features (records, sealed classes, pattern matching), and enhanced security over OpenJDK 11.
sudo apt install openjdk-17-jdk -yOption C — Install Both Versions
You can install multiple versions of OpenJDK side by side and switch between them as needed (covered in Step 5):
sudo apt install openjdk-11-jdk openjdk-17-jdk -yAPT will automatically resolve and install all required dependencies.
Step 4: Verify the Installation
Once the installation completes, confirm that Java is correctly installed by checking the version:
java -versionExpected output for OpenJDK 17:
openjdk version "17.0.x" 2023-xx-xx
OpenJDK Runtime Environment (build 17.0.x+xx-Ubuntu-xubuntu1)
OpenJDK 64-Bit Server VM (build 17.0.x+xx-Ubuntu-xubuntu1, mixed mode, sharing)Also verify the Java compiler is available:
javac -versionExpected output:
javac 17.0.xIf both commands return version information without errors, your OpenJDK installation is successful.
Step 5: Configure the Default Java Version (Multiple Installations)
If you have installed more than one version of OpenJDK, Ubuntu's update-alternatives tool allows you to define which version is used by default system-wide.
Run the following command:
sudo update-alternatives --config javaYou will see output similar to this:
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 1711 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
2 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode
Press <enter> to keep the current choice[*], or type selection number:Type the number corresponding to your preferred default version and press Enter. You can also run the same command for javac to set the default compiler:
sudo update-alternatives --config javacStep 6: Set the JAVA_HOME Environment Variable
Many Java-based applications, build tools (Maven, Gradle), and application servers (Tomcat, WildFly) rely on the JAVA_HOME environment variable to locate the JDK installation. Setting it correctly is a critical configuration step.
Find Your JDK Installation Path
First, identify the exact path to your installed JDK:
sudo update-alternatives --list javaThis will output something like:
/usr/lib/jvm/java-17-openjdk-amd64/bin/javaYour JAVA_HOME path is everything before /bin/java, so in this case: /usr/lib/jvm/java-17-openjdk-amd64
Set JAVA_HOME for the Current User
To set JAVA_HOME for your current user only, edit the .bashrc file:
nano ~/.bashrcScroll to the bottom of the file and add the following lines (adjust the version number as needed):
# Java Environment Variables
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/binSave the file (Ctrl + O, then Enter) and exit (Ctrl + X). Apply the changes to your current session:
source ~/.bashrcSet JAVA_HOME System-Wide (All Users)
To make JAVA_HOME available to all users on the system, edit /etc/environment instead:
sudo nano /etc/environmentAdd the following line:
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"Save and close the file, then reload it:
source /etc/environmentVerify JAVA_HOME is Set Correctly
echo $JAVA_HOMEExpected output:
/usr/lib/jvm/java-17-openjdk-amd64Step 7: Test Your Java Installation with a Sample Program
The best way to confirm everything is working end-to-end is to write, compile, and run a simple Java program.
Create the Source File
nano HelloWorld.javaAdd the Java Code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World! OpenJDK is working correctly.");
}
}Save the file and exit the editor.
Compile the Program
Use the Java compiler (javac) to compile the source file into bytecode:
javac HelloWorld.javaThis generates a HelloWorld.class file in the same directory. If you see no errors, compilation was successful.
Run the Program
Execute the compiled program using the Java runtime:
java HelloWorldExpected output:
Hello, World! OpenJDK is working correctly.Congratulations — your OpenJDK installation on Ubuntu 22.04 is fully functional!
Step 8: Keep OpenJDK Updated
Security patches and bug fixes are regularly released for OpenJDK. To keep your installation current, periodically run:
sudo apt update && sudo apt upgrade -yUbuntu's APT package manager will automatically include OpenJDK updates when they become available in the official repositories.
Bonus: Running Java Applications in Production on AlexHost
Once your development environment is set up, you may want to deploy your Java applications in a production environment. Here are a few recommendations:
Choose the Right Hosting Infrastructure
- VPS Hosting — Ideal for running Spring Boot microservices, REST APIs, and lightweight Java web applications. Full root access lets you configure JVM heap sizes, garbage collection settings, and system resources precisely.
- Dedicated Servers — Best for high-traffic Java applications, large-scale enterprise systems, or applications requiring maximum CPU and RAM resources without sharing.
- GPU Hosting — Perfect for Java-based machine learning workloads or data processing pipelines that benefit from GPU acceleration.
Secure Your Application
- Install an SSL/TLS certificate to encrypt traffic to your Java web application. AlexHost offers SSL Certificates for all hosting plans, enabling HTTPS with minimal configuration.
- Use a reverse proxy (NGINX or Apache) in front of your Java application server (Tomcat, Jetty, or embedded Spring Boot) for improved security and performance.
Use a Control Panel for Easier Management
If you prefer a GUI-based approach to managing your server environment, consider a VPS with cPanel or explore other VPS Control Panels available on AlexHost to simplify server administration alongside your Java deployments.
Troubleshooting Common Issues
java: command not found after installation
This usually means the binary is not in your PATH. Run:
export PATH=$PATH:/usr/lib/jvm/java-17-openjdk-amd64/binThen add this line permanently to ~/.bashrc.
JAVA_HOME not recognized by Maven or Gradle
Ensure JAVA_HOME points to the JDK root directory (not the bin subdirectory). Verify with:
ls $JAVA_HOME/bin/javacIf the file exists, the path is correct.
Wrong Java version being used
Use update-alternatives --config java (Step 5) to switch to the correct version, then verify with java -version.
Permission denied errors
Ensure you are using sudo for system-level commands and that your user has the appropriate permissions on the server.
Summary
Here is a quick reference for everything covered in this guide:
| Step | Command | Purpose |
|---|---|---|
| Update system | sudo apt update && sudo apt upgrade -y | Ensure system is current |
| Search versions | sudo apt search openjdk | Find available OpenJDK packages |
| Install JDK 17 | sudo apt install openjdk-17-jdk -y | Install OpenJDK 17 LTS |
| Verify Java | java -version | Confirm installation |
| Set default version | sudo update-alternatives --config java | Choose default JDK |
| Set JAVA_HOME | Edit ~/.bashrc or /etc/environment | Configure environment variable |
| Test installation | javac HelloWorld.java && java HelloWorld | Validate end-to-end |
Conclusion
Installing OpenJDK on Ubuntu 22.04 is a straightforward process that gives you a powerful, production-grade Java development environment at zero licensing cost. By following this guide, you have:
- Updated your system and installed OpenJDK 11 or 17 from Ubuntu's official repositories
- Verified the installation and configured the default Java version
- Set the
JAVA_HOMEenvironment variable for compatibility with build tools and application servers - Successfully compiled and executed a Java program to confirm everything works
Whether you are building a Spring Boot API, a Gradle-based microservice, or a data processing application, running it on a high-performance AlexHost VPS Hosting platform ensures fast NVMe storage for compilation speed, reliable uptime, and the security features your Java applications deserve.
on All Hosting Services