How to install Java with “apt-get” on Ubuntu / Debian

Java 8 11 Apt-Get Ubuntu Debian

Java is a popular programming language that is widely used for building various applications and software. If you are an Ubuntu 18.04, 20.04 or 22.04 user and want to install Java using the apt-get package manager, this guide will walk you through the process. By following the steps outlined below, you’ll be able to install Java and start developing or running Java applications on your Ubuntu system.

Prerequisites

Before we proceed with the installation, make sure you have the following prerequisites:

  1. An Ubuntu 18.04, 20.04 or 22.04 / Debian OS.
  2. Administrative access to the system (sudo privileges).

Step 1: Update Package Repository

The first step is to ensure that your package repository is up to date. Open a terminal window and run the following command:

$ sudo apt update

This command will refresh the package lists on your system and retrieve information about the latest available packages.

Step 2: Install Java using “apt-get”

Ubuntu provides OpenJDK as the default Java Development Kit (JDK) in its package repository. To install OpenJDK using the apt-get package manager, use the following command:

$ sudo apt-get install default-jdk

This command will download and install the default version of OpenJDK available for your Ubuntu distribution.

Step 3: Verify the Installation

Once the installation is complete, you can verify whether Java has been installed successfully by checking the version. To do this, run the following command:

$ java -version

If Java is installed correctly, you will see output similar to the following:

openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.12+7-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

Step 4: Install Specific Java Versions

If you require a specific version of Java, Ubuntu also provides the option to install different versions using the apt-get package manager. Here are the steps to install Java 11 or 8:

Install Java 11

To install Java 11, run the following command:

$ sudo apt-get install openjdk-11-jdk

Install Java 8

To install Java 8, run the following command:

$ sudo apt-get install openjdk-8-jdk

After executing the respective command, the specified Java version will be downloaded and installed on your Ubuntu system.

Step 5: Set the Java Home Environment Variable

Method 1 : Manually

To ensure that Java is correctly configured on your system, it is recommended to set the JAVA_HOME environment variable. This variable specifies the location of the Java installation directory. To set the JAVA_HOME variable, follow these steps:

Open the .bashrc file using a text editor:

$ nano ~/.bashrc

Add the following line at the end of the file:

$ export JAVA_HOME="/usr/lib/jvm/default-java"

This line assumes that the Java installation directory is /usr/lib/jvm/default-java. If you have installed Java in a different location, make sure to modify the path accordingly.

Save the file and exit the text editor.

To apply the changes, run the following command:

$ source ~/.bashrc

This will reload the .bashrc file and update the environment variables.

Method 2: using update-java-alternatives

update-java-alternatives is a command-line tool that updates all alternatives belonging to one runtime or development kit for the Java language. It is part of the java-common package and is used to update the symbolic links from /usr/bin/java and /usr/bin/javac to point to the Java runtime or development kit of your choice. You can use it to switch between different Java versions installed on your system.

To install it Run:

$ sudo apt-get install java-common

Here is an example of how to use it:

$ sudo update-java-alternatives --list
$ sudo update-java-alternatives --set /path/to/java/version

You can replace /path/to/java/version with the path to the Java version you want to use.

Step 6: Test Java with a simple Program

To ensure that Java is functioning correctly on your system, let’s test it with a simple “Hello, World!” program. Follow these steps:

Create a new file called HelloWorld.java using a text editor:

$ nano HelloWorld.java

Add the following code to the file:

publicclassHelloWorld {
    publicstaticvoidmain(String[] args) {
       System.out.println("Hello, World!");
    }
}

Save the file and exit the text editor.

Compile the Java program using the javac command:

$ javac HelloWorld.java

If there are no errors, you can run the program using the java command:

$ java HelloWorld

The output should be:

Hello, World!

Congratulations! You have successfully installed Java using apt-get on your Ubuntu 20.04 or 22.04 system. You can now start developing and running Java applications on your machine. If you need a specific Java version, you can also install Java 10 or 9 following the provided instructions.

Conclusion

In this article, we have learned how to install Java on Ubuntu 20.04/22.04 using the apt-get package manager. By following the step-by-step instructions, you should now have Java installed and ready to use on your Ubuntu system. Remember to keep your Java installation up to date to benefit from the latest features and security patches. Happy coding!

LEAVE A COMMENT