Introduction
What is Java?
Java is one of the essential programming language which has been widely used in all application developments. It has a lot of variations and usage in network and web app developments.
Here:
In this tutorial, we will see how to install Java on Ubuntu 16.04 server using terminal. Many programmers are using Ubuntu Operating System due to its long term support and consistency.
Developing your web app or running on application requires java most of the time. So, it is better to install Java when you create new server with your cloud service provider.
Without further Ado, lets dive into the installationmpart
Requirements
- Ubuntu 16.04 server from any cloud service provider or installed on your own machine
- You will need a server configured according to the Ubuntu Initial Server setup guide.
- Get 50$ Credit on Vulture Clouds Here and Test the Instruction for free.
Install Java on Ubuntu
Installing Java is easy with Ubuntu from its repository. You can install Open JDK version 8 from Ubuntu repository using the Ubuntu install Open JDK commands. Open JDK version 8 stable and is recommended by all the leading experts.
So, here in this tutorial, we will install Open JDK 8 on our server. First, Update the package index using the below command.
$ sudo apt-get update
First, we have to install Java Run Time Environment. To do that, use the command below.
$ sudo apt-get install default-jre
The JRE is only enough to run the Applications which requires Java. If you want to use JDK instead, you can install it.
The Java Development Kit installs the Java Run Time environment by default when you install JDK.
But:
JDK requires more space to install, however it is an advantage for you. To install JDK, run the below command.
$ sudo apt-get install default-jdk
Install Oracle JDK in Ubuntu
You have to do one more thing. That is downloading the PPA from Oracle and add it to the Ubuntu Repository.
Use the add-apt-repository command to accomplish the task.
$ sudo add-apt-repository ppa:webupd8team/java
This will download the Personal Package Archive(PPA) from Oracle and add it to the Ubuntu Repository. Then, update the Ubuntu repository with the below command.
$ sudo apt-get update
There are a lot of versions being released by Oracle with Major updates. You can install them any of them by specifying the version in the command.
Installing Oracle 8 JDK
Most of the people are installing this version of Oracle on their server due to its support. Oracle JDK 8 is the stable version. To install that, just use the below command.
$ sudo apt-get install oracle-java8-installer
At the same time, if you want to install any other available version, just mentioned the version in the command.
If i want to install Java 9, then i will use the below command.
$ sudo apt-get install oracle-java9-installer
Switching Between Multiple Versions of Java
You can use multiple versions of the Java programming language in the same server.
But:
You have to switch between the Version which you have installed. To do that, use the update alternatives command.
$ sudo update-alternatives --config java
The command will show you the following output.
There are 5 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-8-openjdk-amd64/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 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 4 /usr/lib/jvm/java-8-oracle/jre/bin/java 3 manual mode 5 /usr/lib/jvm/java-9-oracle/bin/java 4 manual mode Press <enter> to keep the current choice[*], or type selection number:
If you want to use the same version that you are using now, just press enter or type the number from the list to use that targeted version.
Setup the Java Installation Location
You have to setup the Java Home Environment variable to indicate the installation location of the Java Run time environment.
To find the Java installation location, use the command below.
$ sudo update-alternatives --config java
From there copy the path, then open a file using nano editor as mentioned below.
$ sudo nano /etc/environment
In that file, just paste the below command which contains the installation location of Java.
JAVA_HOME="/usr/lib/jvm/java-8-oracle"
Make sure, you paste the exact location where Java is installed.
Then, save and exit the file. To make the changes to work, just reload the file.
Now, check the environmental variable setup using the below command.
echo $JAVA_HOME
The command will show you the path which you have entered in the nano editor.
Conclusion
If you can’t install Java on Ubuntu server, command below or just ask for help from the support team by contacting us.
Make sure, subscribe for Server Clear upcoming tutorials. We will notify you of webinar and new tutorial release.
Here is the install java Fedora Tutorial.