A typical installation of Ubuntu comes prepacked with the OpenJDK environment. This environment is substantially enough for regular users who wish to develop simple Java programs. However, some Java libraries are not included or compatible with the OpenJDK environment. Take Atlassian Jira as an example of an application that requires the more traditional and proprietary Sun Java JDK platform. Due to its proprietary status, Ubuntu does not include this in its repository. Hence, we would need to install this manually.
Despite the title, this guide has also been tested in a Fedora, Debian and CentOS environment. If it doesn't work for you, let me know by leaving a comment below! :)
Cover photo by Caspar Rubin on Unsplash
Check Operating System Type
First things first, check whether you’re running on 32-bit or 64-bit Ubuntu:
uname -m
The result will show you what type of operating system you have:
- i686 – 32-bit kernel
- x86_64 – 64-bit kernel
Instructions
Initial setup
Now, go to the Oracle JDK download site and decide which version of JDK you would prefer.
In this guide, we'll be downloading Java JDK 8 Update 162. Accept the agreement, and copy the tar.gz
link for your operating system type.
We'll use wget
to download the compressed file. In your terminal, type the following:
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jdk-8u162-linux-x64.tar.gz"
The cookie header is included in the wget
command because Oracle does not allow a direct download without accepting their agreement; thus, we parse a cookie to ensure that Oracle recognises that you have accepted their agreement.
Create a new directory under opt
:
sudo mkdir /opt/java
Then, extract the compressed file to the above directory:
sudo tar -xzvf jdk-8u162-linux-x64.tar.gz -C /opt/java/
Verify that the files are extracted correctly:
cd /opt/java
ll
total 12K
drwxr-xr-x 3 root root 4.0K Jan 10 05:16 .
drwxr-xr-x 3 root root 4.0K Jan 10 05:12 ..
drwxr-xr-x 8 uucp 143 4.0K Sep 6 02:32 jdk1.8.0_162
Now, we’ll create a symbolic link called current
and link it with the newly extracted JDK directory:
sudo ln -s jdk1.8.0_162/ current
ll
total 12
drwxr-xr-x 3 root root 4096 Jan 10 05:20 ./
drwxr-xr-x 3 root root 4096 Jan 10 05:12 ../
lrwxrwxrwx 1 root root 12 Jan 10 05:20 current -> jdk1.8.0_162/
drwxr-xr-x 8 uucp 143 4096 Sep 6 02:32 jdk1.8.0_162/
Set Environment Variables and Path
Finally, we’re going to set the $JAVA_HOME
environment variable so that you’ll be able to run most Java applications.
To set for a single user:
Let’s load the .bash_profile
from the home directory:
nano ~/.profile
Go to the end of the file, and add the following:
export JAVA_HOME=/opt/java/current
export PATH=$PATH:$JAVA_HOME/bin
Hit Ctrl
and x
to save the file, and y
to overwrite it.
Finally, to ensure the changes are instant, run the following command:
source ~/.profile
To set for all users:
This step is similar to the above, with the only exception being that you would need to edit the global profile
file. This allows every user the right to understand the environment variable that was set.
The global profile
file can be found in the /etc/
directory. You'll need to use the sudo
command to make changes to this file.
Verification
Once it's all completed, run the java -version
command to verify the correct Java version and type:
java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)