Beginner’s Guide: How to Install Java on Ubuntu in Easy Steps

install-java-on-ubuntu-guide

Some programs/tools/utility on Ubuntu required java/JVM, without java these programs are not working. Are you facing the same problem? Don’t worry!

Today I am going to cover in this article how to install Java on Ubuntu. This article will cover the complete tutorial step by step.

You can get java on Ubuntu via three packages JRE, OpenJDK and Oracle JDK.

Java and Java’s Virtual Machine (JVM) are widely used and required to run much software.

CommandsDescription/Usage
java –versionCheck the version of installed java
sudo apt-get updateUpdate repository
sudo apt-get install default-jreInstall default-jre
sudo apt-get install default-jdkInstall Default-jdk
sudo apt-get remove default-jdk/jreUninstall / Remove default-jdk and jre
Command Table how to install java on Ubuntu 20.04

Check if is already installed on Ubuntu

It is necessary to check if java is already installed, so run the following command and check it.

If the java is not installed on your system you will get the following result, and if java installed you will get some different output.

vijay@Ubuntu-19:~$java --version

Command 'java' not found, but can be installed with:

sudo apt install openjdk-11-jre-headless  # version 11.0.3+7-1ubuntu1, or
sudo apt install default-jre              # version 2:1.11-71
sudo apt install openjdk-12-jre-headless  # version 12.0.1+12-1
sudo apt install openjdk-13-jre-headless  # version 13~13-0ubunt1
sudo apt install openjdk-8-jre-headless   # version 8u212-b01-1
vijay@Ubuntu-19:~$

Prerequisites to install java on Ubuntu

What is the requirements to install java on Ubuntu, Of course, you should know otherwise you will get the error or face the problems?

If you know the prerequisites, you can install any software without any problem. The following are prerequisites for installing java.

  • You have installed Ubuntu (It should not be live boot).
  • Login user must be Sudo/root user
  • Active internet connection on System.

Install Default JRE/JDK

apt-get is a utility used to install, update, remove packages on Ubuntu. This is the easiest way to install java on Ubuntu, open terminal run command, sit back on the chair, have a coffee, and java or other packages will be install automatically.

Before going to install java on Ubuntu you must update the packages list used by apt-get utility.

To update package list use following command

$sudo apt-get update

vijay@Ubuntu-19:~$sudo apt-get update 
Hit:1 http://security.ubuntu.com/ubuntu disco-security InRelease               
Hit:2 http://in.archive.ubuntu.com/ubuntu disco InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu disco-updates InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu disco-backports InRelease
Reading package lists... Done
vijay@Ubuntu-19:~$

Next step to install default-jre after completing package list updation.

$sudo apt-get install default-jre

vijay@Ubuntu-19:~$sudo apt-get install default-jre
vijay@Ubuntu-19:~$java --version
openjdk 11.0.3 2019-04-16
OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu1, mixed mode, sharing)
vijay@Ubuntu-19:~$

Next step install default JDK or OpenJDK on the system.

The JDK is usually only needed if you are going to compile Java programs or if the software that will use Java specifically requires it.

The JDK does contain the JRE, so there are no disadvantages if you install the JDK instead of the JRE, except for the larger file size.

Go through the following command to install JDK

$sudo apt-get install default-jdk

vijay@Ubuntu-19:~$sudo apt-get install default-jdk
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  default-jdk-headless libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-11-jdk
  openjdk-11-jdk-headless x11proto-core-dev x11proto-dev xorg-sgml-doctools xtrans-dev
Suggested packages:
  libice-doc libsm-doc libx11-doc libxcb-doc libxt-doc openjdk-11-demo openjdk-11-source visualvm
The following NEW packages will be installed:
  default-jdk default-jdk-headless libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxt-dev
  openjdk-11-jdk openjdk-11-jdk-headless x11proto-core-dev x11proto-dev xorg-sgml-doctools xtrans-dev
0 upgraded, 16 newly installed, 0 to remove and 8 not upgraded.
Need to get 210 MB of archives.
After this operation, 226 MB of additional disk space will be used.
Do you want to continue? [Y/n] 

Install the Oracle JDK on Ubuntu

To install Oracle JDK java version 11 and 12, you must add a new repository by using the following command

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

vijay@Ubuntu-19:~$vijay@Ubuntu-19:~$ sudo add-apt-repository ppa:linuxuprising/java

When you make entry of the repository server in the repository list, you must update again repository list.

vijay@Ubuntu-19:~$ sudo apt-get update 
Hit:1 http://ppa.launchpad.net/linuxuprising/java/ubuntu disco InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu disco InRelease                                      
Get:3 http://security.ubuntu.com/ubuntu disco-security InRelease [97.5 kB]                     
Get:4 http://in.archive.ubuntu.com/ubuntu disco-updates InRelease [97.5 kB]                                
Hit:5 http://ppa.launchpad.net/webupd8team/java/ubuntu disco InRelease                                     
Get:6 http://in.archive.ubuntu.com/ubuntu disco-backports InRelease [88.8 kB]              
Fetched 284 kB in 3s (112 kB/s)     
Reading package lists... Done
vijay@Ubuntu-19:~$

Now use following command to install oracle JDK 12 on Ubuntu 19.04 Server, using following command

$sudo apt install oracle-java12-installer

vijay@Ubuntu-19:~$sudo apt install oracle-java12-installer
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  gsfonts-x11 oracle-java12-set-default
Suggested packages:
  binfmt-support visualvm ttf-baekmuk | ttf-unfonts | ttf-unfonts-core ttf-kochi-gothic | ttf-sazanami-gothic ttf-kochi-mincho
  | ttf-sazanami-mincho ttf-arphic-uming
The following NEW packages will be installed:
  gsfonts-x11 oracle-java12-installer oracle-java12-set-default
0 upgraded, 3 newly installed, 0 to remove and 8 not upgraded.
Need to get 43.6 kB of archives.
After this operation, 236 kB of additional disk space will be used.
Do you want to continue? [Y/n] 
vijay@Ubuntu-19:~$
vijay@Ubuntu-19:~$javac -version 
javac 12.0.1
vijay@Ubuntu-19:~$

Managing on Ubuntu

You have installed java on your system, later newer version of java will launched. Now you think to install new version. No, problem at all, you can install new version.

But if you want your system use newer java version, you will have to configure. Following command is useful

$sudo update-alternatives –config java

You will see all installed package on your system, select number and hit enter.

vijay@Ubuntu-19:~$sudo update-alternatives --config java
[sudo] password for vijay: 
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode
* 2            /usr/lib/jvm/java-12-oracle/bin/java          1091      manual mode

Press  to keep the current choice[*], or type selection number: 
vijay@Ubuntu-19:~$

Uninstall Oracle Java12

To remove oracle java12, run command to remove the installer script. You can use apt-get remove to uninstall program from the Ubuntu 19.04 desktop/server.

vijay@Ubuntu-19:~$sudo apt-get remove oracle-java12-installer
vijay@Ubuntu-19:~$

How to install java on Ubuntu 20.04 Video Tutorial in Hindi

Conclusion

By now you have learnt how to install java on ubuntu. Now you can install default jre and jdk, and oracle java. Also you can install multiple version of java.

So If you have any question, write in commentbox now. you can write me vijay@cyberpratibha.com

Cheers!

If you like our content, please consider buying us a coffee.
Thank you for your support!

5 thoughts on “Beginner’s Guide: How to Install Java on Ubuntu in Easy Steps

  1. If you want to install oracle java 8 or other older versions then download the tar.gz file from Oracle and follow this:
    copy the install-java.sh skript from the github project
    https://github.com/chrishantha/install-java

    and then use command:
    sudo ./install-java.sh -f ~/Downloads/jdk-8u231-linux-x64.tar.gz -p /usr/lib/jvm/

    1. java12 is no longer available. you have to use:

      sudo apt -y install oracle-java14-set-default

      that one is now available on the uprising

Leave a Reply

Your email address will not be published. Required fields are marked *