installation

Install Java on Ubuntu Tutorial

In this tutorial, we will show you how to install the latest version of Java in Ubuntu 12.04 or later. An internet connection is required in order to download the corresponding Java packages.

There are different versions of Java that can be installed in Ubuntu. First of all, the OpenJDK project is an open-source implementation of the Java SE Platform. Second, Oracle Java 7 is Oracle’s own version of Java 7.0. Finally, you can download the latest version of Java by using the corresponding installer.

install java ubuntu

This tutorial applies to Ubuntu 12.04 and later for both Java 6.0 and Java 7.0.

1. OpenJDK

1.1 Java 6.0

In order to install the Java Runtime Environment (JRE), we must issue the following command:

1
sudo apt-get install openjdk-6-jre

Also, we need to install the IcedTea Web plugin, in order for Java applets to be executed, using the command:

1
sudo apt-get install icedtea6-plugin

Finally, in order to write and develop Java programs, we need the Java Development Kit (JDK). We can install it by executing the command:

1
sudo apt-get install openjdk-6-jdk

Notice that the installation of JDK includes the installation of JRE.

1.2 Java 7.0

In order to install the Java Runtime Environment (JRE), we must issue the following command:

1
sudo apt-get install openjdk-7-jre

Also, we need to install the IcedTea Web plugin, in order for Java applets to be executed, using the command:

1
sudo apt-get install icedtea7-plugin

Finally, in order to write and develop Java programs, we need the Java Development Kit (JDK). We can install it by executing the command:

1
sudo apt-get install openjdk-7-jdk

Notice that the installation of JDK includes the installation of JRE.

1.3 Java 8.0

If you want to install Java version 8 JDK edition you can open a terminal and execute through a Launchpad PPA:

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jdk  

If you only want to install the Java Runtime Enviroment:

 sudo apt-get install openjdk-8-jre  

2. Oracle Java

First of all, we must remove any existing versions of OpenJDK/JRE. In order to achieve that, open a terminal and execute the following command:

1
sudo apt-get purge openjdk-\*

This command shall remove any installed package that belongs to OpenJDK/JRE. Then, we create a directory in our local machine that will store the Oracle Java:

1
sudo mkdir -p /usr/local/java

Once we complete these two steps, we must visit the Oracle’s official download page here and download the appropriate Java packages for our system. You can download both Java SE Development Kit 7 and Java SE Runtime Environment 7, but let me remind you that the Java Development Kit (JDK) also includes a Java Runtime Environment (JRE).

Assuming that the downloaded file is placed inside the Downloads folder and that the local architecture is 32-bit, we execute the following commands:

1
2
3
cd ~/Downloads
sudo mv jdk-7u45-linux-i586.tar.gz /usr/local/java
cd /usr/local/java

These commands move the downloaded file inside the newly-created directory. In case your system architecture is 64-bit change the aforementioned commands appropriately.

Then, we extract the installation files from the archive:

1
sudo tar xvzf jdk-7u45-linux-x64.tar.gz

Once you have finished the aforementioned commands, feel free to remove the archive, in order to save some free space:

1
sudo rm jdk-7u45-linux-x64.tar.gz

The next step is to create the corresponding system variables. Execute the following command in order to open Ubuntu default editor

1
sudo gedit /etc/profile

and copy-paste the following lines at the end of the file:

1
2
3
4
5
6
7
8
JAVA_HOME=/usr/local/java/jdk1.7.0_45
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
JRE_HOME=/usr/local/java/jdk1.7.0_45/jre
PATH=$PATH:$HOME/bin:$JRE_HOME/bin
 
export JAVA_HOME
export JRE_HOME
export PATH

Save the /etc/profile file and exit. The final step is to inform the system about the installation location of Java. In order to achieve that, we execute the following commands:

1
2
3
4
5
6
7
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.7.0_45/jre/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.7.0_45/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jre1.7.0_45/bin/javaws" 1
 
sudo update-alternatives --set java /usr/local/java/jdk1.7.0_45/jre/bin/java
sudo update-alternatives --set javac /usr/local/java/jdk1.7.0_45/bin/javac
sudo update-alternatives --set javaws /usr/local/java/jdk1.7.0_45/bin/javaws

Once all the aforementioned commands have been successfully executed, we reload the /etc/profile file, in order for our changes to take place:

1
. /etc/profile

Now, we can verify our installed version of Java as shown in the next section.

3. Oracle Java Installer

3.1 Oracle Java 7.0

A very quick and simple way to download and install the latest version of Java in Ubuntu is running the following commands in a terminal:

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

The first command adds a new entry to your repository, which contains the latest version of Java (currently 7u51). Then, the last command downloads and executes the Oracle’s Java installer. The newly added ppa provides the full Oracle Java Development Kit (JDK) 7 package.

3.2 Oracle Java 9.0 from PPA

First, you need to press the key Ctrl+Alt+T to open the terminal. Then execute the below commands:

  • Add the Webupd8 PPA:
 sudo add-apt-repository -y ppa:webupd8team/java 
  • Install the installer scripts:
sudo apt-get update

sudo apt-get install oracle-java9-installer
  • Set the enviroment variables:
 sudo apt-get install oracle-java9-set-default 

Last but not least we follow the commands at section 4.

3.3 Oracle Java 10.0 from PPA

First, you need to press the key Ctrl+Alt+T to open the terminal. Then execute the below commands:

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

Then set as default with:

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

Last but not least we follow the commands at section 4.

4. Verify the installed version of Java

In order to verify which version of Java is installed in your system, open a terminal (Ctrl + Shift + T) and issue the following command:

1
java –version

A sample output is shown below:

java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

5. Multiple Java versions

In case you have installed multiple Java versions, you can choose the default Java version of your system, using the command:

1
sudo update-alternatives --config java

6. Install Java on Ubuntu – Summary

This was a tutorial on how to install the latest version of Java in Ubuntu 12.04 or later.

You can also check this article about how to check Java version in Windows, Linux, macOS.

Last updated on May 6th, 2020

Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button