This article covers a version of Ubuntu that is no longer supported. If you are currently operating a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:
Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.
See Instead: This guide might still be useful as a reference, but may not work on other Ubuntu releases. We strongly recommend using the following guide for working with Java on Ubuntu: How To Install Java with Apt-Get on Ubuntu 16.04.
Having Java installed is a prerequisite for many articles and programs. This tutorial will guide you through the process of installing and managing different versions of Java on Ubuntu 12.04.
This is the recommended and easiest option. This will install OpenJDK 6 on Ubuntu 12.04 and earlier and on 12.10+ it will install OpenJDK 7.
Installing Java with apt-get
is easy. First, update the package index:
sudo apt-get update
Then, check if Java is not already installed:
java -version
If it returns “The program java can be found in the following packages”, Java hasn’t been installed yet, so execute the following command:
sudo apt-get install default-jre
This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), which is usually needed to compile Java applications (for example Apache Ant, Apache Maven, Eclipse and [IntelliJ IDEA](http://www.jetbrains.com/idea/, etc.) execute the following command:
sudo apt-get install default-jdk
The JDK is usually only necessary if you are going to compile Java programs or if your software specifically requires it in addition to Java. Since the JDK contains the JRE, there are no disadvantages if you install the JDK instead of the JRE, except for the larger file size.
All other steps are optional and must only be executed when needed.
To install OpenJDK 7, execute the following command:
sudo apt-get install openjdk-7-jre
This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), execute the following command:
sudo apt-get install openjdk-7-jdk
The Oracle JDK is the official JDK; however, it is no longer provided by Oracle as a default installation for Ubuntu.
You can still install it using apt-get. To install any version, first execute the following commands:
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
Then, depending on the version you want to install, execute one of the following commands:
This is an old version but still in use.
sudo apt-get install oracle-java6-installer
This is the latest stable version.
sudo apt-get install oracle-java7-installer
This is a developer preview, the general release is scheduled for March 2014. This external article about Java 8 may help you to understand what it’s all about.
sudo apt-get install oracle-java8-installer
When there are multiple Java installations on your Droplet, the Java version to use as default can be chosen. To do this, execute the following command:
sudo update-alternatives --config java
It will usually return something like this if you have 2 installations (if you have more, it will of course return more):
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-7-oracle/jre/bin/java 1062 auto mode
1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
2 /usr/lib/jvm/java-7-oracle/jre/bin/java 1062 manual mode
Press enter to keep the current choice[*], or type selection number:
You can now choose the number to use as default. This can also be done for the Java compiler (javac
):
sudo update-alternatives --config javac
It is the same selection screen as the previous command and should be used in the same way. This command can be executed for all other commands which have different installations. In Java, this includes but is not limited to: keytool
, javadoc
and jarsigner
.
To set the JAVA_HOME
environment variable, which is needed for some programs, first find out the path of your Java installation:
sudo update-alternatives --config java
It returns something like:
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-7-oracle/jre/bin/java 1062 auto mode
1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
2 /usr/lib/jvm/java-7-oracle/jre/bin/java 1062 manual mode
Press enter to keep the current choice[*], or type selection number:
The path of the installation is for each:
/usr/lib/jvm/java-7-oracle
/usr/lib/jvm/java-6-openjdk-amd64
/usr/lib/jvm/java-7-oracle
Copy the path from your preferred installation and then edit the file /etc/environment
:
sudo nano /etc/environment
In this file, add the following line (replacing YOUR_PATH by the just copied path):
JAVA_HOME="YOUR_PATH"
That should be enough to set the environment variable. Now reload this file:
source /etc/environment
Test it by executing:
echo $JAVA_HOME
If it returns the just set path, the environment variable has been set successfully. If it doesn’t, please make sure you followed all steps correctly.
<div class=“author”>Submitted by: <a href=“http://koenv.com”>Koen Vlaswinkel</a></div>
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Thank you for very clear tutorial, jp
Very good tutorial… Thank you :)
Java 8 instructions are outdated
@jformoso: What is the problem you’re seeing? What version of Ubuntu are you trying to install it on?
I was getting an error add-apt-repository: command not found
To fix it had to install sudo apt-get install software-properties-common
This was to install the oracle-jdk on an Ubuntu 12.10x64 droplet
Thank you from Rome, very good tutorial ;)
Thanks for the tutorial. I needed to add “-y” to the add-apt-repository.
Thanks for the tutorial. It is very needful. I set this JAVA_HOME path but I also want know "how to set a path for MAVEN? please guide me in this regard.
@anandsbj1989: It depends on how you installed Maven. If you ran: <pre> sudo apt-get install maven2 </pre> Then you would use “/usr/share/maven2/”
amazing tutorial thanks.