How To Install Java on Ubuntu: A Friendly Guide

How To Install Java on Ubuntu: A Friendly Guide

How To Install Java on Ubuntu A Friendly Guide.

How To Install Java on Ubuntu: A Friendly Guide

Hello there, Ubuntu adventurer! So, you want to install Java on your Ubuntu dedicated server or VPS? Well, you're in luck! Today, we're going on a digital quest together to make that happen. Now, grab your mouse and keyboard, and let's get started!

Understanding Java

Java, as you might already know, is one of the most popular programming languages in the world. It's used in a myriad of applications from enterprise software to Android apps, and even in the tiny devices of the Internet of Things (IoT).

Java requires a runtime environment (JRE) to execute programs. And to develop Java applications, you'll need a Java Development Kit (JDK). The JDK includes the JRE, so if you're going to be doing some coding, you'll only need to install the JDK.

There are different distributions of JDK available, including Oracle's official JDK and OpenJDK, an open-source version. For our quest, we'll use OpenJDK, but I'll also show you how to install Oracle's JDK for good measure.

Let's start the journey!

Step 1: Checking for Existing Java Versions

Before we get started, it's good to check if Java is already installed on your system. Open your terminal (CTRL+ALT+T is the quickest way to do this) and type:


java -version

If you get a response that indicates a Java version, it means Java is already installed. If not, the coast is clear for our installation adventure.

Step 2: Updating the Package Repository

Next, we need to ensure that Ubuntu's package repository is up-to-date. In your terminal, enter the following commands:


sudo apt update
sudo apt upgrade

Now, we're ready to install Java!

Step 3: Installing OpenJDK

To install OpenJDK, enter the following command into your terminal:


sudo apt install default-jdk

This command installs the latest available version of OpenJDK. If you want a specific version, you can specify it. For example, to install OpenJDK 11, you would enter:


sudo apt install openjdk-11-jdk

Once the installation is done, verify it by checking the Java version again:


java -version

If everything went well, you should see the version of Java that you installed displayed.

Step 4: Setting the JAVA_HOME Environment Variable

Some Java applications use the JAVA_HOME environment variable to determine the Java installation location. To set this variable, first, determine where Java is installed with:


sudo update-alternatives --config java

This command will display the path to the Java installation. Copy this path.

Next, open the environment variables file:


sudo nano /etc/environment

At the end of this file, add the following line (replace /path/to/java with the path you copied):


JAVA_HOME="/path_HOME=\"/path/to/java\"

Then, save and close the file (CTRL+X, then Y, then ENTER). To load the variable, use the source command:


source /etc/environment

You can verify that JAVA_HOME has been set by echoing it:


echo $JAVA_HOME

Great job! You've just installed OpenJDK on your Ubuntu system!

Optional: Installing Oracle's JDK

If you need to install Oracle's JDK for specific reasons, here's how. Please note that Oracle's JDK has some licensing restrictions for commercial use.

First, go to Oracle's official Java Downloads page. Accept the license agreement and download the .tar.gz package for Linux.

Once the download completes, navigate to the download location and extract the package with:


tar -xvf jdk-11.*.tar.gz

This will create a directory with the JDK files. Move this directory to /usr/lib/jvm (you may need to create this directory).


sudo mkdir -p /usr/lib/jvm
sudo mv jdk-11.* /usr/lib/jvm/

Next, install the new JDK with the update-alternatives command:


sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-11.*/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-11.*/bin/javac 1

Now, when you run the java -version command, you should see Oracle's JDK as the default. You can switch between JDKs with the update-alternatives --config command:


sudo update-alternatives --config java
sudo update-alternatives --config javac

And that's it! You're now a bona fide Java installer on Ubuntu. Whether you're setting up a server, programming an Android app, or just exploring, you've got the tools you need. Keep exploring, keep learning, and as always, have fun on your Ubuntu adventure!