Mark McGranaghan

Install Java on Ubuntu Lucid Programatically

July 10 2010

The following commands install Sun’s Java 6 JRE on Ubuntu Lucid without interactively prompting the user. This script was tested on Amazon EC2 AMIs ami-2d4aa444 and ami-fd4aa494.

sudo add-apt-repository "deb lucid partner"

cat << EOD | sudo debconf-set-selections
sun-java5-jdk shared/accepted-sun-dlj-v1-1 select true
sun-java5-jre shared/accepted-sun-dlj-v1-1 select true
sun-java6-jdk shared/accepted-sun-dlj-v1-1 select true
sun-java6-jre shared/accepted-sun-dlj-v1-1 select true

sudo dpkg --set-selections << EOS
sun-java6-jdk install

sudo apt-get update
sudo apt-get install sun-java6-jre -y
sudo update-java-alternatives -s java-6-sun --jre

java -version

The addition of parter repository to the apt sources list is needed because the Sun Java JRE is not available in the default apt repositories on Lucid. The debconf-set-selections and dpkg --set-selections calls eliminate the need for user interaction during the Java install. Finally, the update-java-alternatives line ensures that the java command on the system in question uses the Java 6 Sun JRE.