How to install Java on Ubuntu
Java is a general-purpose, object-oriented programming language. Its syntax looks a lot like C and C++, but it is easier to use and helps you write more robust programs. Sun Microsystems, where Java was created, described it in the 1990s as a simple, object-oriented, network-savvy, interpreted, robust, secure, architecture-neutral, portable, high-performance, multithreaded and dynamic language.
This guide shows how to install Java on an Ubuntu server, switch between several Java versions and set the JAVA_HOME variable.
Installing the JRE/JDK from the Ubuntu Repository
The easiest way is to install the OpenJDK version that comes with Ubuntu. Start with the usual update and upgrade commands:
sudo apt-get update && sudo apt-get upgradeThen install the JRE (Java Runtime Environment), which is enough to run Java applications:
sudo apt-get install default-jreIf you plan to develop in Java, compile Java programs or run software that explicitly requires it, install the JDK (Java Development Kit):
sudo apt-get install default-jdkTo install a specific OpenJDK version, use its package name, for example:
sudo apt-get install openjdk-21-jdkInstalling Oracle JDK
Oracle JDK is the Java build distributed by Oracle. Oracle provides ready .deb packages, so you do not need any third-party repositories. At the time of the update, the current LTS release is Oracle JDK 25:
wget https://download.oracle.com/java/25/latest/jdk-25_linux-x64_bin.deb
sudo apt install ./jdk-25_linux-x64_bin.debCheck the installed version:
java -versionHow to Switch Between Java Versions on Ubuntu
Several Java versions can be installed on the same server. Use the update-alternatives command to choose the default one:
sudo update-alternatives --config javaYou will see an 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-21-openjdk-amd64/bin/java 2111 auto mode
1 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode
2 /usr/lib/jvm/jdk-25-oracle-x64/bin/java 419430400 manual mode
Press <enter> to keep the current choice[*], or type selection number:Type the number of the version you want to use by default. You can do the same for other Java tools, such as the compiler (javac), the documentation generator (javadoc) or the JAR signing tool (jarsigner). Replace the last word with the tool name:
sudo update-alternatives --config javacConfiguring JAVA_HOME on Ubuntu
To set the environment variable, first find the Java installation directory. Run update-alternatives again and copy the path of the version you need, without the /bin/java part at the end:
sudo update-alternatives --config javaOpen /etc/environment in a text editor:
sudo nano /etc/environmentAdd the following line at the end of the file, using the path you copied. JAVA_HOME must point to the JDK root directory, not to its bin directory:
JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"This sets JAVA_HOME for all users on the system. Save the file with Ctrl+O, confirm with Enter, and exit with Ctrl+X.
Reload the file to apply the changes to the current session:
source /etc/environmentCheck that the variable is set correctly:
echo $JAVA_HOMEYou should see the path you added:
/usr/lib/jvm/java-21-openjdk-amd64Other users need to run source /etc/environment or log in again to apply the setting.
Conclusion
That is it. Now you know how to install Java on an Ubuntu server. It is not difficult, but there are a few pitfalls along the way. If you need a server for your Java applications, take a look at our Ubuntu VPS plans, and contact BlueVPS support if you have any questions about the installation.
Blog