How to install Java on your Amazon EC2 instance in 2025?

Manzeel Uprety
3 min readAug 24, 2021

--

Updated Feb 2025

Last weekend, I was setting up JMeter for load testing. Instead of stressing my local machine 💻, I decided to spin up an EC2 instance on AWS. But before installing JMeter, I needed Java on the instance.

If you’re setting up Java on an AWS EC2 instance, this guide is for you. Let’s get started!

🚀 Step 1: Launch an EC2 Instance

1️⃣ Login to your AWS Console and navigate to the EC2 Dashboard.
2️⃣ Click Launch Instance and configure the instance as per your requirements.
3️⃣ Make sure to download the .pem key file during the final steps — this is needed for SSH access.
4️⃣ For this guide, we’re using an Ubuntu-based EC2 instance.

🛠 Step 2: Connect to Your EC2 Instance

Once your instance is up and running, connect via SSH from your local terminal.

1️⃣ Move to the directory where your .pem file is stored and update its permissions:

chmod 400 mypemfile.pem

2️⃣ Connect to the EC2 instance:

ssh -i "mypemfile.pem" ubuntu@ec2-x-xxx-xx-0.ap-south-1.compute.amazonaws.com

Pro Tip: Ensure you’re in the same directory as your .pem file before running the SSH command.

> chmod 400 mypemfile.pem

After changing the file permission, connect to you EC2 instance from your terminal.

> ssh -i "mypemfile.pem" ubuntu@ec2-x-xxx-xx-0.ap-south-1.compute.amazonaws.com

📌 Step 3: Install Java on EC2

1️⃣ Switch to a superuser:

sudo -i

2️⃣ Update the package list to ensure everything is up to date:

sudo apt update && sudo apt upgrade -y

3️⃣ Verify if Java is already installed (most likely, it’s not)

java -version

📂 Step 4: Create a Directory for Java

1️⃣ Navigate to the /usr/lib/ directory:

cd /usr/lib/

2️⃣ Create a new jvm directory:

mkdir jvm

3️⃣ Grant proper permissions

chmod u=rwx,g=rwx,o=rwx jvm

🔄 Step 5: Upload Java JDK to Your EC2 Instance

1️⃣ Exit from your EC2 session (exit) and upload the JDK file from your local machine:

scp -i "mypemfile.pem" ~/Downloads/jdk-11.0.25_linux-x64_bin.tar.gz ubuntu@ec2-x-xxx-xx-0.ap-south-1.compute.amazonaws.com:/usr/lib/jvm

2️⃣ Once the upload is complete, reconnect to your EC2 instance and navigate to the jvm directory:

cd /usr/lib/jvm

3️⃣ Extract the JDK archive:

tar zxvf jdk-11.0.25_linux-x64_bin.tar.gz

🌎 Step 6: Set Up Java Environment Variables

1️⃣ Open the environment file for editing:

nano /etc/environment

2️⃣ Add the following lines to the file

PATH="/usr/lib/jvm/jdk-11.0.25/bin:$PATH"
JAVA_HOME="/usr/lib/jvm/jdk-11.0.25"

3️⃣ Save and exit (CTRL + X, then Y, then Enter).

🛠 Step 7: Configure Java Alternatives

Run the following commands to let the system recognise Java and javac:

update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-11.0.25/bin/java" 0
update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk-11.0.25/bin/javac" 0

✅ Step 8: Verify Java Installation

Run:

java -version

🎉 Boom! Java is now installed on your AWS EC2 instance! 🚀🔥

===============================

===============================

If you find this story helpful, feel free to buy me a virtual coffee 😉👇🏻

https://ko-fi.com/mnzel1

--

--

Manzeel Uprety
Manzeel Uprety

No responses yet