Read in 6 minutes
How to Install Java on Ubuntu 18.04
Java is one of the most popular programming languages in the world, used for building different types of cross-platform applications.
This tutorial describes how to install OpenJDK as well as Oracle Java on Ubuntu 18.04.
The same instructions apply for Ubuntu 16.04 and any Ubuntu-based distribution, including Kubuntu, Linux Mint and Elementary OS.
Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges.
Java is distributed in three different editions, Standard Edition (SE), Enterprise Edition (EE), and Micro Edition (ME). This tutorial covers the installation of the Java SE (Standard Edition) edition.
OpenJDK and Oracle Java are the two main implementations of Java, with almost no differences between them except that Oracle Java has a few additional commercial features.
We will show you how to install various Java packages. If you don’t know which Java implementation or version to use, the general recommendation is to stick with the default OpenJDK version available on Ubuntu 18.04.
Installing the Default OpenJDK
The default Java version in Ubuntu 18.04 is OpenJDK 10. Once next LTS version OpenJDK 11 is released, it will become the default Java version in Ubuntu 18.04.
Follow the steps below to install Java OpenJDK on an Ubuntu machine:
First, update the
aptpackage index with:
sudo apt update
Once the package index is updated install the default Java OpenJDK package with:
sudo apt install default-jdk
Verify the installation, by running the following command which will print the Java version:
The output will look something like this:
openjdk version "10.0.2" 2018-07-17 OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4) OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, mixed mode)
Installing OpenJDK 8
At the time of writing, OpenJDK 8 is the current LTS version of Java.
If your application requires Java 8 you can install it by typing the following commands:
sudo apt update sudo apt install openjdk-8-jdk
Installing Oracle Java with APT
In this section, we will go through the steps of installing Oracle Java from the WebUpd8 repository. The WebUpd8 package will automatically download and install Oracle Java 8 JDK for us.
First, add the WebUpd8 repository to your sources list:
sudo add-apt-repository ppa:webupd8team/java
If you get an error message saying
add-apt-repository command not foundthen you will need to install the
When adding the repository, you’ll see a message like this:
Enterto continue and the installer will update the package index.
Update the packages list and install the Oracle Java 8 package with:
sudo apt update sudo apt install oracle-java8-installer
To install the package you’ll need to press
Enterand then accept the Oracle Binary Code license terms by selecting
Yesas shown below:
Once you accept the license the installer will download the Java
tar.gzpackage from the Oracle download page and will install Oracle Java 8 JDK on your Ubuntu system.
Installing Oracle Java from the Oracle Website
At the time of writing this article, the WebUpd8 repository includes only the latest version of Java 8. If you want to install Java 10 or any other specific Oracle Java version then you can simply download the Java tarball from the Oracle’s downloads page.
In this guide, we will download the latest Java version 10. Before downloading the Java
tar.gz file you should check if there is a new version available.
Follow the steps below to install the latest Oracle Java from the Oracle Website:
Start by downloading the Java archive file using the following curl command:
curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/10.0.2+13/19aef61b38124481863b1413dce1855f/jdk-10.0.2_linux-x64_bin.tar.gz
The command above will automatically accept the Oracle license.
Next, create a directory for the Java installation:
sudo mkdir /usr/local/oracle-java-10
Extract the Java
.tar.gzfile to the previously created directory:
sudo tar -zxf jdk-10.0.2_linux-x64_bin.tar.gz -C /usr/local/oracle-java-10
Once the files are extracted run the following commands to create new alternatives:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/oracle-java-10/jdk-10.0.2/bin/java" 1500 sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/oracle-java-10/jdk-10.0.2/bin/javac" 1500 sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/oracle-java-10/jdk-10.0.2/bin/javaws" 1500
The alternatives command is used to create and remove symbolic links which are determining default commands.
We have extracted the Java files in the
/usr/local/oracle-java-10 directory, you can use a different directory if you want.
Set the Default Java Version
To check the default Java version you would use the following command:
openjdk version "10.0.1" 2018-04-17 OpenJDK Runtime Environment (build 10.0.1+10-Ubuntu-3ubuntu1) OpenJDK 64-Bit Server VM (build 10.0.1+10-Ubuntu-3ubuntu1, mixed mode)
If you have multiple Java versions installed on the server you can change the default version using the
update-alternatives tool as shown below:
sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 3 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode 4 /usr/local/oracle-java-10/jdk-10.0.2/bin/java 1500 manual mode Press <enter> to keep the current choice[*], or type selection number:
To change the default Java version just enter the version number (the number in the Selection column) and press
Set the JAVA_HOME Environment Variable
Some applications written in Java are using the
JAVA_HOME environment variable to determine the Java installation location.
To set the
JAVA_HOME environment variable, use the
update-alternatives command to find where Java is installed:
sudo update-alternatives --config java
In our case the installation paths are as follows:
- OpenJDK 10(11) is located at
- OpenJDK 8 is located at
- Oracle Java 8 is located at
- Oracle Java 10 is located at
Copy the installation path of your preferred installation. Next, open the
sudo nano /etc/environment
Add the following line, at the end of the file:
Make sure you replace the path with the path to your preferred Java version.
You can either log out and log in or run the following command to apply the changes to your current session:
To verify that the
JAVA_HOME environment variable is correctly set, run:
/etc/environment is a system-wide configuration file, which is used by all users. If you want to set the
JAVA_HOME variable on a per-user basis then add the line to the
.bashrc or any other configuration file which is loaded when the user logs in.
If for any reason you want to uninstall the Java package, you can uninstall it like any other package installed with
For example, if you want to uninstall the
openjdk-8-jdk package simply run:
sudo apt remove openjdk-8-jdk
In this tutorial, you learned how to install and manage multiple Java versions on your Ubuntu server.
If you have any question or feedback feel free to leave a comment.