How to install the JDK on Ubuntu Linux
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Installing the Java Development Kit (JDK) on Ubuntu Linux is an essential step for Java developers and for running Java applications. This guide provides a comprehensive walkthrough on how to install JDK on Ubuntu using various methods.
Prerequisites
Before installing JDK, make sure you have sudo privileges on your Ubuntu system. This will allow you to execute commands with administrative rights, which are necessary for the installation process.
Installation Methods
You can install JDK on Ubuntu through several methods, including utilizing Ubuntu's APT package manager, manually installing using the tarball from Oracle’s website, or using alternative versions like OpenJDK.
Method 1: Installing Default JDK with APT
- Update the package index: Open your terminal and run the following command to update your package index.
- Install the default JDK: Execute the command below to install the default JDK package available in Ubuntu’s repositories:
- Verify the installation: After installation, you can verify it by checking the version of the Java compiler:
Method 2: Installing Oracle JDK Manually
- Download the Oracle JDK: Navigate to the Oracle website and download the desired version of JDK for Linux. Choose
.tar.gzas the format. - Extract the tarball: Use the
tarcommand to extract the JDK tarball:
- Move JDK to a standard location: It's a good practice to move JDK to a standard location.
/usr/lib/jvmis a commonly used directory for this purpose. Create the directory if it does not exist, and move the extracted JDK folder to this directory.
- Set JAVA_HOME and update the system path: To use the newly installed JDK as the default one, set the
JAVA_HOMEenvironment variable and update thePATHvariable by adding the following lines to your~/.bashrcfile:
Reload the .bashrc file to apply the changes.
Method 3: Using SDKMAN!
SDKMAN! is a tool that can manage multiple versions of JDK installations.
- Install SDKMAN!: Open a terminal and enter:
- Install JDK using SDKMAN!: After installation, install JDK by using SDKMAN!:
Setting Up Java Environment
Once JDK is installed, configuring your Java environment correctly is crucial.
- Set environment variables: As discussed, setting
JAVA_HOMEand updatingPATHare essential steps. - Configure alternatives: If multiple Java versions are installed, use
update-alternativesto select the default version:
Table: Java Installation and Commands Overview
| Step | Command | Description |
| Update Package Index | sudo apt update | Updates the list of available packages and their versions. |
| Install Default JDK | sudo apt install default-jdk | Installs the default JDK from Ubuntu repositories. |
| Download Oracle JDK | Oracle Website | Manual download of JDK tarball. |
| Extract tarball | tar -xvf jdk-11.0.1_linux-x64_bin.tar.gz | Extracts the JDK tarball. |
| Move to Standard location | sudo mv jdk-11.0.1 /usr/lib/jvm/ | Moves JDK to a common directory for Java installations. |
Conclusion
Installing the JDK on Ubuntu Linux is straightforward with these methods, ensuring that any setup—whether it's for development, production, or multiple version management—is robust and reliable.
Related reading
- How to integrate Apache Spark with Spring MVC web application for interactive user sessions
- How to integration test auto configuration for a custom Spring Boot style starter library?
- How to intercept SLF4J with logback logging via a JUnit test?
- How to interpret a Java thread stack?
- How to iterate over a JSONObject?
- How to Iterate over a Set/HashSet without an Iterator?
- How to iterate over a TreeMap?
- How to iterate through range of Dates in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.