It’s a pretty straightforward and easy process, and we’ll show you step-by-step instructions on how to install Java on Ubuntu. These instructions will work for Ubuntu 16.04, 18.04, Ubuntu 20.04, and any other LTS release, or even other Ubuntu-based distros like Xubuntu.
This should work for any Ubuntu-based distro, even non-LTS versions like 20.10, but they were tested on Ubuntu 18.04 and Ubuntu 20.04. They should work on both servers and desktops.
In a hurry? Click here and follow the instructions.
Before we begin installing Java
Some things you’ll need before we start installing Java:
- An Ubuntu server. You can get one from Vultr or any other cloud hosting provider
- A sudo/root user. All commands below are executed by the root user. If you’re using a non-root user then you’ll need to append ‘sudo’ to most commands.
- An SSH client like MobaXterm (if you use Windows), or SSH enabled (if you use Ubuntu)
- Make sure you don’t have Java already installed. If you do, remove it, or if you want to use multiple Java versions/installs, then keep it and configure them later.
So that’s pretty much it.
There are multiple ways of installing Java
We’ll order them based on difficulty (easiest first):
- Contact us and we’ll install and configure Java for you (any version, any OS)
- Installing the latest Java using the default (Ubuntu’s) JDK using apt-get (use this if you don’t know what to choose)
- Installing Java 11 or Java 13 using Oracle’s JDK with apt-get
- Installing Java manually using Oracle’s JDK (not recommended for beginners)
How to install Java (the default JDK) on Ubuntu using apt-get
We’ll start with our instructions on how to install the latest recommended version of Java (JDK) using the Ubuntu package. By installing the JDK (Java Development Kit), you’ll also install the JRE (Java Runtime Environment). This is the OpenJDK package which is an open source implementation of Java.
Step 1: Update Ubuntu
The first thing you should always do is update your system. You can do so by running the following commands:
apt-get update && apt-get upgrade
Step 2: Install the default JDK
Run the following command:
apt-get install default-jdk
And that’s it. Depending on when you’re reading this, the command will install Java 11 or a newer version. You can now move on to step 3 and configure your Java.
How to install Java 17 using the Oracle JDK
If you don’t want to use Ubuntu’s default open source JDK, you can use the official Oracle JDK through a 3-rd party repository.
Step 1: Update Ubuntu
Again, you should always update your system first before you do anything else. Run the following commands:
apt-get update && apt-get upgrade
And install the required package if you don’t have it already installed:
apt-get install software-properties-common
Step 2: Add the Java repository
The first thing you need to do is add a 3-rd party repository to get the Oracle JDK. We’ll use the one from Linux Uprising, but you can use any other repository:
add-apt-repository ppa:linuxuprising/java
And then update your package list again:
apt-get update
Step 3: Install Java 17
So to install the JDK 17th (LTS), latest version, run the following command:
apt-get install oracle-java17-installer
The command is pretty similar for other versions too (if you want to install another version, replace the XX)
apt-get install oracle-javaXX-installer
And that’s it. You can now move on to step 4 and configure your Java.
No longer recommended: How to install Java 8 using the Oracle JDK
To install Java 8 (which will reach its end of life January 2019!), follow these instructions:
Step 1: Update Ubuntu
Again, you should always update your system first before you do anything else. Run the following commands:
apt-get update && apt-get upgrade
And install the required package if you don’t have it already installed:
apt-get install software-properties-common
Step 2: Add the Java repository
The first thing you need to do is add a 3-rd party repository to get the Oracle JDK. We’ll use the one from WebUpd8, but you can use any other repository:
add-apt-repository ppa:webupd8team/java
And then update your package list again:
apt-get update
Step 3: Install Java
So to install the JDK 8th, outdated version, run the following command:
apt-get install oracle-java8-installer
And that’s it. You can now move on to step 4 and configure your Java.
How to install Java manually using Oracle’s JDK on Ubuntu
This step is not recommended for beginners. You won’t get updates from Ubuntu’s repositories, you’ll have to manually update your Java each time there’s a new release. If you want to install a custom version of Oracle’s JDK that’s not available via a package, you can follow the steps below.
Step 1: Update
As always. Update first:
apt-get update && apt-get upgrade
Step 2: Download the JDK
Go to Oracle’s downloads page and select the version you want to download. We’ll use Java’s Standard Edition 17 for the purpose of this tutorial. Replace the version numbers if you’re going to use a different version. Java 17 is only available for 64-bit systems, so if your Ubuntu is 32-bit, you must use a different version.
Update 2019: Now that you need an Oracle.com account to download the file below, you’ll have to manually download the file and continue the process after the next step. Alternatively, you can just use the Ubuntu repositories.
Download the .tar.gz file:
wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz
The Java version and this link often change, so please go to the download page yourself and copy the link yourself.
Create a directory for your Java installation:
mkdir /opt/java
This is where we’ll install Java. You can use a different directory if you want to install it elsewhere like /usr/local
Step 3: Install Java
Now extract the .tar.gz (tarball) file to the directory you previously created
tar -zxf jdk-17_linux-x64_bin.tar.gz -C /opt/java
And that’s it. move on to step 4 and configure your Java.
Setting up Java on Ubuntu
Now it’s time to configure your Java.
Check what Java version you’re using
First, make sure you’ve installed Java on your system and check what version you have:
java -version
The output should be something like:
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39) Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
So based on this, we’re using version 11.
Set a default if you have multiple Java installations
If you have multiple Java installations, you can set a default one by using the following command:
update-alternatives --config java
You can also use this command to check if you have multiple installations.
You’ll get an output with a list of installed Javas. Press enter to keep the default one without any changes or enter a number to select a different default Java.
Set the JAVA_HOME variable
You’ll most likely need to set the JAVA_HOME variable so other applications can find the location of your Java installation. To find the Java installation path, run the previous command again:
update-alternatives --config java
And copy the installation path – second column – under “Path”.
Next, open the file “/etc/environment” with a text editor
nano /etc/environment
And add the following line at the end of the file:
JAVA_HOME="/your/java/installation-path"
Of course, make sure you update the path with the one you previously copied, example:
JAVA_HOME="/usr/lib/jvm/java-17-oracle"
Save the file and then reload it:
source /etc/environment
To test if everything’s done right, you can check your JAVA_HOME variable using:
echo $JAVA_HOME
And the output should be your Java installation path.
There are multiple ways of setting up the JAVA_HOME variable, and multiple other variables that you may need to set, depending on what you need and what you’re going to use.
If you need any help or have any questions, leave a comment.
34 thoughts on “How to Install Java on Ubuntu”
wget –header “Cookie: oraclelicense=accept-securebackup-cookie” http://download.oracle.com/otn-pub/java/jdk/9.0.1+11/jdk-9.0.1_linux-x64_bin.tar.g link not working
The link often changes, so it’s best if you go to the download page and get the link yourself. I updated the post with a working link now for JDK 9.0.4
Hi there, i have followed your instructions but when i typed “apt-get install oracle-java9-installer” i got this error : “Package ‘oracle-java9-installer’ has no installation candidate”. idk how to solve this, i have searched over the internet, but never work.
Thank you.
The repo might be temporary down or have some issues since the new Java release. Try again in a few days and make sure you do
apt-get update
before running the install command. If it doesn’t work either useapt-get install default-jdk
or download and install the latest Java from oracle.come as per our instructions.I have followed instructions but my system does not let me replace environment file with added JAVA_HOME variable:
JAVA_HOME=”/usr/lib/jvm/java-9-openjdk-amd64/bin/java”
Apart from this I believe I have followed all your instructions, thanks for info.
Make sure you’re root or executing with sudo.
Also make sure you’re adding the correct directory, and make sure the quotes are entered right (copying can sometimes cause issues)
If it doesn’t work again, try different instructions
Thanks. It’s helpful.
Thanks, these are good instructions, but you probably shouldn’t put “java” at the end of the JAVA_HOME variable, it should end with “bin”
Great post, thanks!!
Just a minor note, JAVA_HOME should point to “/usr/lib/jvm/java-9-oracle” –> https://docs.oracle.com/cd/E19182-01/820-7851/inst_cli_jdk_javahome_t/
Yes, thank you. JAVA_HOME should point to where Java is actually installed.
hello
THanks but it isn’t work for me
what shuold i do?
http://uupload.ir/files/nvqp_screenshot_from_2018-04-28_15-43-28.png
What to do when it says environment is read only ??
Open with `sudo`
Even then if it shows read-only message, `chmod` it
Thanks man. I followed it through and through and it worked. Great work.
worked for me
>> though you’ll find many other outdated tutorials saying 9 is still a “developer/beta/preview” release, it’s actually stable
Maybe it’s stable, but it’s not a “Long term support” release. JDK 9 will stop getting bug fixes and security patches as of March 2018, (before the article was released) before 8 does.
If you can use Open JDK, go with version 11.
If you have to use an enterprise version, go with version 8.
Thanks for the comment. The tutorial was a bit outdated so we updated it with Java 11 and Java 8 only.
Greate help to install java on ubuntu 18.10
Thanks for your help. The Oracle page wasn’t this thorough
What page specifically? The link to Oracle’s download page works. It may give you an error if you didn’t use the cookie tag.
Thank you so much 🙂
Perfectly detailed steps i got over the problem of JDK and database issues and now everthing is running perfectly awesome thanks a bunch
Great!
Straight-forward, well organized.
Thank you.
Lovely work thank you!
where is step 4?
It’s at https://thishosting.rocks/install-java-ubuntu/#set-up
Good!! Thanks.
How to uninstall java 11?
Try this: https://askubuntu.com/questions/84483/how-to-completely-uninstall-java
In step 3 please updata JAVA13 command to be
apt-get install oracle-java13-installer
rather than
apt-get install oracle-java11-installer
Oops, you’re right. Thanks, updated the tutorial.
It works for me. Thanks a ton!!!
What exactly means: And copy the installation path – second column – under “Path”. ?
When you enter the previous command you’ll get results. In those results, the second column/row is the Path to your Java installation. Copy that path because you’ll need it later.
These instructions do not work.# apt-get install oracle-java11-installer
Reading package lists… Done
Building dependency tree
Reading state information… Done
Package oracle-java11-installer is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
oracle-java11-installer-local
E: Package ‘oracle-java11-installer’ has no installation candidate