View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Step-by-Step Guide on How to Install Java in Linux

Updated on 30/04/20251,614 Views

If you're diving into the world of Java development or looking to run powerful Java-based applications, mastering the installation process is your first big step. 

Java continues to dominate the programming world with its versatility, reliability, and massive community support. Whether you're a curious beginner or an experienced developer setting up a new environment, learning to install Java in Linux is a crucial milestone. 

In this guide, we’ll take you through every detail—from choosing the right Java version to getting your first program up and running smoothly on your Linux system.

Kickstart your journey in Java and software development with this industry-recognized Software Engineering program.

An Overview

The Java Runtime Environment (JRE), the Java Virtual Machine (JVM), and the Java Development Kit (JDK) are among the components of Java. A JDK is necessary for the development and execution of Java programs, while a JRE is only needed for execution.

The JVM is responsible for translating and executing the Java bytecode that the JRE receives. Thus, understanding the differences between JDK, JRE, and JVM before learning how to install Java in Linux OS is crucial.

Several Java Development Kit(JDK) versions can be installed on different operating systems, including Linux, Windows, and Mac OS. The following is a detailed tutorial on how to install Java in a Linux terminal.

Boost your development career with these top-rated tech programs:

•  UpGrads Full Stack Development Bootcamp

• AI-Powered Full Stack Development by IIIT Bangalore

•  UpGrads Cloud Computing & DevOps Certificate 

JVM, JRE, or JDK: What’s the Right Choice for You?

Before you start installing Java, it’s essential to know exactly what you’re setting up. 

Java isn’t just a single package—it comes with different components, each serving a specific purpose. Understanding the difference between the JVM, JRE, and JDK will help you choose the right one based on whether you want to run or develop Java applications.

  • Java Virtual Machine (JVM)

The Java Virtual Machine, or JVM, is the core engine that makes Java applications portable and platform-independent. It takes the compiled Java bytecode (.class files) and translates it into machine code that your computer can execute. 

However, the JVM by itself is purely for running programs — it doesn’t include tools for writing, compiling, or debugging Java code. Think of the JVM as the interpreter that allows your system to understand and execute Java applications, but not to create them.

In short, if you only have the JVM installed, you can run Java programs that others have created, but you won’t be able to build or modify Java applications yourself.

Check out: Exploring Java Architecture: A Guide to Java's Core, JVM and JDK Architecture

  • Java Runtime Environment (JRE)

The Java Runtime Environment, or JRE, builds on top of the JVM. It bundles the JVM together with a set of libraries, Java class files, and other necessary resources that Java programs need to function properly. 

Without the JRE, even if you have the JVM, many Java applications would not run because they rely on these libraries and support files.

In short, the JRE allows you to run Java applications smoothly, but like the JVM, it does not include the development tools you would need to write or compile new programs. It's perfect for end-users who only want to execute Java apps without developing anything themselves.

Must explore: How Do Java Programs Work?

  • Java Development Kit (JDK)

The Java Development Kit, or JDK, is the complete package for anyone who wants to develop Java applications. 

It includes everything found in the JRE (meaning you can run Java programs), but it also adds essential development tools such as the Java compiler (javac), a debugger (jdb), and various utilities for documentation (javadoc) and packaging.

If you’re planning to write, compile, test, or debug Java programs, the JDK is absolutely necessary.

In short, if you're a developer (or learning to be one), you need the JDK. It provides everything required to create and run Java programs from scratch.

Also read: How to Install Java in Windows 10

Key Difference Between JRE vs OpenJDK vs Oracle JDK

Feature/Aspect

JRE (Java Runtime Environment)

OpenJDK

Oracle JDK

Purpose

Run existing Java applications

Develop and run Java applications

Develop and run Java applications

Includes Development Tools

No (Only runtime environment)

Yes (Compiler, debugger, etc.)

Yes (Compiler, debugger, additional tools)

License

Free for personal and commercial use

Open-source (GNU General Public License - GPL)

Free for personal use; Subscription needed for commercial use

Support & Updates

Limited

Community-driven updates and support

Oracle-provided updates, patches, and long-term support (LTS)

Performance

Optimized for running Java apps

Very good; matches Oracle JDK closely

Highly optimized with some enterprise enhancements

Additional Features

None beyond running applications

Standard Java SE features

Advanced monitoring, flight recorder, mission control tools

Suitability For

Users who only want to run Java applications

Developers and general production environments

Enterprises needing long-term support and additional features

Cost

Free

Free

Requires payment/subscription for commercial use

Also check: Why is Java a Platform Independent Language?

How to Check If Java Is Already Installed in Linux?

Before installing Java, it’s a good idea to check whether it's already installed on your system. Here’s how you can do it:

Step 1: Open the Terminal

  • Launch your terminal. (You can usually find it in your applications menu or open it using the shortcut Ctrl + Alt + T.)

Step 2: Check the Java Runtime (java)

Type the following command and press Enter:java -version

  • If Java is installed, you will see an output similar to:openjdk version "21.0.2" 2024-01-16

OpenJDK Runtime Environment (build 21.0.2+13)

OpenJDK 64-Bit Server VM (build 21.0.2+13, mixed mode)

  • If Java is not installed, you might see an error like:Command 'java' not found

Step 3: Check the Java Compiler (javac)

To verify if the Java Development Kit (JDK) is installed (not just the runtime), check for the compiler:

javac -version

  • If the JDK is installed, you'll get an output showing the compiler version, such as:javac 21.0.2
  • If the JDK is missing, you’ll see a message like:Command 'javac' not found

Steps to Install Java in Linux

Using your Linux distribution package administration feature is one of the simplest methods to get Java on your computer and install it. The primary steps are as follows:

Step 1: Open a window on your Linux Terminal.

Step 2: Run the command below to update the package list as follows:

$ sudo apt update

Step 3: The following command should be executed when installing the default Java Runtime Environment (JRE):

$ sudo apt install default-jr

The default JRE for your system will be installed with this command.

Step 4: Check the Java version on your machine using the command below to confirm the installation:

$ java -version

We installed and downloaded Java on the Linux machine using the package manager.

How to Install JDK in Linux?

If you need the JDK for Java development, here’s how to install it:

Install OpenJDK JDK

On Ubuntu/Debian:

sudo apt update

sudo apt install default-jdk

Or install a specific version, like OpenJDK 21:

sudo apt install openjdk-21-jdk

On CentOS/RHEL/Fedora:

sudo dnf install java-21-openjdk-devel

Verify Installation

Check your installed version:

java -version

javac -version

Both should show the Java version if the installation was successful.

Download and Install Java IDE on Linux

An IDE (Integrated Development Environment) makes writing, debugging, and running Java programs easier.

Popular Java IDEs available for Linux include:

  • Eclipse
  • IntelliJ IDEA
  • NetBeans

Install Eclipse on Ubuntu/Debian:

sudo snap install --classic eclipse

Install IntelliJ IDEA (Community Edition):

sudo snap install intellij-idea-community --classic

Or, download directly from the official websites and follow the Linux installation guides provided.

Write Your First Java Program on a Linux OS

Now that everything’s ready, let’s write and run your first Java program!

  1. Create a Java file:

nano HelloWorld.java

  1. Write a simple Java code:
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Linux Java World!");
    }
}

Save and exit the file (in nano, press CTRL+O, Enter, then CTRL+X).

  1. Compile the Java program:

javac HelloWorld.java

This will generate a HelloWorld.class file.

  1. Run the program:
java HelloWorld

Output:

Hello, Linux Java World!

Congratulations! You've successfully compiled and run your first Java program on Linux.

Concluding Words

Installing Java on Linux is straightforward once you understand the different components—JRE, JDK, and OpenJDK vs Oracle JDK. 

Whether you are setting up a development machine or just need Java to run applications, Linux offers a flexible, easy installation experience through package managers or manual downloads.

After setting up Java and an IDE, you’re ready to dive deep into the world of Java programming.

If you encounter any issues during installation, Linux communities and official Java documentation are great places for troubleshooting help. 

Happy coding!

FAQs

1. What version of Java should I install on Linux?

The version of Java to install depends on your specific needs. If you are just running applications, the JRE will suffice, but for development, you need the JDK. Most users opt for OpenJDK, which is free and open-source.

2. Can I install Java on Linux via a package manager?

Yes, Linux makes it easy to install Java using its package managers like apt for Ubuntu/Debian or dnf for Fedora. This ensures that you install a version compatible with your system and automatically manages dependencies. Using the package manager is the most efficient way to install Java.

3. How do I check if Java is installed on my Linux system?

You can check if Java is installed by typing java -version or javac -version in the terminal. If Java is installed, the terminal will display the version information. If it's not installed, you'll receive an error, indicating you need to install it.

4. Is OpenJDK as good as Oracle JDK?

OpenJDK is a fully open-source implementation of Java and is widely used in both development and production environments. It provides everything you need for Java development and is suitable for most users. While Oracle JDK has some additional features and support, OpenJDK is often sufficient for most Linux users.

5. How do I install the latest version of Java on Linux?

To install the latest version of Java, you can either use the package manager with a command like sudo apt install openjdk-21-jdk or download it directly from the official Oracle or OpenJDK websites. Always make sure to check for updates and install the latest stable version for security and performance.

6. Can I install multiple versions of Java on Linux?

Yes, Linux allows you to install multiple versions of Java simultaneously. You can configure your system to use a specific version of Java for different projects, making it easy to manage multiple environments. Use the update-alternatives command on Ubuntu to switch between Java versions.

7. How do I set Java environment variables on Linux?

You need to set JAVA_HOME and update your PATH variable for Java to work properly. You can add these variables to your .bashrc or .profile file to make them persistent. After updating the file, run source ~/.bashrc to apply the changes.

8. What IDEs are recommended for Java development on Linux?

Popular Java IDEs for Linux include Eclipse, IntelliJ IDEA, and NetBeans. These IDEs offer robust features like code suggestions, debugging, and integrated build systems. Most of them are available for free, with advanced versions offering additional features for enterprise use.

9. How do I compile and run a Java program on Linux?

To compile a Java program, use the javac command followed by the filename (e.g., javac HelloWorld.java). After compilation, run the program using the java command (e.g., java HelloWorld). Make sure your Java files are located in the same directory where you're running these commands.

10. How do I uninstall Java from Linux?

Uninstalling Java from Linux is simple with the package manager. For example, use sudo apt remove openjdk-21-jdk on Ubuntu/Debian, or sudo dnf remove java-21-openjdk on Fedora. You can also use sudo apt autoremove to remove unnecessary dependencies.

11. Why is Java not running on my Linux machine after installation?

If Java isn't running, it might be due to incorrect environment variable settings or a missing Java runtime. Ensure that JAVA_HOME and PATH are correctly set, and verify Java’s version by typing java -version in the terminal. If the issue persists, reinstalling Java or checking for any missing dependencies may resolve the issue.

image

Take the Free Quiz on Java

Answer quick questions and assess your Java knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Explore Our Free Software Tutorials

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.