How to install Java on your EC2 instance in 2025?

Published: at 01:16 PM

Last weekend, I was setting up JMeter for load testing. Instead of stressing my local machine 💻, I decided to spin up an Ubuntu-based 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.

Java on EC2

🛠 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.


📌 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 recognize 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

Java version

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


🪄 The One-Line Version

Steps 4 through 7 exist because I was installing a specific Oracle JDK build from a downloaded tarball. If you don’t need a particular vendor — and for JMeter you almost certainly don’t — Ubuntu’s package manager does all of it in one command:

sudo apt update && sudo apt install -y openjdk-17-jdk

That handles the directory, the extraction, JAVA_HOME, and the update-alternatives wiring for you. Verify the same way:

java -version

Use openjdk-17-jre instead if you only need to run Java and not compile it — it’s a noticeably smaller download, which matters on a small instance.

So when is the manual route worth it? When you need a JDK version the distribution doesn’t package, when licensing requires the Oracle build specifically, or when you’re pinning an exact build for reproducibility. Otherwise, take the one-liner.

🔀 Switching Between Installed JDKs

If you end up with more than one — the distribution’s OpenJDK plus a manually installed Oracle JDK, say — update-alternatives lets you pick interactively rather than editing paths:

sudo update-alternatives --config java

It prints the installed options with their paths and lets you select one by number. The same works for javac.

✅ Making JAVA_HOME Stick

/etc/environment is read at login, not by every process. Two consequences worth knowing:

Check it landed:

echo $JAVA_HOME
$JAVA_HOME/bin/java -version

If the second command works but plain java -version doesn’t, your PATH entry didn’t apply — the JAVA_HOME line took effect but the PATH line didn’t.

🧪 Now Actually Run JMeter

The whole point of this was load testing, and there’s one thing to get right: run JMeter in non-GUI mode on the server. The GUI is for building test plans locally; using it to run a test eats the memory you wanted for load generation.

jmeter -n -t testplan.jmx -l results.jtl -e -o ./report

-n is non-GUI, -t is the plan, -l is the raw results log, and -e -o generates the HTML dashboard afterwards. Pull the report back down with scp and open it locally.

🧯 Common Snags


ko-fi