[java] How to install the JDK on Ubuntu Linux

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

What are the steps to install it on Ubuntu?

This question is related to java ubuntu

The answer is


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.

Installed in ubuntu 18.04

My workaround was,

$ sudo apt update

Install OpenJDK 8:

$ sudo apt install openjdk-8-jdk

Verify the Java installation by running the following command which will print the Java version:

$ java -version

The output should look like this:

Output:

openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

Over Debian you can try

apt-get install default-jdk

Note: WebUpd8 team's PPA has been discontinued with effective from April 16, 2019. Thus this PPA doesn't have any Java files. More information can be found on PPA's page on Launchpad. Hence the below method no longer works and exists because of historical reasons.

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

Using a PPA (Obsolete)

Note: WebUpd8 team's PPA has been discontinued with effective from April 16, 2019. Thus this PPA doesn't have any Java files. More information can be found on PPA's page on Launchpad. Hence the below method no longer works and exists because of historical reasons.

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.


You can use SDKMan,

curl -s "https://get.sdkman.io" | bash
source "~/.sdkman/bin/sdkman-init.sh"
sdk install java

The following used to work before the Oracle Java license changes in early 2019.

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

The PPA is discontinued, until the author finds a workaround for the license issues.


Try to use SDKMAN! package manager - https://sdkman.io/install Now it's the easiest way to install many JVM-based SDKs, tools and frameworks on any Linux distribution.

Upon installation, run the following command to see all available Java distributions:

sdk list java

Select the distribution that you need and install it:

sdk install java <version>

The best is to install default Java until a 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.


I have successfully installed JDK 10 on Ubuntu 18.04 LTS following this video.

I am copying the excerpt from the description of the video.

Just open the terminal and give these commands :

For Java Installation (PPA)

sudo add-apt-repository ppa:linuxuprising/java
sudo apt-get update
sudo apt-get install oracle-java10-installer

For setting up environment variables (make java10 default)

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

The same procedure can be followed on Ubuntu 16.04, Linux Mint, Debian and other related Linux systems to install JDK 10.


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.


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

If you would like to use the AdoptOpenJDK distribution other than Java 10, you can use their official repository as described on the AdoptOpenJDK website (also applies for Debian):

  1. Import the official AdoptOpenJDK GPG key by running the following command:

    wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -
    
  2. Import the AdoptOpenJDK DEB repository by running the following command:

    sudo add-apt-repository --yes https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/
    

    If you get a command not found error, try running:

    apt-get install -y software-properties-common
    

    Then repeat the first command.

  3. Refresh your package list with apt-get update and then install your chosen AdoptOpenJDK package. For example, to install OpenJDK 8 with the HotSpot VM, run:

    apt-get install <adoptopenjdk-8-hotspot>
    

You can find the available packange names / Java versions under https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/pool/main/a/


Execute these series of commands (insert, update, and 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
    

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

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


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


I recommend JavaPackage.

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


Note: WebUpd8 team's PPA has been discontinued with effective from April 16, 2019. Thus this PPA doesn't have any Java files. More information can be found on PPA's page on Launchpad. Hence the below method no longer works and exists because of historical reasons.

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 the above steps, verify the installed version using the 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 the 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


Try this in case you do not want to install OpenJDK: JDK Source Installer for Ubuntu


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).


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

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.


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/


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 the default method (repository without external PPAs) in Ubuntu 12.10+ for Oracle's JDK - I will try to describe it.

  • Install JavaPackage: sudo apt-get install java-package
  • Download Oracle JDK from Oracle downloads page
  • Make a Debian package from the downloaded .tar.gz archive: make-jpkg jdk-YOUR_VERSION-linux-PLATFORM.tar.gz This command will produce a .deb package.
  • Install the package in your favourite way (for example, sudo dpkg -i oracle-java8-jdk_8u40_amd64.deb)

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


You can install via apt-get:

sudo add-apt-repository ppa:linuxuprising/java
sudo apt-get update
sudo apt-get install oracle-java11-installer

After, do not forget to check the version:

java -version

  1. Install the appropriate version of OpenJDK

    JAVA_VERSION=8 sudo add-apt-repository -y ppa:openjdk-r/ppa sudo apt-get update sudo apt-get -qq install -y openjdk-$JAVA_VERSION-jdk

  2. Set Environment Variables in /etc/profile.d/jdk.sh

    JAVA_HOME=/usr/lib/jvm/java-$JAVA_VERSION-openjdk-amd64 echo "export JAVA_HOME=$JAVA_HOME" | sudo tee -a /etc/profile.d/jdk.sh echo "export J2SDKDIR=$JAVA_HOME" | sudo tee -a /etc/profile.d/jdk.sh echo "export J2REDIR=$JAVA_HOME/jre" | sudo tee -a /etc/profile.d/jdk.sh echo "export PATH=$PATH:$JAVA_HOME/bin:$J2REDIR/bin" | sudo tee -a /etc/profile.d/jdk.sh

  3. Check your installation

    /bin/bash /etc/profile.d/jdk.sh java -version echo $JAVA_HOME echo $J2REDIR echo $PATH


In Ubuntu 18.04, We can install java like a normal package without using an external repository

Just run this command

sudo apt install openjdk-8-jdk

In Ubuntu1604 I faced "No installation candidate error". Following below steps helped me install.

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


I just did this on an Ubuntu virtual machine. Here's how I did it.

Ubuntu comes with the Java JRE installed, but not the JDK. Java -version gives the JRE, not the JDK. Run javac -version. If the JDK isn't installed, you will get an error. Run the following command in the terminal to get the JDK: sudo apt install default-jdk. Now, run which javac. The output should be /usr/bin/javac. It has now been installed. IntelliJ won't let us use this path because it is the binary of javac. Go to Project Structure > Project Settings > Project. Where it says 'Project SDK', add a JDK. Then go to /usr/lib/jvm. Now you should be able to compile java projects in IntellIJ.

You may have to edit the configurations so that IntelliJ knows where to look for your main class. Go to Run > Edit Configurations. Add your main class for the Main class and add the root of your project as the Working directory.


Simply run:

sudo apt-get install default-jdk

Please follow this step to install oracle JDK

  1. Download oracle JDK (Eg Java 8 for 64 bit machine jdk-8u241-linux-x64.tar.gz ) from Oracle
  2. Extract the *.tar.gz in home directory or preferred location
  3. Set Environment variable Eg Open terminal type sudo gedit ~/.bashrc Add following line at end of .bashrc file

    export JAVA_HOME=/opt/jdk1.8.0_241 #add your own jdk location

    export PATH=$PATH:$JAVA_HOME/bin

  4. Save and exit
  5. Finally source the .bashrc Eg source ~/.bashrc
  6. Now open a new terminal type jdk -version

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.