How to Install Java for Ubuntu

Do note that the following method should work on all Ubuntu Desktops, as well as on Ubuntu-based distros like Linux Mint.
There are multiple versions and types of Java one can install on his/her computer which can confuse beginners sometimes. However, if you are not sure which Java version you have to install, you can go safe with the default variant.

How to Install Java for Ubuntu

Installing OpenJDK/JRE
For those who don’t know, OpenJDK is an open-source variant of JDK and JRE.




Simply enter the following commands one-by-one in the terminal and you should be good to go. You can open the terminal by pressing CTRL+ALT+T simultaneously.

$ sudo apt update

Before we begin with the installation, it is also necessary to check if you already have Java installed.
YOu can check the current version of Java on your system using the following command

$ java -version

If you don’t see any java installation, please process to the next steps.

Enter this command to install java:

$ sudo apt install default-jre
$ sudo apt install default-jdk

To check if Java has been installed or not, you can use the same command we mentioned earlier as well
$ java -version

To check the JDK installation, one can use the following command
$ javac -version

You have successfully installed the open-source Java variant which is provided by Ubuntu itself. However, if you are interested in getting the official one by Oracle, you can follow the steps mentioned below.

Install Java for Ubuntu

Do note that, Oracle JDK simply cannot be installed using any package manager in the terminal directly as it goes against Oracle’s licensing agreement. You will have to create an account and download from their website.

While it is a bit of complex stuff, just keep following the steps and you should be good to go

  • Visit this Installer Page
  • Select the version you are willing to install: You don’t have to download it as of now
  • Visit this Download Page and Click on the option to “JDK Download”
  • Accept the user license and click on “Download”
  • Enter the following command in your terminal and execute it

$ scp Downloads/jdk-11.0.7_linux-x64_bin.tar.gz username@your_server_ip:~

Don’t forget the replace “downloads” with your folder name where the file was downloaded, “username” with your username and “your_server_ip” with your IP address.

Once done, execute the following command

  • $ sudo apt install software-properties-common
  • Import signing keys to verify the software

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A

You should see an output something like this:

$ gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" imported
$ gpg: Total number processed: 1
$ gpg: imported: 1

  • Add repository to your list of package sources using the command mentioned below

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

  • Press Enter to continue the installation.
  • Update your package list

$ sudo apt update

  • Create the following directory and move OracleJDK here

$ sudo mkdir -p /var/cache/oracle-jdk11-installer-local/
$ sudo cp jdk-11.0.7_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/

  • You can finally install the package using the terminal and your package managers by executing this command

$ sudo apt install oracle-java11-installer-local

The installer might also ask to accept the Oracle License agreement. Simply accept it.

Also Read: Dual boot Ubuntu and Windows 10

Do note that if you have multiple Java installations on one serve or machine, you will have to configure the version to use as the default

$ sudo update-alternatives --config java

Enter the Selection Number to set your default Java installation.

Setting the JAVA_HOME Environment Variable

A lot of programs that are written using Java uses the “JAVA_HOME” environment variable to check and determine the installation locations on your machine.




In order to set the environment variable, we will have to determine the installation location of Java.

$ sudo update-alternatives --config java

From the output, you can determine your Java installation path.

Output

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-11-oracle/bin/java             1091      manual mode

Press to keep the current choice[*], or type selection number:

In this case, the installation paths are as follows:

  • OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
  • Oracle Java is located at /usr/lib/jvm/java-11-oracle/jre/bin/java.

Copy your installation path and open /etc/environment/ using vim or your preferred text editor. Nano would work completely fine too.

$ sudo vi /etc/environment

Simply add the following line tho the end of your file and don’t forget to replace “your installation path” with your actually installation path which you copied earlier.

JAVA_HOME=”/usr/lib/jvm/java-11-openjdk-amd64″

Save the file and reload it by executing the following command.

$ source /etc/environment

This will enable the JAVA_HOME path for all users on your machine.

You can also execute the mentioned code on the terminal and confirm Path installation.
echo $JAVA_HOME

So that was you can install multiple java versions and manage them. Softwares like Tomcat which specifically runs on Java will not have any issues running on your machine now.

Also Read: How to Install Chrome on Ubuntu

Leave a Comment