How to Install OpenJDK 11 on Amazon Linux 2

How to Install OpenJDK 11 on Amazon Linux 2

How to Install OpenJDK 11 on Amazon Linux 2

Dear Reader, I hope you are doing well today. In this tutorial, I am here to help you install OpenJDK 11 or Java 11 on Amazon Linux 2 EC2 instance in a step-by-step manner. Additionally, I’ll show you how to install JDK 11 apart from the JRE that OpenJDK 11 installs.

You can also check my posts to install NginxApachePHP 8.2GitAnsiblePHP 8.1Docker etc. on Amazon Linux 2.

Background

Amazon Linux 2: Amazon Linux 2 is a very popular Linux AMI provided by AWS itself and is also free tier eligible. You can use the Yum package manager to install software on Linux 2.

OpenJDK 11: OpenJDK 11 is the open-source implementation of version 11 of the Java SE Platform or Java 11. It is entirely open source or free to use and comes with GNU General Public License.

Related: How to Get Started with AWS Lambda using Java

Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.

Steps to Install OpenJDK 11 on Amazon Linux 2

  1. Launch an EC2 Instance(Linux 2)
  2. Connect to your Linux 2 Instance
  3. Install OpenJDK 11 on the Instance
  4. Verify the OpenJDK 11 Installation
  5. Install JDK and Make Javac Work
  6. Switch Between Different Java Version (Optional)

Let’s get started …

Step 1: Launch an EC2 Instance(Linux 2)

Before we install the OpenJDK 11 on our EC2 Linux 2 instance, we’ll need an instance up and running. I have covered it for you in a tutorial already.

Link to tutorial: How to Launch EC2 Linux 2 Instance Step by Step in AWS

Step 2: Connect to your Linux 2 instance

After you are done with the first step, you have an up-and-running Linux 2 instance. Time to connect to the instance.

If you want to SSH from your local system, feel free to use the below command from your terminal.

ssh -i /path/my-key-pair.pem ec2-user@instance-public-ip

I prefer the EC2 instance connect to SSH into instances from the browser itself and without managing a key pair.

How to SSH into the EC2 Instance using the EC2 Instance Connect from the Browser

1. Select your instance and click Connect as shown below

How to Install Apache Web Server on Amazon Linux 2

2. You will see the below screen with the default username for a Linux 2 instance which is ec2-user.

How to Install Apache Web Server on Amazon Linux 2

verify that ec2-user is showing in the username field and click Connect.

And, you are connected to your instance- Voila !!!

How to Install Apache Web Server on Amazon Linux 2

Awesome !!!

Shall we move ahead with the OpenJDK installation now?

Alright, let’s go.

Step 3: Install OpenJDK 11 on Amazon Linux 2 Instance

Amazon’s core package repository usually contains only super-stable software. For the newer version, they put it on amazon-linux-extras repository.

Having said that, OpenJDK 8 is present in the core repository however OpenJDK 11 is not available in the core repository rather it is available in amazon-linux-extras repository.

To confirm this, you can run the below command and hit enter-

amazon-linux-extras

This will list the available packages under this extras repository. You will see that java-openjdk11 is available under it as you can see in the below screenshot.

You can install java-openjdk11 from the amazon-linux-extras repository using the below command-

sudo amazon-linux-extras install java-openjdk11

Note: You’ll have to use sudo(root privilege ) to run the above command. The reason is that whenever you try to install, remove or change any software, you must have root privilege to do such tasks.

Let’s start with the installation of OpenJDK 11.

1 Update the System with the Latest Packages

It’s always recommended to run a system update to update all the packages to the latest ones before you install new software.

Run the below command for the same-

sudo yum update -y

2 Install OpenJDK 11 on Amazon Linux 2

As we already saw, you can install openjdk 11 on amazon linux 2 using amazon-linux-extras install java-openjdk11 command.

sudo amazon-linux-extras install java-openjdk11

We are already connected to the EC2 instance, let’s run the above command.

Java installation started

Once the installation is complete, you can move ahead to check the installed version in the system.

Step 4: Verify the OpenJDK 11 Installation

We have just installed java 11 or openjdk 11 on our Linux 2 EC2 instance. Let’s check the installed version of java(runtime) and javac(compiler) using the below commands.

java -version
javac -version

As you run the above command, you will notice that the command sudo amazon-linux-extras install java-openjdk11 only installs JRE and not the JDK. And the reason is obvious. The package contains only JRE. And you get the error like –

javac: command not found

Check Java 11 Version

This is absolutely okay if all you want is to run a Java 11 application on your Linux 2 machines. However, If your purpose is to use the machine for Java-related development work as well, you will need JDK as well.

Let’s see how can we install JDK 11.

Step 5: Install JDK and Make Javac Work

The package that contains JDK 11 is java-11-openjdk-devel and to my surprise, it is available in the core repository itself.

Install JDK on Amazon Linux2 using the below command-

sudo yum install java-11-openjdk-devel
JDK 11

Enter y and hit enter.

The installation gets completed and this time when you type javac -version, you get the installed version.

How to Install Java 11 on Amazon Linux 2 5

Congratulation !!!

You have successfully installed OpenJDK 11 or Java 11 with JDK and JRE on Linux 2. At least, you are all ready to develop and run Java applications on your Amazon Linux instance.

Step 6: Switch Between Different Java Versions (Optional)

This step is absolutely optional and just for your information. If you are running more than one version of Java, you can switch between them using the below command.

alternatives --config java
alternatives --config java

All the options are shown to you with a selection number. You can enter a number and that version will become your current version. You can do the same with Javac if you need.

Optional: Troubleshooting Installation of OpenJDK 11 on Amazon Linux 2

If you are facing a problem during installation, make sure to check the below things.

Tip 1:

#1 Check if your EC2 instance has internet connection

Amazon EC2 instance uses the public internet to download and install a package. So make sure your connectivity is okay.

Additionally, check your security group. It should allow an outbound HTTPS connection.

 #2 Check if amazon-linux-extras is Installed

Since you install the OpenJDK 11 from the extras repository, Make sure it is installed on your Linux 2.

which amazon-linux-extras
How to Install OpenJDK 11 on Amazon Linux 2 1

As you can see usually Linux 2 already comes with it. However, if you don’t see it- you can install it by using the below command-

sudo yum install -y amazon-linux-extras

#3 Enable java-openjdk 11 topic

In extra’s repository usually we need to enable a topic or repo before we can install it.

sudo amazon-linux-extras enable java-openjdk11

This command enables OpenJDK topic and it gives you the command to install

How to Install OpenJDK 11 on Amazon Linux 2 2

You can use the above commands like-

 yum clean metadata && yum install java-11-openjdk

yum clean metadata helps in the cleanup of packages/repo and is useful at troubleshooting time.

Conclusion:

In this post, we learnt how to Install OpenJDK 11 on Amazon Linux 2 EC2 Instance. We learnt a bit about the Amazon extras repository and how to install a package using that on Amazon Linux 2.

Apart from the OpenJDK 11 which contains only JRE, we also saw how to install JDK 11 that you can actually use for any kind of development purpose if you need.

Were you able to install OpenJDK 11 on Linux 2 using this tutorial? Let me know in the comment section.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

Suggested Read:

Leave a Reply

Your email address will not be published. Required fields are marked *