Methods for Installing JDK on Ubuntu
There are two primary approaches to installing JDK on Ubuntu:
- Using the apt package manager
- Manual installation from downloaded packages
The apt method is recommended as it simplifies future updates through apt-get upgrade.
Method 1: Installing via apt
This section covers both OpenJDK and Oracle JDK installations.
1. OpenJDK Installation
To install OpenJDK, follow these steps:
Step 1: Find available OpenJDK versions
apt-cache search openjdk
The command will display various OpenJDK versions. Choose the appropriate one for your needs.
Step 2: Install the selected version
sudo apt-get install openjdk-11-jdk
Step 3: Configure environment variables
Edit your bash configuration file:
vim ~/.bashrc
Add the following lines at the end of the file:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
Step 4: Verify installation
java -version
Successful installation will show output similar to:
openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-Ubuntu-0ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.12+7-Ubuntu-0ubuntu1, mixed mode)
2. Oracle JDK Installation
For Oracle JDK, follow these steps:
Step 1: Add the Oracle PPA repository
sudo apt install software-properties-common
sudo add-apt-repository ppa:linuxuprising/java
sudo apt-get update
Step 2: Install Oracle Java
sudo apt-get install oracle-java17-installer
During installation, accept Oracle's terms and conditions when prompted.
Step 3: Set as default Java version
sudo update-java-alternatives -s java-17-oracle
Step 4: Switch between Java versions (if multiple installed)
To switch to Java 11:
sudo update-java-alternatives -s java-11-openjdk-amd64
To switch back to Oracle Java 17:
sudo update-java-alternatives -s java-17-oracle
Step 5: Verify installation
java -version
Method 2: Manual Installation from Archive
When you need a specific JDK version not available in repositories, manual installation is an option.
Step 1: Download JDK from Oracle
Visit the Oracle JDK download page and download the appropriate .tar.gz package for your system.
Step 2: Extract and install
Create installation directory:
sudo mkdir /opt/java
Extract the downloaded archive:
sudo tar -zxvf jdk-17.0.2_linux-x64_bin.tar.gz -C /opt/java
Step 3: Configure environment variables
Edit your bash configuration:
sudo vim ~/.bashrc
Add these lines at the end:
# Oracle JDK configuration
export JAVA_HOME=/opt/java/jdk-17.0.2
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
Apply the changes immediately:
source ~/.bashrc
Step 4: Register the Java installation
sudo update-alternatives --install /usr/bin/java java /opt/java/jdk-17.0.2/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk-17.0.2/bin/javac 1
sudo update-alternatives --install /usr/bin/jar jar /opt/java/jdk-17.0.2/bin/jar 1
Configure the default Java version:
sudo update-alternatives --config java
Step 5: Verify installation
java -version
Successful installation will display:
java version "17.0.2" 2021-07-20
Java(TM) SE Runtime Environment (build 17.0.2+8-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-86, mixed mode)
Step 6: Troubleshooting
If you encounter the error "cannot execute binary file", it may indicate a mismatch between your system architecture and the JDK version. Verify your system architecture:
uname -m
The output "x86_64" indicates a 64-bit system, while "i686" indicates 32-bit.