I am trying to install the Java Development Kit (JDK) on Ubuntu Linux distro, but I am unable to install it.

What are the steps to install it on Ubuntu?

share|improve this question
    
Which JDK version? Java6 or 7? 32 or 64 bit version? – Joachim Isaksson Feb 9 '13 at 12:59
    

22 Answers 22

up vote 539 down vote accepted

Referring to Ask Ubuntu question How to set JAVA_HOME for OpenJDK?,

How to install "Open JDK" (Java developement kit) in Ubuntu (Linux)?

  1. Open Terminal from Application Dash or press Ctrl+Alt+T

  2. Run the following command in Terminal:

    sudo apt-get install openjdk-8-jdk
    sudo apt-get install openjdk-8-source #this is optional, the jdk source code
    
  3. Type commandline as below...

    apt-cache search jdk
    

    (Note: openjdk-8-jdk is symbolically used here. You can choose the JDK version as per your requirement.)

  4. For "JAVA_HOME" (Environment Variable) type command as shown below, in "Terminal" using your installation path...

    export JAVA_HOME=/usr/lib/jvm/java-8-openjdk
    

    (Note: "/usr/lib/jvm/java-8-openjdk" is symbolically used here just for demostration. You should use your path as per your installation.)

  5. For "PATH" (Environment Variable) type command as shown below, in "Terminal" using your installation path...

    export PATH=$PATH:/usr/lib/jvm/java-8-openjdk/bin
    

    (Note: "/usr/lib/jvm/java-8-openjdk" is symbolically used here just for demostration. You should use your path as per your installation.)

  6. Check for "open jdk" installation, just type command in "Terminal" as shown below

    javac -version
    
share|improve this answer
19  
I copied your "export PATH=" ... command and screwed up my box. Please correct it to: "export PATH=$PATH:/usr/lib/jvm/java-6-openjdk/bin" – IEnumerator Jun 27 '13 at 23:08
43  
I'd replace export PATH=$PATH:/usr/lib/jvm/java-6-openjdk/bin with export PATH=$PATH:$JAVA_HOME/bin, which is more DRY – bevacqua Dec 18 '13 at 20:28
2  
just replace everywhere java-6-openjdk to java-7-openjdk if you want to install java7 – Tebe May 30 '14 at 12:57
15  
You better check what is the actual folder. In my case it was: /usr/lib/jvm/java-7-openjdk-amd64 – chelder Jan 31 '15 at 20:21
3  
For ubuntu 16.04 use openjdk-8-jdk There is noopenjdk-7-jdk see here – naomi Oct 25 '16 at 5:32
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer
share|improve this answer
2  
For more information: webupd8.org/2012/01/… – marcelocra Feb 3 '14 at 16:39
28  
This is what I used. If you want java6 : sudo apt-get install oracle-java6-installer . if you want java8 : sudo apt-get install oracle-java8-installer . If you have multiple java installed on your system, you can select with : sudo update-alternatives --config java – Guagua Jun 13 '14 at 22:27
8  
This is the correct answer – thangamanikasi Jul 5 '15 at 3:23
1  
This is the easiest answer and working with all jdk versions, just replace java7 with java6, 8 or 9 and you will be able to install the version you want! – XIII Sep 14 '16 at 18:57
    
Easy to remove them also, eg: sudo apt-get remove oracle-java7-installer – ITJscott Jan 16 '17 at 19:30

You can install Oracle's JDK 1.7 fairly easily too; as an example this is how to install JDK 1.7.0_13;

As root, do;

cd /usr/local
tar xzf <the file you just downloaded>

As your normal user, add or change these two lines in your ~/.profile to point to the installation;

export JAVA_HOME=/usr/local/jdk1.7.0_13
export PATH=$PATH:$JAVA_HOME/bin

If it's an update, you may also want to remove the old java installation directory in /usr/local.

Log out and in again (or do . ~/.profile), and everything should just work.

The downside with Oracle's JDK is that it won't update with the rest of your system like OpenJDK will, so I'd mostly consider it if you're running programs that require it.

share|improve this answer
1  
do i need to give the path to the downloaded folder in tar xzf command – Illegal Argument Nov 16 '13 at 4:29
3  
@Subash you need the downloaded file to be in th /usr/local directory before running the tar xzf command. To get that, cd to the path where your downloaded file is and run: sudo mv <nameOfYourFile.tar.gz> /usr/local – Jorge Antonio Díaz-Benito Nov 18 '13 at 9:29
2  
Dont forget to sudo – Nidhin David Jul 18 '15 at 15:12
    
Hi, new to linux, I just have to extract the archive and it's installed? – sliders_alpha Mar 4 '16 at 9:45
    
You may also need to define aliases for JVM and Java Compiler and put them inside your ~/.profile file: alias java='$JAVA_HOME/bin/java' and alias javac='$JAVA_HOME/bin/javac' – Haitham Sweilem Apr 26 '16 at 23:13

In case you have already downloaded the ZIP file follow these steps.

Run the following command to unzip your file.

tar -xvf ~/Downloads/jdk-7u3-linux-i586.tar.gz
sudo mkdir -p /usr/lib/jvm/jdk1.7.0
sudo mv jdk1.7.0_03/* /usr/lib/jvm/jdk1.7.0/
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0/bin/javaws" 1

After installation is complete, set environment variables as follows.

Edit the system path in file /etc/profile:

sudo gedit /etc/profile

Add the following lines at the end.

JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH

Source: http://javaandme.com/

share|improve this answer
    
Thanks! I was looking for the method using update-alternatives – GermanK Sep 27 '14 at 14:28
5  
Where did the JRE_HOME variable come from? – Foreever Apr 26 '16 at 11:44

If you want to install the latest JDK 1.8, use the webupd8team PPA. Add the repository in your system:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

You can now install Oracle Java 8 using the following command:

sudo apt-get install oracle-java8-installer

This ppa repository also provides a package to set environment variables automatically. Just type :

sudo apt-get install oracle-java8-set-default

Hope this will help!

share|improve this answer

OpenJDK is OK for the most cases, but Oracle JDK can be required for some bank client applications (my case) - I can't use OpenJDK.

I'm surprised that I don't see any answer with default method (repository without external PPAs) in Ubuntu 12.10+ for Oracle's JDK - will try to describe it.

  • install JavaPackage:
    sudo apt-get install java-package
  • download Oracle JDK from Oracle downloads page
  • make debian package from downloaded .tar.gz archive:
    make-jpkg jdk-YOUR_VERSION-linux-PLATFORM.tar.gz
    this command will produce .deb package
  • install package with your favourite way (for example, sudo dpkg -i oracle-java8-jdk_8u40_amd64.deb)

It's officially supported way from debian developers for installing Oracle JDK, and I suppose it's very simple.

share|improve this answer
    
This is easily the best answer. Took a couple minutes and gave the official JDK, since some apps don't like OpenJDK too much. – refi64 Jul 24 '16 at 21:24
    
Reference: wiki.debian.org/JavaPackage – prolibertas Jun 16 '17 at 20:31
    
can we add the documentation with that method? – nyxee Aug 19 '17 at 23:19
    
note: you can also install the documentation easily this way. this should be the most recommended way of installing java. – nyxee Aug 20 '17 at 5:28

Installing Java 8 on Ubuntu

First you need to add webupd8team Java PPA repository in your system and install Oracle Java 8 using following set of commands.

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer

Verify Installed Java Version

After successfully installing oracle Java using above step verify installed version using following command.

$ java -version


java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

Configuring Java Environment

In Webupd8 ppa repository also providing a package to set environment variables, Install this package using following command.

$ sudo apt-get install oracle-java8-set-default

reference

share|improve this answer

Simply run:

sudo apt-get install default-jdk
share|improve this answer

Wiki from Ubuntu website:

For Ubuntu 10.04 LTS, the sun-java6 packages have been dropped from the Multiverse section of the Ubuntu archive. It is recommended that you use openjdk-6 instead.

If you can not switch from the proprietary Sun JDK/JRE to OpenJDK, you can install sun-java6 packages from the Canonical Partner Repository. You can configure your system to use this repository via command-line:

 sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
 sudo apt-get update   
 sudo apt-get install sun-java6-jre sun-java6-plugin
 sudo update-alternatives --config java

For Ubuntu 10.10, the sun-java6 packages have been dropped from the Multiverse section of the Ubuntu archive. It is recommended that you use openjdk-6 instead.

If you can not switch from the proprietary Sun JDK/JRE to OpenJDK, you can install sun-java6 packages from the Canonical Partner Repository. You can configure your system to use this repository via command-line:

 sudo add-apt-repository "deb http://archive.canonical.com/ maverick partner"
 sudo apt-get update   
 sudo apt-get install sun-java6-jre sun-java6-plugin
 sudo update-alternatives --config java
share|improve this answer

Using a PPA

You can use WebUpd8 PPA (this will download the required files from Oracle and install JDK 8):

sudo apt-add-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

Are PPA's safe to add to my system and what are some “red flags” to watch out for?

Also ensure your JAVA_HOME variable has been set to:

/usr/lib/jvm/java-8-oracle

Manual install

The tar.gz provided by Oracle don't have an actual installation process. You just extract those files to a location you want and add them to your path. So the process is the following:

Download a .tar.gz from Oracle (here I will be using jdk-8u20-linux-x64.tar.gz); Extract it to somewhere;

Move the extracted folder to /usr/lib/jvm. This is not required but it is the place where Java runtime software is installed

sudo mv /path/to/jdk1.8.0_20 /usr/lib/jvm/oracle_jdk8

Create a file /etc/profile.d/oraclejdk.sh with the following content (adapt the paths to reflect the path where you stored your JDK):

    export J2SDKDIR=/usr/lib/jvm/oracle_jdk8
    export J2REDIR=/usr/lib/jvm/oracle_jdk8/jre
    export PATH=$PATH:/usr/lib/jvm/oracle_jdk8/bin:/usr/lib/jvm/oracle_jdk8/db/bin:/usr/lib/jvm/oracle_jdk8/jre/bin
    export JAVA_HOME=/usr/lib/jvm/oracle_jdk8
    export DERBY_HOME=/usr/lib/jvm/oracle_jdk8/db

Done! Those paths will only be recognized after you logout or restart, so if you want to use them right away run source /etc/profile.d/oraclejdk.sh.

share|improve this answer

You can use sudo apt-get install default-jdk terminal command to install default jdk version.

Before installing java type: sudo apt-get update terminal command and then type install terminal command. You can get more information from here.

share|improve this answer

Have a look at OpenJDK. It is the standard JVM implementation on Linux.

share|improve this answer

I recommend JavaPackage:

https://wiki.debian.org/JavaPackage

It's very simple, you just need to follow the instructions to create a .deb package from the oracle tar.gz file.

share|improve this answer

sun-java6-jdk is a virtual package provided by oracle-java8-installer or oracle-java7-installer or oracle-java7-installer.

sudo apt-get install oracle-java8-installer  

will give you sun-java6-jdk.

share|improve this answer
    
oracle-java8-installer is available only from external PPA – avtomaton Mar 4 '15 at 20:07

you can install via apt-get:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java9-installer

also via manually

su -
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee /etc/apt/sources.list.d/webupd8team-java.list
echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee -a /etc/apt/sources.list.d/webupd8team-java.list
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
apt-get update
apt-get install oracle-java9-installer
exit

after do not forget check version

java -version
share|improve this answer

If you want to install Oracle JDK, you can use this automated script that does all the work for you.

There are detailed instructions how to use it on the author's blog.

share|improve this answer

Installing Oracle's Java JDK requires you to accept the Oracle license before the installation begins. This is only required once. If for some reason you need the installation to be automated, you can run the following commands to install without user interaction, useful for an automatic script for example.

sudo add-apt-repository -y ppa:webupd8team/java
sudo apt-get update
echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections
echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections
sudo apt-get -y install oracle-java8-installer
java -version
share|improve this answer

You can use oraji. It can install/uninstall both JDK or JRE from oracle java (.tar.gz).

  1. To install run sudo oraji '/path/to/the/jdk_or_jre_archive'
  2. To uninstall run oraji -u and confirm the version number.
share|improve this answer

I had the same problem and none of the comments worked for me. Finally, I noticed that I disabled my updates. When I reactivate it, so sudo apt-get update worked correctly and the issue was solved. (update in system settings> software and updates>updates tab here I ticked two first option of important update and recommended updates).

share|improve this answer

Best is to install default java until specific java version is not required. before this execute java -version to check if Java is not already installed.

sudo apt-get update
sudo apt-get install default-jre
sudo apt-get install default-jdk

That is everything that is needed to install Java.

share|improve this answer

try this link in case you Not want to install open_jdk https://github.com/shahriyar1369/JDK_Source_installer_for_ubuntu

share|improve this answer

Execute these series of commands (Insert/Update/Install)and you are all set to go.

  1. First add the repository: sudo add-apt-repository ppa:webupd8team/java

  2. Update: sudo apt-get update

  3. Install: sudo apt-get install oracle-java7-installer

share|improve this answer

protected by Community Feb 21 '17 at 6:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.