In this session we’ll show you step-by-step instructions on how to install Java on Ubuntu .These instructions will work for Ubuntu 16.04,18.04, and any other LTS release.They should work on both servers and desktops.Here We are installing java jdk version 8 for this tutorial.
There are multiple ways of installing Java
1.Installing the latest Java using the default (Ubuntu’s) JDK using apt-get
2.Installing Java manually using Oracle’s JDK
Here We will explain the 2nd method as most developers needed custom version of oracle JDK. On upcoming posts will explain the other one
Before we begin installing Java:
Some things you’ll need before we start installing Java:
- An Ubuntu desktop/server
- A sudo/root user.If you’re using a non-root user then you’ll need to append ‘sudo’ to most commands.
- Make sure you don’t have Java already installed. If you do, remove it, or if you want to use multiple Java versions/installs, then keep it and configure them later
Step 1: Update the packages
As always update the package
sudo apt-get update
Step 2: Download Oracle JDK 8 & Extract
Go to Oracle’s downloads page and select the version you want to download. We’ll use Java’s 1.8.0 version for the purpose of this tutorial.Replace the version numbers if you’re going to use a different version.
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Update 2019: Now that you need an Oracle.com account to download the file below, you’ll have to manually download the file and continue the process after the next step.
Here you can select the java version depending on your linux architecture whether 32 bit or 64 bit.For Ubuntu/Fedora we need to select the tar.gz file.
After downloading the file we need to extract it as below.It is best practice to move the downloaded file into a common location.
Create a directory for your Java installation:
This is where we’ll install Java. You can use a different directory if you want to install it elsewhere like /usr/local or in /opt.Here I am installing on softwares folder fort his tutorial.
Now extract the .tar.gz (tarball) file to the directory you previously created using the command:
sudo tar xvzf jdk-8u231-linux-x64.tar.gz
I am copying this file in my software folder (/home/rajesh/Documents/softwares).From that location open the terminal and run below command to unpack our java zip file
Setting up Java on Ubuntu
Step 3: Install java & javac on the system
Now we need to install java and javac. For that run the below commands
sudo update-alternatives --install "/usr/bin/java" "java" /home/ubuntu/Documents/jdk1.8.0_201/jre/bin/java 1
sudo update-alternatives --install "/usr/bin/javac" "javac" /home/ubuntu/Documents/jdk1.8.0_201/bin/javac 1
Please note the location where the java zip was extracted.By this command let Ubuntu know where our JDK/JRE is located.
Step 4 :Set a default if you have multiple Java installations
Now we have to inform Ubuntu that our Java Installation ie jdk1.8.0_231 must be the default java.So run below command.
sudo update-alternatives --config java sudo update-alternatives --config javac
You can also use this command to check if you have multiple installations.
You’ll get an output with a list of installed Javas. Press enter to keep the default one without any changes or enter a number to select a different default Java.
We are getting this response because we have only one java is installed. First, make sure you’ve installed Java on your system and check what version you have using below command :
java -version
It means our installation completed properly.Next we need to set java path.For that we have many options.
Step 5:Configure environment variable for java
You’ll most likely need to set the JAVA_HOME variable so other applications can find the location of your Java installation. To find the Java installation path, run the previous command again:
update-alternatives --config java
will get path as
/home/rajesh/Documents/softwares/jdk1.8.0 231
There are multiple ways of setting up the JAVA_HOME ,PATH variable, and multiple other variables that you may need to set, depending on what you need and what you’re going to use.
To set these environment variables permanently
open the file .bashrc or .bashprofile using any file editor like vim or nano
nano .bashrc (do it from the home directory itself)
Add the below contents to it & save .
export JAVA_HOME=/home/rajesh/Documents/softwares/jdk1.8.0 231 export PATH=${PATH}:${JAVA_HOME}/bin
Now load the environment variables in current shell using following command.
source .bashrc
We can verify the path & java home using echo $JAVA_HOME and echo $PATH
Will get output respectively depends on the java path
/home/rajesh/Documents/softwares/jdk1.8.0_231/jre/bin/java
/home/rajesh/bin:/home/rajesh/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/rajesh/Documents/softwares/jdk1.8.0_231/jre/bin/java/bin
Now we have successfully installed and configured java in our system for the developers.
Cool 😎. Nice tutorial 👏
Thanks
Nice Bro.
Thanks
Nice.. !!
Thanks
Nice work.
Thanks