Java is a programming language used to build Android apps and real life web applications like Gmail and Google Docs. This ubiquitous language can be installed onto an Ubuntu server and its what we’ll be teaching in this tutorial today.
- Open the terminal and log in as root. If you are logged in as another user, you will need to add sudo before each command.
- Working on a Linux Ubuntu 16.04 server
- No installations of previous Java versions
Step 1: Updating your system
apt-get update && apt-get upgrade
Step 2: Install the Repo
First, install the repository, its provided by a third party and you’ll need to press enter to continue downloading. The Oracle Java (JDK) Installer automatically downloads and installs Oracle JDK8.
Update your system once more
Step 3: Install Java 8
Using the command below will kickstart the Java Installer which will assist in our task. Press Y when prompted to continue.
apt-get install oracle-java8-installer
Once you come to the Installer page click OK and Yes to agree to their licensing and terms.
Step 4: Verify the Installation of Java 8
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
Set Java’s Home Environment
You may be looking to set Java’s home environment once we’ve installed it onto our server. To do that, lets first, let’s find Java’s path.
update-alternatives --config java
There is 1 choice for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 auto mode
* 1 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode
Take the highlighted path and add the variable into your /etc/environment file. Add the JAVA_HOME path after any line that is already present.
Once you are in the file type i to insert the needed line:
Save and exit the file by typing ESC followed by :wq and press enter. Afterward, use the source command to have the system recognize the changes in your file.
You’ll know the variable has been set correctly by calling the variable.
root@1804:~# echo $JAVA_HOME