This article will teach you how to install Java on 18.04. The commands will work on any system running the latest version, Ubuntu 18.04 Bionic Beaver, as well as 16.04, and any other Long Term Support (LTS) releases. Java is normally distributed in three editions, namely Micro (I), business (EE), and Standard Edition (EE). For this guide, we will be using Java SE 11 (LTS).
In general, Java is one of the main programming languages in the world. It is built to have as few dependencies as possible, allowing app developers to “write once, run anywhere.” As a result, compiled Java code can run on any platform that supports Java. You can use it to create anything from simple web applications to advanced software.
Without further ado, let’s see how to install Java on 18.04!
JDK or JRE?
The (Java Development Kit) and the (Java Runtime Environment) are the two main kits that we will be working with. Oracle Java contains the JVM, the Java program, along with other infrastructure, while the JDK is a larger set that includes the JRE. The JDK is essentially an SDK (software development kit), as it gives you access to everything in the JRE, plus tools like jdb, javadoc and the compiler itself, javac.
If you are not interested in compiling Java software, the JRE will do just fine. If you wish, you can modify it later.
The following commands assume that you are already using a root user, but in case you are not, you should add sudo to the beginning of your commands.
Install Java on Ubuntu via default packages
The first method to install Java on Ubuntu is through default packages. Start the process by updating the current packages to the latest version:
apt-get update && apt-get upgrade
Upon completion, install the latest version of the Java Runtime Environment (JRE) by running this command:
apt-get install default-jre
It is also possible to install the Java Development Kit (JDK). This is required by specific software or used to compile Java programs. JDK includes JRE, so there are no disadvantages if you choose this method.
apt-get install default-jdk
That is all! Java is ready to use on your Ubuntu machine. You can check if it was configured correctly with these commands:
java -version javac -version
The output will be similar to this:
root@vps42681194:~# java -version openjdk version “11.0.6” 2020-01-14 OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1) OpenJDK 64-Bit Server VM (build 11.0. 6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing) root@vps42681194:~# javac -version javac 11.0.6
Install Java on Ubuntu via Oracle JDK
Another alternative is to install Java on Ubuntu using the official Oracle JDK. Start by updating your current packages to the latest version:
apt-get update && apt-get upgrade
For this example, we will use a third-party library managed by WebUpd8. To make deployment easier, first install the following package:
apt-get install software-properties-common
Next, get the Java PPA with the following command:
add-apt-repository ppa:webupd8team/java
You might even want to run a quick update while you’re at it:
apt-get update
Finally, you can install Java on Ubuntu by running the following command and following the on-screen instructions:
aptupdate; apt-get install oracle-java9-installer
The above command will install Java version 14. To get an older version, you can change the syntax to java13 0 java11 and so on.
And that’s it! Java was installed successfully. You can check the version with these two commands:
java -version javac -version
You will see output similar to this:
root@vps42681194:~# java -version java version “14” 2020-03-17 Java(TM) SE Runtime Environment (build 14+36-1461) Java HotSpot(TM) 64-Bit Server VM (build 14+36- 1461, mixed mode, sharing) root@vps42681194:~# javac -version javac 14
Manage Java
A single server can have multiple Java installations. You can set the default version using this command line:
update-alternatives –config java
The following output will appear:
There are 2 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-14-oracle/bin/java 1091 manual mode Press
Type the number of the selection menu you want to use as the default. You can also do this with other Java commands, like:
update-alternatives –config javac update-alternatives –config javadoc update-alternatives –config jarsigner
Java Home Environment Configuration
Another useful thing to know is how to set the variable JAVA_HOME. Most applications require it to find the location of your Java installation. The command used above can help you locate the Java installation path:
update-alternatives –config java here are 2 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-14-oracle/bin/java 1091 manual mode Press
When you have copied the installation path, you will have to edit the file environment located in the directory etc:
nano /etc/environment
Add the following line at the end to set the value of JAVA_HOME:
JAVA_HOME=”/usr/lib/jvm/java-14-oracle/bin/java”
Don’t forget to update it with the actual path to your Java installation.
press CTRL + X to finish editing and save changes. Then make sure the changes take effect with this command:
source /etc/environment
You can check if it is active by typing:
echo $JAVA_HOME
further reading
Check out these articles for more VPS guides and setup tips:
conclusion
By the end of this tutorial, you have learned how to install Java on Ubuntu 18.04. You also learned some basic concepts, such as setting the default Java version and defining the JAVA_HOME variable.
If you found this article useful, feel free to share it with the world. And if you have any tips, suggestions or ideas, we are waiting for them in the comments section!
Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.
