Java is a popular programming language used for building various applications. In this tutorial, we’ll guide you through the process of installing Java on an Ubuntu 22.04 server using APT. We’ll also discuss how to manage different versions of Java and set the default version. By the end of this tutorial, you’ll have a working Java installation on your Shape.host Cloud VPS.

Step 1: Deploying a Cloud Instance on Shape.host

  • Log in to Shape.host Dashboard:
  • Navigate to the Shape.host website and log in to your account.
  • Create a New Instance:
  • Click on the “Create” button located at the top right corner of the dashboard.
  • From the dropdown menu, select “Instances”.
Shape.host Resources dashboard showing the Create menu and instance rebuild controls.
  • Select Instance Location:
  • Choose the desired location for your server. For this tutorial, we’ll select “New York, USA”.
Shape.host Create New Instance form with the available data center locations.
  • Choose a Plan:
  • Select a plan that fits your requirements. For example, you might choose a plan with 2 cores CPU, 2 GB Memory, and 50 GB SSD disk space.
  • Select an Operating System:
  • Scroll down to the “Choose an image” section and select “Ubuntu 22.04”.
Shape.host instance form with a server plan and Ubuntu image selected.
  • Configure Additional Options:
  • (Optional) You can configure additional options like User Data Configuration and IPv6 Networking.
  • Enter a hostname for your instance, e.g., “Tutorial Ubuntu”.
  • Click on the “Create instance” button to deploy the instance.
Shape.host instance form showing hostname, instance count and the Create Instance button.

Step 2: Connecting to Your Instance

  • Retrieve SSH Credentials:
  • Note the IP address of your newly created instance from the Shape.host dashboard.
  • Connect via SSH:
  • Open a terminal on your local machine.
  • Use the following command to connect to your instance:
    sh ssh root@your_instance_ip
  • Replace your_instance_ip with the actual IP address of your instance.

To follow this tutorial, you will need a server running Ubuntu 22.04 with root access.

Step 3: Installing Java

Ubuntu 22.04 comes with multiple versions of Java available through the default APT repositories. In this tutorial, we will install the latest version of OpenJDK, the open-source implementation of the Java Platform.

First, update the package index:

apt update
Terminal showing APT package management commands and output.

Next, install the default Java Development Kit (JDK) package by running:

apt install default-jdk -y
Terminal showing APT package management commands and output.

This will install the latest version of OpenJDK. You can verify the installation by checking the Java version:

java -version
Terminal showing java commands or output during Installing Java.

You should see an output similar to this:

openjdk version "11.0.23" 2024-04-16
OpenJDK Runtime Environment (build 11.0.23+9-post-Ubuntu-1ubuntu122.04.1)
OpenJDK 64-Bit Server VM (build 11.0.23+9-post-Ubuntu-1ubuntu122.04.1, mixed mode, sharing)

Step 4: Installing Specific Versions of OpenJDK

If you need a specific version of Java, you can install it using APT. For example, to install OpenJDK 11, run:

apt install openjdk-11-jdk -y

You can install multiple versions of Java alongside each other. After installation, you can check the installed version using the java -version command.

Step 5: Managing Java Versions

If you have multiple versions of Java installed, you can configure the default version using the update-alternatives command.

First, check the available Java versions:

update-alternatives --config java

You will see a list of installed Java versions. Enter the number associated with the version you want to set as the default, and press Enter.

For example:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      manual mode

Press <enter> to keep the current choice[*], or type selection number: 1

To set the default Java compiler (javac), use the following command:

update-alternatives --config javac

Step 6: Setting the JAVA_HOME Environment Variable

For many applications, you will need to set the JAVA_HOME environment variable, which points to the Java installation directory.

To find the installation path of your Java version, use the update-alternatives command:

update-alternatives --config java
Terminal showing java commands or output during Setting the JAVA_HOME Environment Variable.

The path is shown next to the selected version. For example: /usr/lib/jvm/java-11-openjdk-amd64/bin/java.

To set the JAVA_HOME variable, edit the /etc/environment file:

nano /etc/environment
Terminal showing nano commands or output during Setting the JAVA_HOME Environment Variable.

Add the following line at the end of the file:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Terminal showing the GNU nano editor and configuration text.

Replace the path with the actual path of your Java installation.

Load the environment variable:

source /etc/environment
Terminal showing the command to edit the JAVA_HOME environment configuration.

Verify that JAVA_HOME is set correctly:

echo $JAVA_HOME
Terminal showing nano commands or output during Setting the JAVA_HOME Environment Variable.

You should see the Java installation path.

By following these steps, you have successfully installed and configured Java on your Shape.host Cloud VPS. Shape.host offers reliable and scalable Cloud VPS services to meet your hosting needs. Whether you’re running a small website or a large application, Shape.host provides the resources and support you need to succeed.