Quickly Install Java for Linux on Ubuntu and Linux Mint
LinuxBytes: In this article, we will teach you how to install Java for Linux Distributions like Ubuntu and other Ubuntu Based Linux distros like Linux Mint and Elementary OS. Learn the quick way to install Java on Ubuntu.
Java is one of the oldest and most popular programming languages in the world. With its ability to run on multiple platforms, Java runs on nearly 3 Billion devices. The worlds most popular mobile operating system, Android is dependent on Java.
Java is created to work on multiple platforms by avoiding dependencies. It is like “write once, run anywhere.”
Java powers many mobile applications and is the preferred choice for many Desktop, Web and Server applications as it is stable and robust. It also works efficiently on multiple platforms like Windows, Mac, Linux and Android.
In fact, installing Java is one of the first things to do after installing any operating system, as many applications require Java for proper functioning.
In this quick guide, we will see how to install Java on Linux distributions like Ubuntu, Debian and their derivatives. Installing Java on Ubuntu is relatively easy as it only requires running few Linux commands in Linux Terminal. Here below we have compiled the easiest way to install Java on Ubuntu.
Understanding the Java Versions
Before proceeding to install Java on Ubuntu it is better to know about the different forms of Java. Let us quickly explain you the difference between JRE, OpenJDK and Oracle JDK.
Java Runtime Environment shortly referred to as JRE is a Java Environment that is essential to run Java-based applications on your machine. This is helpful to run Java programs on Linux.
Java Development Kit or JDK is the Java environment required to develop Java based applications. It is essential for programmers and developers who create Java based applications. There are two versions of JDK. Open JDK and Oracle JDK.
OpenJDK is the open source implementation of Java Development Kit and is free to use without any licencing issues. While the Oracle JDK is the official version of Oracle which may contain some proprietary code.
OpenJDK will be handy in most cases and some programs such as Android Studio suggests using Oracle JDK to avoid UI/performance issue.
Note: OpenJDK and Oracle Java are not identical. There may be licensing, performance, and stability differences, and this should be considered carefully when developing production applications.
With this knowledge regarding Java, you should be able to make a decision about choosing the Java version that suits your requirements.
Once you decide, install the required Java packages using the below commands.
If you are still confused in choosing the Java implementation, go with the Open JDK version of Java.
Before proceeding to install Java on Linux, make sure you are logged in as a user with sudo privileges. Also, check if Java is installed on your machine by default. To do that just run
java -version in Linux terminal.
openjdk version “11.0.2” 2019-05-18
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-1ubuntu0.18.04.3)
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-1ubuntu0.18.04.4, mixed mode)
If you see the above output it means that Java 10 is installed on your system via OpenJDK. If not Java is not available on your machine and you should proceed with further installation steps.
Install Java on Ubuntu Linux!
The following commands will work on machines running the latest version, Ubuntu 18.04 Bionic Beaver, as well as 16.04, and any other Long Time Support (LTS) releases.
Java is usually distributed in three editions, namely Standard Edition (SE), Micro (ME) and Enterprise (EE). For this guide, we’ll use Java SE 11 which is the latest and Long Term Support version of Java as of writing this post.
Installing the OpenJDK (Java 11)
Follow the below steps to install OpenJDK version of Java on Ubuntu
01. Update your system and package index by running
sudo apt-get update && sudo apt-get upgrade
02. After the update process is finished run the below command to install the default Java OpenJDK
sudo apt install default-jdk
03. The above command will install the latest version of Java OpenJDK on Ubuntu. You can verify the installation by running
java -version command in Linux terminal.
Note: JRE is included in the JDK package. If you only looking to install JRE, install the default-jre package by just replacing jdk with jre in the above command.
Installing OpenJDK 8
While Java 11 is the latest version of Java, most Java applications still depend on Java 8. If you want to install Java JDK 8 here are the commands to run:
sudo apt update && sudo apt install openjdk-8-jdk
You can also install Java 7 or Java 6 etc, just use openjdk-7-jdk, openjdk-6-jdk.
Installing OracleJDK 11 on Ubuntu
If you are developing applications for Android on Ubuntu using Android Studio then you might need to install Oracle JDK on your Ubuntu system. So here is how you can install the latest version of Oracle Java Development Kit.
Before proceeding to install Oracle Java on Linux make sure you are aware of the Oracle Java license as the Oracle Java permits free usage only for non-commercial and development use.
Steps to install Oracle Java:
To install the latest version of Oracle Java you need to add the Linux Uprising PPA and procced with the below steps to install Oracle Java 11 on Ubuntu 18.04
01. Add the required dependencies by running below command
sudo apt-get install software-properties-common
02. Add the Linux Uprising PPA by running the commands
sudo add-apt-repository ppa:linuxuprising/java
03. Once the repository is added, update the packages list and install the Oracle Java 11 by running the command
sudo apt update && sudo apt install oracle-java11-installer
This will start the installation and will prompt you to accept accept Java licence during installation. Accepting the licence will start the installation and java will be installed on your system.
You can verify the installation by running
java -version command in Linux terminal.
Installing Other version of Oracle Java
Java 11 is the latest version of Java if you want to install other version of Java on Ubuntu, here is how you can do it.
If you want to install Java 10 and set it as default, use the following command:
sudo apt install oracle-java10-installer
sudo apt install oracle-java10-set-default
If you want to install other versions of Java like Java8, Java7 just replace 10 with the version number you wish to install.
Managing Java for Linux!
Well now that you have Java Installed on your system, it is time to learn few configuration options that will help you to manage Java on Ubuntu.
Setting Default Version
java -version command will give you the current version of Java that is active by default. But if you have multiple versions of Java installed on your system or server then you can set the version you need as default.
To do that just make the following changes by running the below commands.
Now select the Java version you want to set as default and press Enter.
sudo update-alternatives –config java
Set the JAVA_HOME Environment Variable
The JAVA_HOME environment variable is required for some applications to determine the Java Installation location
To set the JAVA_HOME environment variable, use the update-alternatives command to find where Java is installed.
sudo update-alternatives –config java
In our case the installation paths are as follows:
OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java
OpenJDK 8 is located at /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Copy the installation path of your preferred installation. Next, open the /etc/environment file:
sudo nano /etc/environment
Now add it in the file like the one in below and save it.
JAVA_HOME environment variable can be checked by running the command
echo $JAVA_HOME in terminal. You should see the correct path where Java is installed.
Uninstalling Java on Ubuntu
If you want to remove Java for any reason you can simply do it by running the remove command
sudo apt remove openjdk-10-jdk
This command will quickly remove the OpenJDK version of Java from your system. And to remove Oracle Java version, run below commands
sudo apt-get purge oracle-java8-installer
sudo apt-get autoremove
We hope you like this detailed and informative guide on Installing Java on Ubuntu and quick ways to manage it on your system or server.
Java is a must of you are a developer as many server-side applications require Java. Also, several system applications like VirtualBox and development tools require Java.
So this is how you can install Java in Ubuntu and other Ubuntu based distributions.
If you like our detailed guide on installing Java On Ubuntu share it with your friends on social media.
Meanwhile, if you face any issues during installation reach us through comments and we will assist you.