Table of Contents
- 1. Overview
- 2. Repository installation
- 3. Extracting the Java JDK files and manual installation
- Step 1: Download Oracle java
- Step 2: Create directory to save Java installation files
- Step 3: Extract tar file using tar command
- Step 4: Move folder using mv command
- Step 5: Set permission using chmod command
- Step 6: Set JAVA_HOME environment variable using bashrc file
- Step 7: run/apply bashrc file updates from command line
- Step 8: Verify Java installation using java and javac commands
In this tutorial, I am going to explain how to install Java in Ubuntu Linux environment.
The primary goal of this article is to learn java installation in Linux, Java installation in most of the Linux flavors are similar, few steps can be modified according to the OS flavor in which you are installing java.
There are basically 2 ways to install Java in Linux.
2. Repository installation
Step 1: Add Oracle Java repository and install Java 8 using installer
In this type of installation you have to configure respective repository and then you have to execute commands:
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer
Step 2: Managing Java
It is also possible to have multiple Java installations in the machine, So in that case, we need to configure default java version which should be used while running java programs from the command line.
We have to execute a command to configure alternatives:
$ sudo update-alternatives --config java
The output of the above command will look like,
Selection Path Priority Status --------------------------------------------------------------------------------- *0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 auto mode 1 /usr/lib/jvm/java-6-oracle/jre/bin/java 1 manual mode 2 /usr/lib/jvm/java-7-oracle/jre/bin/java 2 manual mode Press <enter> to keep the current choice[*], or type selection number:
Now we have to select the appropriate Java version which we want to set as default, Similarly we can also update the javac command alternatives using command:
$ sudo update-alternatives --config javac
In this way, we can also update other java commands like
The generic command for updating alternatives will look like:
$ sudo update-alternatives --config <command>
Step 3: Set JAVA_HOME environment variable
Now we have to set
JAVA_HOME so that other programs which are using Java, can find the java. In order to set
JAVA_HOME, we have to find out the location at which java is installed.
Now copy the
JAVA installation path and open /etc/environment file using any editor,
I have used
gedit for the opening the file.
$ sudo gedit /etc/environment
Now, append the following line, please remember to use your java installation path.
After updating the file my /etc/environment looks like:
Step 4: Verify java installation
Now, we have to verify the JAVA_HOME path and installation using commands:
To verify JAVA_HOME, I have used below command from terminal,
$ echo $JAVA_HOME
To verify the javac & java, I have used commands:
$ javac -version
$ java -version
If the above commands work properly then we have configured the Java installation in Linux properly.
3. Extracting the Java JDK files and manual installation
Step 1: Download Oracle java
This is the second method of installing java in Linux, In this method, we have to manage all the things manually.
This method is appropriate for easy update and changes java versions.
In order to install Java manually, we have to download the Java JDK-8 files, I have downloaded a file
jdk-8u45-linux-x64.tar.gzfrom Oracle’s official website.
Similarly, you can download the respective files as per your OS(32/64 bits) from link,
I have downloaded the jdk8 tar file(
jdk-8u45-linux-x64.tar.gz) and placed it at my Desktop location.
Once the file is downloaded, we need to perform following steps.
Create a folder in the Linux system, where we are going to save the Java files.
In order to save the java files, appropriate location is
But the JVM directory doesn’t exist by default in Linux system, so we have to create the JVM directory under location
Step 2: Create directory to save Java installation files
To create the directory, we have to open the terminal and we have executed commands as superuser, so we will execute commands by prepending sudo.
I have used following command to create a directory:
$ sudo mkdir /usr/lib/jvm
Step 3: Extract tar file using tar command
Once the directory is created, we have to extract the tar file that we have downloaded and move the content of the folder to
Extract the tar file using the command:
$ tar -xvf jdk-8u45-linux-x64.tar.gz
Step 4: Move folder using mv command
Once the tar file is extracted, we need to move the extracted folder, I have used following command to move the folder data:
$ sudo mv jdk1.8.0_45 /usr/lib/jvm/
Step 5: Set permission using chmod command
Now we have to set the permission for the jvm folder,
I have used below command to set the permission on the
$ sudo chmod -R 777 /usr/lib/jvm/jdk1.8.0_45
Step 6: Set JAVA_HOME environment variable using bashrc file
Once the permission is set, we have to set the
PATH variable in the
I have used gedit editor for updating the
.bashrc file, you can use your favorite editor to update the file I have used below command to open the
Append the below content at the end of file.
Once the modifications are completed in
.bashrc file, the file looks like:
Step 7: run/apply bashrc file updates from command line
.bashrc file is updated, we have to run the below command.
$ . ~/.bashrc
Step 8: Verify Java installation using java and javac commands
Now, we have to verify the
JAVA_HOME path and installation using commands:
JAVA_HOME I have used below command from terminal,
$ echo $JAVA_HOME
To verify the javac & java, I have used commands,
$ javac -version
$ java -version
If the output of the commands is appropriate/without any error, then you have configured the Java correctly.
I have explained both the methods of installing java in Linux, both are appropriate methods.
I would recommend using second method of installing java in Linux based on my experience.
Now you are ready for running various software which uses java like,