How To Install OpenJDK On Ubuntu


In this tutorial, we will show you how to install OpenJDK on Ubuntu using apt.

Prerequisites

Before you begin this guide you'll need the following:

  • A local development or server environment setup. You can check out the following tutorial here...
Step 1 — Update your package index

Run the following command to update your current package index:

sudo apt update

You'll see output similar to the following output:


Hit:1 http://dl.google.com/linux/chrome/deb stable InRelease
Hit:2 http://ppa.launchpad.net/ethereum/ethereum/ubuntu focal InRelease        
Hit:3 http://archive.ubuntu.com/ubuntu focal InRelease                         
Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]        
Hit:7 https://packages.microsoft.com/repos/ms-teams stable InRelease           
Get:8 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal-updates/main Sources [462 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal-updates/restricted Sources [32.9 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,580 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [604 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/main Translation-en [304 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [280 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [810 kB]
Get:17 http://archive.ubuntu.com/ubuntu focal-updates/restricted Translation-en [115 kB]
Get:18 http://archive.ubuntu.com/ubuntu focal-updates/universe i386 Packages [667 kB]
Get:19 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [904 kB]
Get:20 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [391 kB]
Get:21 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [940 B]
Get:22 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 DEP-11 Metadata [7,980 B]
Get:23 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [23.7 kB]
Get:24 http://archive.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [40.7 kB]
Get:25 http://archive.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [66.2 kB]
Get:26 http://archive.ubuntu.com/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [2,464 B]
99% [18 Packages store 0 B]                                         733 kB/s 0s

Step 2 — Install OpenJDK

In this step, we are going to install the package onto our system using apt.

 Run the following command to install the Apache Maven package:


sudo apt install default-jdk

You'll see output similar to the following output:


Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libllvm11 libllvm11:i386 shim
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libaopalliance-java libatinject-jsr330-api-java libcdi-api-java
  libcommons-cli-java libgeronimo-annotation-1.3-spec-java
  libgeronimo-interceptor-3.0-spec-java libguava-java libguice-java
  libhawtjni-runtime-java libjansi-java libjansi-native-java libjsr305-java
  libmaven-parent-java libmaven-resolver-java libmaven-shared-utils-java
  libmaven3-core-java libplexus-cipher-java libplexus-classworlds-java
  libplexus-component-annotations-java libplexus-interpolation-java
  libplexus-sec-dispatcher-java libplexus-utils2-java libsisu-inject-java
  libsisu-plexus-java libslf4j-java libwagon-file-java
  libwagon-http-shaded-java libwagon-provider-api-java
Suggested packages:
  libaopalliance-java-doc libatinject-jsr330-api-java-doc libservlet3.1-java
  libasm-java libcglib-java libjsr305-java-doc libmaven-shared-utils-java-doc
  liblogback-java libplexus-cipher-java-doc libplexus-classworlds-java-doc
  libplexus-sec-dispatcher-java-doc libplexus-utils2-java-doc testng
The following NEW packages will be installed:
  libaopalliance-java libatinject-jsr330-api-java libcdi-api-java
  libcommons-cli-java libgeronimo-annotation-1.3-spec-java
  libgeronimo-interceptor-3.0-spec-java libguava-java libguice-java
  libhawtjni-runtime-java libjansi-java libjansi-native-java libjsr305-java
  libmaven-parent-java libmaven-resolver-java libmaven-shared-utils-java
  libmaven3-core-java libplexus-cipher-java libplexus-classworlds-java
  libplexus-component-annotations-java libplexus-interpolation-java
  libplexus-sec-dispatcher-java libplexus-utils2-java libsisu-inject-java
  libsisu-plexus-java libslf4j-java libwagon-file-java
  libwagon-http-shaded-java libwagon-provider-api-java maven
0 upgraded, 29 newly installed, 0 to remove and 59 not upgraded.
Need to get 8,537 kB of archives.
After this operation, 11.2 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Type Y in your terminal input to download the package and all required dependencies.

Step 3 - Configure Environment Variables

In this step, we are going to configure our environment variables

Modify the contents of the following file: $HOME/.profile using your favorite text editor for this example we are using vi:


vi $HOME/.profile

Once the text file opens up in the terminal window, add the following lines that point to the path of your JDK at the end of the file:


JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

export PATH=${JAVA_HOME}/bin:${PATH}

Save and close the file. This script will be sourced at a shell startup.

Load the environment variables using the source command:


source $HOME/.profile

You can now verify that the environment variable is set by running the following command:


echo $JAVA_HOME

You'll see output similar to the following output:


/usr/lib/jvm/java-11-openjdk-amd64

Step 4 — Verify The JDK Installation

In this step, we are going to verify the installation.

 Run the following command to verify that the package was successfully installed.


java -version

You'll see output similar to the following output:


openjdk version "11.0.13" 2021-10-19

OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.20.04)

OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

Conclusion

In this article, you should have installed the OpenJDK package, Set the environment variables to be used later by an IDE or application servers, and verify your installation.

Comments