Let’s see how to install java in Linux in just 3 steps. For java Development, you need a JDK and not JRE. There’s a huge difference between JRE and JDK. So when I say installing java in this tutorial, I mean installing JDK.
Installing java in Linux is quite simple. Follow the below steps:
Installing Java on Ubuntu, Mint, or other Debian based Distros
Usually, all distro comes with one default JDK set into their PPA hence all you need to do is just fire a single command and you will be good to go. But, before installing java first ensure whether you have on already.
If it doesn’t return anything then you are good to go otherwise you already have java.
But in case it doesn’t return anything then open the terminal and fire the below command
sudo apt-get install default-jdk
Sudo is required since the installation process requires the root’s permission.
To ensure whether java is installed or not, you fire below command
It should return back path to your java file like below
Or you can also ensure by running the below command
It should return the version of java installed.
Installing Java on Red Hat or Fedora
Like the above steps first, ensure whether you already have java. Run below command, if command doesn’t return anything then you don’t have java on your machine otherwise proceed from the next step
If it doesn’t return anything then fire below command
sudo yum install java-1.8.0-openjdk
This should install java 8. In order to install java 11 on RedHat or fedora use below command
sudo yum install java-11-openjdk-devel
After installation is done, ensure installation by checking version of java using below command
Manually Installing java in any Linux distro
There could be an instance when the above-written way doesn’t work for some distros or may be just because you wanna have more command over your machine and you want to install java in your own way, then you’ll prefer the manual way.
Since many vendors provide JDK, first you’ll have to decide which one to use. I personally prefer Adoptopen JDK. Download the JDK,l whichever version you want and proceed to next step.
In Linux, we are supposed to install custom applications in /opt path. So we will also install java to this path.
use the below command to extract the tar file you just downloaded. you may also use the GUI
tar -xvf <path to java.tar.gz> -C /opt
The above command will create a directory in /opt directory which contains your JDK.
The next step is we need to set the JAVA_HOME variable.
We will do it via the .profile file. In case you don’t know what is .profile file in Linux, this file contains a shell script, which runs every time start a session in Linux (eg: when you open a terminal session, or simply login into your Linux machine)
So let’s say the installation directory of java is /opt/java-11, then at the very bottom of “.profile” add below line
JAVA_HOME variable is used by the various application which usage java.
The next step is adding java into the PATH vaariable
In linux there’s A common practice to create a soft link of all application file in /usr/bin, so we will also do it.
To create softlink to java fire below command
ln -s /opt/java-11 /usr/bin/java
If the above command fails with permission issue the use sudo before the command
Now let’s add this path to the PATH variable.
open .profile again and add below line at very bottom
Java is talled. To verify, start a new terminal and fire below command
It should return the version which you had chosen to install.
In case you face any problem please leave a comment or mail on email@example.com
If you are already very comfortable in java and wanna proceed to an advanced level, Here’s my course on REST API using Spring boot