How to Install Java JDK on Linux
Introduction
Installing the Java Development Kit (JDK) on a Linux system is a straightforward process, and while the exact steps may vary based on your Linux distribution, the general approach remains consistent across different versions of the JDK. This guide will walk you through the process of downloading, installing, and configuring the JDK on a Linux system, ensuring you’re ready for Java development.
Step 1: Download the JDK
To begin, you’ll need to download the appropriate version of the JDK for your Linux distribution.
- Visit the Download Page
Navigate to the Oracle JDK download page or OpenJDK. - Select the Version
Choose the version of the JDK that you want to install. Common versions include JDK 8, JDK 11, and JDK 17. It’s generally a good idea to select the latest Long-Term Support (LTS) version. - Download the Linux Installer
Look for the tar.gz file or RPM package suitable for your distribution:- Debian/Ubuntu: Look for
.deb
files. - Fedora/Red Hat/CentOS: Look for
.rpm
files. - Alternatively, you can download a tar.gz file for manual installation.
- Debian/Ubuntu: Look for
Step 2: Install the JDK
2.1. For Debian/Ubuntu-based Distributions
- Open the Terminal
You can usually find this in your applications menu or by pressingCtrl + Alt + T
. - Update Package Index:
1
sudo apt update
- Install JDK
If you downloaded a.deb
file, you can install it using:1
sudo dpkg -i path_to_your_file.deb
Alternatively, you can install OpenJDK directly from the repository:
1
sudo apt install openjdk-17-jdk # Replace with the desired version
2.2. For Fedora/Red Hat/CentOS-based Distributions
- Open the Terminal
- Install JDK
If you downloaded a.rpm
file, install it using:1
sudo rpm -ivh path_to_your_file.rpm
Alternatively, install OpenJDK from the repository:
1
sudo dnf install java-21-openjdk-devel # Replace with the desired version
2.3. For tar.gz installation (all distributions)
- Extract the tar.gz File
1
tar -xvzf jdk-17_linux-x64_bin.tar.gz # Replace with your file name
- Move it to a Suitable Directory
1
sudo mv jdk-17 /usr/local/
Step 3: Configure Environment Variables
After installation, you need to set the JAVA_HOME
environment variable and update the PATH
to use the JDK.
- Open the Terminal.
- Edit the Profile File
- For system-wide changes, edit
/etc/profile
:1
sudo nano /etc/profile
- For user-specific changes, edit
~/.bashrc
or~/.bash_profile
:1
nano ~/.bashrc
- For system-wide changes, edit
- Add the Following Lines
1 2
export JAVA_HOME=/usr/local/jdk-17 # Replace with your JDK path export PATH=$JAVA_HOME/bin:$PATH
- Save and Close the File
If using nano, pressCtrl + X
, thenY
, andEnter
. - Apply the Changes
1
source ~/.bashrc # or source /etc/profile for system-wide changes
Step 4: Verify the Installation
To verify that the JDK is correctly installed and configured, run the following commands in the terminal:
- Check the Java Version
1
java -version
You should see the version of Java that you installed.
- Check the Java Compiler (
javac
)1
javac -version
This will confirm that the Java compiler is available and working.
If both commands return the expected version numbers, your JDK installation is complete.
Additional Notes: Processor Architectures
When downloading the JDK, ensure you choose the correct version for your system’s architecture:
- x64
Most modern systems run on 64-bit architecture. If you are using a 64-bit version of Linux, download the x64 version of the JDK. - ARM
If you are on a newer ARM-based Linux system, ensure you download the appropriate ARM version of the JDK, as many distributions provide builds tailored for ARM processors.
To check your system’s architecture, you can run:
1
uname -m
This command will return x86_64
for 64-bit or arm64
for ARM.
Troubleshooting
- Java Not Found
Ensure that theJAVA_HOME
andPATH
variables are correctly set and that you have reloaded your profile. - Permission Issues
Usesudo
for commands that require administrative privileges.
Conclusion
Installing the Java JDK on Linux is a straightforward process, with similar steps across different distributions.
By downloading the appropriate installer, running it, and configuring environment variables like JAVA_HOME
and PATH
,
you will be set up for Java development.
Be sure to choose the correct JDK build based on your system architecture to avoid any compatibility issues. With the JDK successfully installed, you’re now ready to start developing Java applications on your Linux system!