How To Install Java on Ubuntu with Apt-Get


As a lot of articles and programs require to have Java installed, this article will guide you through the process of installing and managing different versions of Java.

Installing default JRE/JDK

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 execute the following command:

sudo apt-get install default-jdk

That is everything that is needed to install Java.

All other steps are optional and must only be executed when needed.

Installing OpenJDK 7 (optional)

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

Installing Oracle JDK (optional)

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:

Oracle JDK 6

This is an old version but still in use.

sudo apt-get install oracle-java6-installer

Oracle JDK 7

This is the latest stable version.

sudo apt-get install oracle-java7-installer

Oracle JDK 8

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

Managing Java (optional)

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.

Setting the “JAVA_HOME” environment variable

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:

  1. /usr/lib/jvm/java-7-oracle
  2. /usr/lib/jvm/java-6-openjdk-amd64
  3. /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):


That should be enough to set the environment variable. Now reload this file:

source /etc/environment

Test it by executing:


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.


no Dashboard appears,Unity doesn’t load, no Launcher



You just need to turn the Unity plugin back on. The problem is this is a pain in the bottom because you’ve now got no graphical method to do this. So:

  1. Press Ctrl+Alt+F1 and log in.
  2. Install the jibby you’ll need to configure the settings by running this:
    sudo apt-get install compizconfig-settings-manager
  3. Then run it by doing this:
    export DISPLAY=:0

    The first part tells the terminal which display you want it to load on (otherwise it won’t have a clue).

  4. Press Ctrl+Alt+F7 (or Ctrl+Alt+F8 sometimes) to get back to the graphical display where there should be a CompizConfig Settings Manager screen sitting there.
  5. Find the Unity plugin. Enable it.
  6. Everything should spring into life but if it doesn’t, you might have to restart. You can do that by going back to TTY1 and running sudo reboot.


For 12.10 and below:

Press Ctrl+Alt+T for a terminal and run ccsm, then re-enable your ‘Unity’ plugin.

You also may then need to run a unity --reset.



In 13.04:

unity --replace is deprecated. Instead, use the following:

dconf reset -f /org/compiz/ 
unity --reset-icons &disown

Reboot if it doesn’t work right away.


A solution good for me (has solved the same problem):

in a terminal:

sudo dconf reset -f /org/compiz/

and then

setsid unity

I hope it’ll be useful.


  1. Press Ctrl+Alt+F2.
  2. Log in.
  3. Run sudo shutdown -r now in a terminal.
  4. At the login screen after your system restarts, choose Unity 2D (for now) by right-clicking the sprocket icon.
  5. Wait for a fix for Unity 3D/Ubuntu 11.10 (e.g. a week).
  6. Update your system to get the fix; in a terminal type sudo apt-get update && sudo apt-get upgrade.

UPDATE: I was able to resolve this by using ccsm, from Unity 2D login.

(This is adapted from this Ubuntu Forums post.)

  1. At the login screen, select Unity 2D and login.
  2. Install ccsm (CompizConfig Settings Manager) by running in a terminal: sudo apt-get install compizconfig-settings-manager compiz-fusion-plugins-extra.
  3. Start ccsm, and select the category Desktop. Enable the Ubuntu Unity Plugin; if asked to resolve conflicts, select the MIDDLE button for all conflicts. Close ccsm when done.
  4. Restart, select Unity (3D) and login.


The easiest way to do this is to press Ctrl+Alt+T to get a terminal and then issue:

rm -rf ~/.compiz-1 ~/.config/compiz-1

and your unity session will come back before your eyes..

This technique is better than re-launching ccsm, because you can get into OP’s error condition without even accidentally de-selecting unity in ccsm; you can get it simply by accidentally pressing the “preferences” button in ccsm.


I had this problem too. I solved it with deleting the content of the ~/.config/compiz-1/compizconfig file and relog.

rm -rf ~/.config/compiz-1/compizconfig/*

Method:- Follow the given link

Getting message “sudo: must be setuid root”, but sudo IS already owned by root

This problem is caused sometimes when the permissions of the file, /usr/bin/sudo get set to 777. If you do something like chmod -R 777 /usr/, you can do this. It effectively ruins sudo. Here is the solution if this is your problem, and the accepted answer doesn’t work:

To fix:

Restart pc, press shift at boot menu.

This should bring up GNU GRUB (ie recovery mode) menu.

  • If this doesn’t work, just restart mid boot and choose recovery mode when prompted on next launch.

Select the line which starts with Advanced options

Select the topmost version of the OS ending with (“recovery mode”)

Press enter

In the following menu, go down to “Drop to root shell prompt”

Type the following:

mount -o remount,rw /

mount --all

chown root:root /usr/bin/sudo

chmod 4755 /usr/bin/sudo


This should restore sudo privellages.

commands not working in ubuntu terminal

The command could not be located because ‘/usr/bin’ is not included in the PATH environment variable.

Just set the PATH variable on the command line:

export PATH=/usr/bin:/bin
sudo nano /etc/environment, copy and paste given line