JDK Installation
Ubuntu Linux
Programming
Java Development
Operating Systems

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.

Browse interview questions

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

  1. Update the package index: Open your terminal and run the following command to update your package index.
bash
   sudo apt update
  1. Install the default JDK: Execute the command below to install the default JDK package available in Ubuntu’s repositories:
bash
   sudo apt install default-jdk
  1. Verify the installation: After installation, you can verify it by checking the version of the Java compiler:
bash
   javac -version

Method 2: Installing Oracle JDK Manually

  1. Download the Oracle JDK: Navigate to the Oracle website and download the desired version of JDK for Linux. Choose .tar.gz as the format.
  2. Extract the tarball: Use the tar command to extract the JDK tarball:
bash
   tar -xvf jdk-11.0.1_linux-x64_bin.tar.gz
  1. Move JDK to a standard location: It's a good practice to move JDK to a standard location. /usr/lib/jvm is a commonly used directory for this purpose. Create the directory if it does not exist, and move the extracted JDK folder to this directory.
bash
   sudo mkdir -p /usr/lib/jvm
   sudo mv jdk-11.0.1 /usr/lib/jvm/
  1. Set JAVA_HOME and update the system path: To use the newly installed JDK as the default one, set the JAVA_HOME environment variable and update the PATH variable by adding the following lines to your ~/.bashrc file:
bash
   export JAVA_HOME=/usr/lib/jvm/jdk-11.0.1
   export PATH=$PATH:$JAVA_HOME/bin

Reload the .bashrc file to apply the changes.

bash
   source ~/.bashrc

Method 3: Using SDKMAN!

SDKMAN! is a tool that can manage multiple versions of JDK installations.

  1. Install SDKMAN!: Open a terminal and enter:
bash
   curl -s "https://get.sdkman.io" | bash
  1. Install JDK using SDKMAN!: After installation, install JDK by using SDKMAN!:
bash
   sdk install java 11.0.7.hs-adpt

Setting Up Java Environment

Once JDK is installed, configuring your Java environment correctly is crucial.

  1. Set environment variables: As discussed, setting JAVA_HOME and updating PATH are essential steps.
  2. Configure alternatives: If multiple Java versions are installed, use update-alternatives to select the default version:
bash
   sudo update-alternatives --config java
   sudo update-alternatives --config javac

Table: Java Installation and Commands Overview

StepCommandDescription
Update Package Indexsudo apt updateUpdates the list of available packages and their versions.
Install Default JDKsudo apt install default-jdkInstalls the default JDK from Ubuntu repositories.
Download Oracle JDKOracle WebsiteManual download of JDK tarball.
Extract tarballtar -xvf jdk-11.0.1_linux-x64_bin.tar.gzExtracts the JDK tarball.
Move to Standard locationsudo 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.