How to Install Java on Ubuntu (2026 Guide) – Complete DevOps Engineer Tutorial
Introduction
Java remains one of the most widely used programming languages in the world. From enterprise applications and banking systems to modern microservices, big data platforms, and cloud-native applications, Java powers thousands of production environments every day.
If you are a developer, system administrator, DevOps engineer, or cloud engineer, knowing how to properly install and manage Java on Linux is an essential skill.
Many beginners simply run a package installation command and move on. However, production environments often require specific Java versions because applications may not be compatible with newer releases.
In this complete guide, you'll learn how to install Java on Ubuntu, verify the installation, configure environment variables, manage multiple Java versions, and follow real DevOps best practices used in enterprise environments.
- Introduction
- Real DevOps Story: Application Failed After Java Upgrade
- What is Java and Why Is It Important?
- Step 1: Update Ubuntu Packages
- Step 2: Check Existing Java Installation
- Step 3: Install Java on Ubuntu
- Step 4: Verify Java Installation
- Step 5: Configure JAVA_HOME
- Step 6: Manage Multiple Java Versions
- Important Java Commands
- DevOps Best Practices
- Common Errors and Fixes
- Recommended Resources
- Frequently Asked Questions
- Conclusion
Real DevOps Story: Application Failed After Java Upgrade
During a production migration project, one team upgraded Java from version 11 to version 21 because they wanted the latest performance improvements.
The upgrade seemed successful at first.
However, after deployment, the application started throwing compatibility errors. A third-party library used by the application was certified only for Java 11 and failed to load correctly.
The production environment became unstable, resulting in downtime and emergency rollback procedures.
This is why experienced DevOps engineers maintain strict version control and test upgrades in staging environments first.
What is Java and Why Is It Important?
Java is a high-level, object-oriented programming language designed to run on multiple platforms using the Java Virtual Machine (JVM).
Today, Java is used in:
- Enterprise applications
- Spring Boot microservices
- Banking systems
- Big data platforms
- Android development
- Cloud-native applications
- DevOps automation tools
Many popular platforms such as Jenkins, Hadoop, Kafka, Elasticsearch, and numerous enterprise systems require Java.
Step 1: Update Ubuntu Packages
Before installing Java, update your package repositories.
sudo apt update && sudo apt upgrade -y
This ensures Ubuntu installs the latest available packages and security updates.
Step 2: Check Existing Java Installation
Before installing Java, verify whether Java is already available on your system.
java -version
If Java is not installed, you may see an error similar to:
Command 'java' not found
This confirms that Java needs to be installed.
Step 3: Install Java on Ubuntu
The easiest and most reliable method is installing OpenJDK from Ubuntu repositories.
Install Latest OpenJDK
sudo apt install default-jdk -y
This installs the default Java Development Kit maintained by Ubuntu.
Install Java 21
sudo apt install openjdk-21-jdk -y
Java 21 is currently a popular Long-Term Support (LTS) release for enterprise environments.
Install Java 17
sudo apt install openjdk-17-jdk -y
Many organizations still standardize on Java 17 because of long-term support and application compatibility.
Install Java Runtime Only
If you only need to run Java applications and not compile code:
sudo apt install default-jre -y
Step 4: Verify Java Installation
After installation, verify Java is working correctly.
java -version
Example output:
openjdk version "21.0.7"
OpenJDK Runtime Environment
OpenJDK 64-Bit Server VM
Verify the compiler as well:
javac -version
Example:
javac 21.0.7
Step 5: Configure JAVA_HOME Environment Variable
Many enterprise applications require the JAVA_HOME environment variable.
Find the Java installation path:
sudo update-alternatives --config java
Example output:
/usr/lib/jvm/java-21-openjdk-amd64/bin/java
Open the profile file:
nano ~/.bashrc
Add the following lines:
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin
Reload the configuration:
source ~/.bashrc
Verify:
echo $JAVA_HOME
Step 6: Manage Multiple Java Versions
Many DevOps environments require multiple Java versions.
Check installed Java versions:
update-java-alternatives --list
Switch Java versions:
sudo update-alternatives --config java
You will see a numbered list of installed Java versions.
Select the desired version and press Enter.
Verify the active version:
java -version
This capability is extremely useful when maintaining legacy and modern applications on the same server.
Important Java Commands
| Command | Purpose | Description |
|---|---|---|
java -version |
Check version | Displays installed Java version |
javac -version |
Compiler version | Shows Java compiler version |
which java |
Locate Java | Shows Java binary location |
echo $JAVA_HOME |
Check variable | Displays JAVA_HOME path |
update-alternatives --config java |
Switch version | Select active Java version |
jar -tf app.jar |
View archive | Lists contents of JAR file |
DevOps Best Practices
- Use LTS Java releases for production systems
- Always test Java upgrades in staging environments
- Document Java versions in deployment documentation
- Configure JAVA_HOME consistently across servers
- Use Infrastructure as Code for Java installation
- Monitor application logs after Java upgrades
- Keep security patches up to date
- Avoid upgrading production servers directly
In most enterprise environments, Java version management is part of the CI/CD deployment process to ensure consistency across development, staging, and production environments.
Common Errors and Fixes
| Error | Cause | Solution |
|---|---|---|
| java: command not found | Java not installed | Install OpenJDK package |
| JAVA_HOME not set | Missing environment variable | Configure JAVA_HOME |
| Wrong Java version | Multiple versions installed | Use update-alternatives |
| Application startup failed | Unsupported Java version | Verify application compatibility |
| Permission denied | Insufficient privileges | Use sudo where required |
Frequently Asked Questions
1. Which Java version should I use for production?
Java LTS releases such as Java 17 or Java 21 are recommended because they receive long-term security and maintenance updates.
2. What is the difference between JDK and JRE?
JDK includes development tools such as the Java compiler, while JRE is only required to run Java applications.
3. Can I install multiple Java versions?
Yes. Ubuntu allows multiple Java versions to coexist, and you can switch between them using update-alternatives.
4. Is OpenJDK free?
Yes. OpenJDK is open-source and widely used in enterprise environments.
5. Why is JAVA_HOME important?
Many Java applications, build tools, and CI/CD pipelines depend on JAVA_HOME to locate the Java installation.
6. How do I uninstall Java?
sudo apt remove openjdk-* -y
This removes installed OpenJDK packages from the system.
Conclusion
You now know how to install Java on Ubuntu, configure JAVA_HOME, manage multiple Java versions, and apply production-ready DevOps practices.
Whether you're deploying Spring Boot applications, managing Jenkins servers, building CI/CD pipelines, or running enterprise workloads, proper Java management is a critical skill.
By following the steps in this guide, you can maintain stable, secure, and predictable Java environments across development, staging, and production systems.