I recently upgraded to the latest LTS version on my dev machine and needed to install Java. I couldn’t remember exactly what I did last time but knew that I needed to download the tarball from Oracle, save the extracted files somewhere, add the runtime (java), SDK compiler (javac) and webstart (javaws) executables to the ubuntu alternatives list and add a JAVA_HOME refererence for other tools like Gradle.
This post is to document how I did this for future reference, but will hopefully be useful to other people solving the same problem. I understand that there is an apt package that I can add and do it that way but it is not owned, maintained or controlled by Oracle. Because I am who I am, I do it manually. This is my own opinionated approach and it works. This is meant to be a guide, I take zero responsibilty for any problems you may encounter while performing the next few steps. All of that being said, please post questions/comments if you encounter issues.
Lets get started.
Step #1: Download the Java 8 SDK from Oracle.
Because of the speed at which technology changes and Java is headed toward version 10, I am hesitant to share links. This link may or may not work by the time you are reading this. If in the case it no longer works, visit google and search for ‘Java SE Development Kit 8 Downloads’.
I chose to download 8u172, Linux x64, the one ending in .tar.gz. If you are running RHEL (Redhat Enterprise Linux), CentOS or Fedora, you should download the rpm and be aware that these instructions won’t work for you at the point of updating the alternatives. This is a guide for Ubuntu.
Step #2: Move tarball to Installation Reference Location.
The subtitle for this section can be deceiving if you are not familiar with how symlinks or references work. But stay with me here.
Create a directory at /usr/lib called jvm-oracle. You will need to use the sudo command as this directory is at root level:
sudo mkdir /usr/lib/jvm-oracle
Copy your tarball over:
sudo cp ~/Downloads/(name of your tarball) /usr/lib/jvm-oracle
Move into the /usr/lib/jvm-oracle and extract your tarball:
sudo tar -xvzf (name of tarball)
List out the directory contents and find your extracted folder:
You should see a directory like ‘jdk1.8.0_172’ which is mine at the time of this article. Move into your dir and the bin folder with cd and list out the contents.
Move to new dir:
cd jdk1.8.0_172. Tip: if you type a part of the directory name and hit the tab key, it should auto complete. If it doesn’t, type a little more of it and try hitting tab again.
Move to bin:
List out Contents:
This directory (bin) contains all of the executables you will need to reference for updating your alternatives. Get the path to this directory and copy/paste it into gedit or your favorite text editor.
Step #3: Add to Ubuntu Alternatives.
I like to prepare the next three command lines in a text editor so I can just copy/paste them into the command line and hit enter.
Here are mine, double check the jdk directory name before using them.
NOTE: Be sure to paste them into a text editor before pasting to the command line. And paste/execute one line at a time.
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm-oracle/jdk1.8.0_172/bin/java 1 sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm-oracle/jdk1.8.0_172/bin/javac 1 sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm-oracle/jdk1.8.0_172/bin/javaws 1
Since references are directly symlinked to your path, you should immediately have the ability to check to see if your update-alternatives commands worked.
Java Runtime Version:
Java SDK Compiler Version:
Java Web Start Version:
You should see version output for the above commands when hitting enter after entering each one. If you don’t, re check your update-alternatives commands.
Step #4: Add JAVA_HOME to your environment.
Here is a fancy command that can be used to append a JAVA_HOME to the end of your .bashrc file. If you manually type, be sure to take notice of single quotes versus backticks. This is very important.
echo $'\nJAVA_HOME='`which java` >> ~/.bashrc
Check to make sure it appended:
If you need to change anything, open in vim, nano or gedit to fix:
Alternatively, you can add it manually by opening your .bashrc file and setting
Close your terminal, reopen a new one and type
echo $JAVA_HOME to ensure your environment var has been properly set. If you don’t see
/usr/bin/java for output, close all terminal sessions, open a new terminal session and try the
echo $JAVA_HOME again. If you are still having issues, check the entry in your .bashrc file again.
If you want to set a system-wide JAVA_HOME, add to your /etc/environment file. Note: Because /etc/environment is under the root path, you will need to use the sudo command before your editor command.
That is all I have folks. Be sure to post questions/comments. Happy coding!