[*]
This post shows students and new users how to install OpenJDK and Oracle Java JDK on Ubuntu Linux. For anyone building Java based applications, they’re going to be using the Java programming language. Java language is popular and in almost all operating systems.
Java can be implemented in multiple ways. When it comes to Linux, there are two primary Java implementations: OpenJDK and Oracle Java JDK. There two are identical, except for Oracle Java, which has some few commercial features and comes with some restrictions on how to use the software.
Oracle Java license limits Java for personal and development usage only. You cannot use it for commercial purposes. OpenJDK is a free, opensource Java software which allows anyone from anywhere to freely use as they see fit.
OpenJDK consists of Java Runtime Environment (JRE) and Java Development Kit (JDK).
JRE consists of the Java virtual machine (JVM), classes, and binaries that allow you to run Java programs. JDK includes JRE and development/debugging tools and libraries necessary to build Java applications.
When you’re ready to install both Java software, use either methods below: If you prefer to go with the open source version, then install OpenJDK. If you instead want to install Oracle Java, then use the second method below.
How to install OpenJDK on Ubuntu
OpenJDK is the open source version of Java. It’s highly compatible with Ubuntu and if you don’t know which version of Java to install, the open source version should use selected. At the time of this writing, OpenJDK 17 is the latest stable version that can be installed on Ubuntu.
OpenJDK (java.net)
To install OpenJDK 17 on Ubuntu, run the commands below. If there’s a newer version available from the link above, then install it instead.
sudo apt update sudo apt install openjdk-17-jdk
Once OpenJDK is installed, you can verify and validate that Java is installed, by running the command below:
java -version
That should display similar lines as show below:
openjdk version "17" 2021-09-14 OpenJDK Runtime Environment (build 17+35-Ubuntu-120.04) OpenJDK 64-Bit Server VM (build 17+35-Ubuntu-120.04, mixed mode, sharing)
OpenJDK is installed and validated.
If you also want to install Java JRE, part of the OpenJava suite, then simply run the commands below:
sudo apt update sudo apt install openjdk-17-jre
If you only want to run the bare minimal of Java Runtime, then run the commands below:
sudo apt update sudo apt install openjdk-17-jdk-headless
Some applications still don’t fully support the latest OpenJDK 17. For those, they can install the previous Java LTS which was version 8.
sudo apt update sudo apt install openjdk-8-jdk
If you have multiple versions of Java installed, simply use the commands below to set which one should be the default for your system.
sudo update-alternatives --config java
You will be presented with a list of all installed Java versions. Enter the number of the version you want to be used as the default and press Enter
There are 3 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 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press <enter> to keep the current choice[*], or type selection number: 0
Some program require that JAVA_HOME is configured on the system. You can set the default home by using the lines above in the config file.
For Java 17, it displays /usr/lib/jvm/java-17-openjdk-amd64 and Java 8, it’s /usr/lib/jvm/java-8-openjdk-amd64.
To set their homes, run the commands below to open the system environment file.
sudo nano /etc/environment
Then add a line for Java 17 as below:
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
Run the commands below to save your changes.
source /etc/environment
Verify that the JAVA_HOME environment variable was correctly set:
echo $JAVA_HOME
That should output similar line as shown below:
/usr/lib/jvm/java-17-openjdk-amd64
That should do it! You have successfully install OpenJDK on Ubuntu Linux.
How to install Oracle JDK on Ubuntu
If prefer to install Oracle JAVA instead of the open source version above then use the steps below. At the time of this writing, the latest Oracle Java version is 17.
Download the latest Java SE Development Kit 17 LTS release from it from the link below.
Download Oracle Java
Take notes of the version number you’re downloading. If there’s a newer version number then the one below, select it instead.
You can also easily install Java DEB package by running the commands below. At the time of this writing, the current latest version of Java JDK is jdk-17
You may have to replace the highlighted line in the commands below with the latest as it becomes available.
cd /tmp wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.deb"
Now that you’ve downloaded the correct archive package for your system, run the commands below to install Oracle Java.
Again, the current latest version is jdk-17
sudo apt install ./jdk-17_linux-x64_bin.deb
After that, run the commands below to add Oracle Java as an alternative on Ubuntu. The commands below configure Ubuntu to use Java alternatives.
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1711
Now when you run the commands again to list the available Java packages, you should see Oracle Java as an alternative.
sudo update-alternatives --config java
Oracle Java is # 4 on the list. You can enter it to make it the default.
here are 4 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 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 4 /usr/lib/jvm/jdk-17/bin/java 1711 manual mode Press <enter> to keep the current choice[*], or type selection number:
If you don’t have another versions of Java installed, then the commands will return nothing.
Next, run the commands below to make Java 17 to be the default Java compiler for your Ubuntu desktop.
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk-17/bin/jar 1711 sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-17/bin/javac 1711 sudo update-alternatives --set jar /usr/lib/jvm/jdk-17/bin/jar sudo update-alternatives --set javac /usr/lib/jvm/jdk-17/bin/javac
That should get Java installed and configured. To set JAVA environment variables, create a new file in the /etc/profile.d directory for Java JDK.
sudo nano /etc/profile.d/jdk17.sh
Then copy and paste the lines into the end of the file and save.
export J2SDKDIR=/usr/lib/jvm/jdk-17 export J2REDIR=/usr/lib/jvm/jdk-17 export PATH=$PATH:/usr/lib/jvm/jdk-17/bin:/usr/lib/jvm/jdk-17/db/bin export JAVA_HOME=/usr/lib/jvm/jdk-17 export DERBY_HOME=/usr/lib/jvm/jdk-17/db
Next, run the commands below
source /etc/profile.d/jdk17.sh
The commands above should configure Java to work and function with Ubuntu. To test if Java is installed correctly, run the commands below.
You can now verify that Java is installed and configured by outputting its home directory.
echo $JAVA_HOME
It should output similar line as below:
Output: /usr/lib/jvm/jdk-17
Conclusion:
This post showed you how to install Java on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.