How to install Java 6 on ubuntu 12.04 / 12.10

Source of Confusion: Many Versions of Java

You will find many methods to install java / jre/ jvm on web and thus it can be very much confusing to a user which method to use. Given there are many implementations of Java available, adds to the confusion. The different implementations of JDK available are:

  1. Java from Oracle (earlier provided by Sun which has been acquired by Oracle
  2. Java available from IBM
  3. OpenJDK
  4. GNU’s JDK

On an ubuntu system, one expects Java to be available as standard debian package installable with apt-get install <package-name>. But on ubuntu packages installed this way is the Open Source Version. If that suits your purpose, you can simply install it using:

$ sudo apt-get install openjdk-6-jdk

However if you wish to install the “Standard Java (earlier Sun Java)”, then it too can be installed either directly from binary distribution or from ubuntu alternate package repository.

Most Common Way:

The simplest way to install java is from a third party repository with

$ sudo add-apt-repository ppa:webupd8team/java

$ sudo apt-get update

$sudo apt-get install oracle-java7-installer


But this works only for Java 7 as Java 6 is not available from ubuntu repositories because of licensing issues. If you download and install Java 6 locally on you machine in your home directory then it is not available to other users on the system and sometimes launching an application which is dependent upon java would be unable to find the java installed in your home directory. Moreover you have to mess with JAVA_HOME, CLASSPATH etc. environment variables. In such a case the best method is to install Java6 manually by following the steps below:

UPDATE: Today I was trying to build android from source and discovered that Java 6 is available now from webupd8team repository. You can simply install oracle / sun java 6 with

$sudo apt-get install oracle-java6-installer

How to install Java 6 Manually

Download the jdk binary from oracles website and follow the steps below:

 $ chmod u+x jdk-6u34-linux-i586.bin
 $ ./jdk-6u34-linux-i586.bin
 $ sudo mkdir -p /usr/lib/jvm
 $ sudo mv jdk1.6.0_34 /usr/lib/jvm/
 $ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.6.0_34/bin/java" 1
 $ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.6.0_34/bin/javac" 1
 $ sudo update-alternatives --install "/usr/lib/mozilla/plugins/" "" "/usr/lib/jvm/jdk1.6.0_34/jre/lib/i386/" 1

Want to install Multiple Versions of Java?

If you already have some other version of java installed on your system say like the Java 7 or say the OpenJDK then you can choose the default java to be used on your system using the update-alternative command. Running the update-alternative shows the list of alternatives for the command and you can choose one of the options listed. As an example if say there are multiple programs fulfilling the same functionality then which one is to be used by a program is not clear. For example say you have installed Aptana Studio IDE on your system which is Eclipse based then to run Aptana it requires jre. If there are multiple versions of jre installed on your system then it is not clear which one should be used to run Aptana. This is where update-alternatives comes into play. For more details check the man page of update-alternatives. To select the version of java to be used by default run the following commands and select the appropriate version.

$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac
$ sudo update-alternatives --config
$ sudo update-alternatives --config javaws

Running the commands above would show a prompt something like this. The details may vary on system to system.

There are 2 choices for the alternative java (providing /usr/bin/java).  
Selection Path Priority Status 
* 0 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 auto mode 
1 /usr/lib/jvm/jre1.7.0/jre/bin/java 3 manual mode  

Press enter to keep the current choice[*], or type selection number: 1