Java Illustration by Walker Cahall
Verified and Tested 06/02/21
Introduction
If you had the option to choose between a base model car or a fully loaded model, which one would you choose?
Both cars will take you from point A to point B, but with the fully-loaded car, you have additional features that can make rush hour a chill hour.
Well, that’s the same concept when we take a closer look at Java. There are two standard types of installations, JRE (Java Runtime Environment) and JDK (Java Development Kit). Like the base model car, JRE enables the ability to create Java applications for different types of deployments using minimal core tools to accomplish the task. JDK is a fully loaded Development Kit that has everything that JRE has plus additional resources to create/secure Applications and Applets.
Installing Java on Ubuntu 20.04
Before we begin, let make sure that the server is fully updated with the following command:
sudo apt-get update
After your server has been fully updated, you will need to know what version of Java is currently installed or if it is not installed, with the following command:
java -version
Once you have verified if Java is installed or not, choose the type of Java installation that you want with the following:
sudo apt-get install default-jdk sudo apt-get install default-jre
The above command will install the latest version of Java available in the Ubuntu default repository.
Install Java Open JRE or JDK on Ubuntu 20.04
If you want to install the specific Java version, run the following command:
sudo apt-get install openjdk-8-jre sudo apt-get install openjdk-8-jdk
Install Java Oracle JRE or JDK on Ubuntu 20.04
Another alternative Java install is with Oracle JRE and JDK. By default, Oracle Java is not available in the Ubuntu default repository. So you will need to download it from the Oracle website.
First, log in to the Oracle website and visit the Oracle Download page. Then, pick your desired Java version and download it to your server. In this section, we will download jdk-11.0.8_linux-x64_bin.tar.gz. Once the download is completed, create a directory for Java installation:
sudo mkdir -p /var/cache/oracle-jdk11-installer-local
Next, copy the downloaded file to the directory:
sudo cp jdk-11.0.8_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/
Next, install some required dependencies:
sudo apt-get install software-properties-common gnupg2 -y
Next, add the Java key with the following command:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A
Next, add the Java repository with the following command:
sudo add-apt-repository ppa:linuxuprising/java
Next, update the repository and install the Oracle Java with the following command:
sudo apt-get update -y sudo apt-get install oracle-java11-installer-local -y
If you have multiple versions of Java installed on your server, then you have the ability to select a default version. Check your alternatives with the following command:
sudo update-alternatives --config java
Output:
There are 3 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-11-oracle/bin/java 1091 manual mode 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press to keep the current choice[*], or type selection number: 1
Once you have viewed your alternatives, choose the version that you want by selecting the assigned number, and then hit enter.
Setup JAVA_HOME on Ubuntu 20.04
Since many programs nowadays need a JAVA_HOME environment variable to work properly. We will need to find the appropriate path to make these changes. With the following command, you can view your installs and their path:
sudo update-alternatives --config java
A directory path listing for each installation is below:
/usr/lib/jvm/java-11-openjdk-amd64/ /usr/lib/jvm/java-11-oracle/ /usr/lib/jvm/java-8-openjdk-amd64/
To edit the environment file use your text editor and edit the following file:
sudo nano /etc/environment
Now that you are in the environment file, add the following code along with the Path of your installation from the previous step. ( Example: JAVA_HOME=”YOUR_PATH”)
JAVA_HOME="/usr/lib/jvm/java-11-oracle/"
Reload the file so all your changes could take effect with the following command:
source /etc/environment
Verify that your implementations are correct with the following command. Confirm :
echo $JAVA_HOME
What Next?
Congratulations! You now have successfully installed Java on your Ubuntu server. Thank you for following along and feel free to check back with us for further updates or check out other helpful Ubuntu How-to’s in our blog.
Cloud hosting is just one of the many hosting services offered by Atlantic.Net – we also offer dedicated server hosting services. Contact us today for more information!