How to Install Java with “apt-get” on Ubuntu / Debian
Java remains a cornerstone of software development, powering a vast array of applications from enterprise solutions to mobile apps. If you are an Ubuntu 18.04, 20.04 or 22.04 user and want to install Java using the apt-get package manager, this guide will walk you through the process. By following the steps outlined below, you’ll be able to install Java and start developing or running Java applications on your Ubuntu system. This article focuses on How to install Java with “apt-get” on Ubuntu / Debian.
Prerequisites
Before we proceed with the installation, make sure you have the following prerequisites:
- A running Ubuntu or Debian system (18.04, 20.04, or 22.04 recommended).
- A user account with
sudo
privileges. - An active internet connection.
Step 1: Update Package Repository
The first step is to ensure that your package repository is up to date. Open a terminal window and run the following command:
$ sudo apt update
This command will refresh the package lists on your system and retrieve information about the latest available packages.
Step 2: Install Java using “apt-get”
Ubuntu provides OpenJDK as the default Java Development Kit (JDK) in its package repository. To install OpenJDK using the apt-get package manager, use the following command:
$ sudo apt-get install default-jdk
This command will download and install the default version of OpenJDK available for your Ubuntu distribution.
Step 3: Verify the Installation
Once the installation is complete, you can verify whether Java has been installed successfully by checking the version. To do this, run the following command:
$ java -version
If Java is installed correctly, you will see output similar to the following:
openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.12+7-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)
Step 4: Install Specific Java Versions
If you require a specific version of Java, Ubuntu also provides the option to install different versions using the apt-get package manager. Here are the steps to install Java 11 or 8:
Install Java 11
To install Java 11, run the following command:
$ sudo apt-get install openjdk-11-jdk
Install Java 8
To install Java 8, run the following command:
$ sudo apt-get install openjdk-8-jdk
After executing the respective command, the specified Java version will be downloaded and installed on your Ubuntu system.
Step 5: Set the Java Home Environment Variable
Method 1 : Manually
To ensure that Java is correctly configured on your system, it is recommended to set the JAVA_HOME
environment variable. This variable specifies the location of the Java installation directory. To set the JAVA_HOME
variable, follow these steps:
Open the .bashrc
file using a text editor:
$ nano ~/.bashrc
Add the following line at the end of the file:
$ export JAVA_HOME="/usr/lib/jvm/default-java"
This line assumes that the Java installation directory is /usr/lib/jvm/default-java
. If you have installed Java in a different location, make sure to modify the path accordingly.
Save the file and exit the text editor.
To apply the changes, run the following command:
$ source ~/.bashrc
This will reload the .bashrc
file and update the environment variables.
Method 2: using update-java-alternatives
update-java-alternatives
is a command-line tool that updates all alternatives belonging to one runtime or development kit for the Java language. It is part of the java-common
package and is used to update the symbolic links from /usr/bin/java
and /usr/bin/javac
to point to the Java runtime or development kit of your choice. You can use it to switch between different Java versions installed on your system.
To install it Run:
$ sudo apt-get install java-common
Here is an example of how to use it:
$ sudo update-java-alternatives --list
$ sudo update-java-alternatives --set /path/to/java/version
You can replace /path/to/java/version
with the path to the Java version you want to use.
Step 6: Test Java with a simple Program
To ensure that Java is functioning correctly on your system, let’s test it with a simple “Hello, World!” program. Follow these steps:
Create a new file called HelloWorld.java
using a text editor:
$ nano HelloWorld.java
Add the following code to the file:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Save the file and exit the text editor.
Compile the Java program using the javac
command:
$ javac HelloWorld.java
If there are no errors, you can run the program using the java
command:
$ java HelloWorld
The output should be:
Hello, World!
Congratulations! You have successfully installed Java using apt-get on your Ubuntu 20.04 or 22.04 system. You can now start developing and running Java applications on your machine. If you need a specific Java version, you can also install Java 11 or 8 following the provided instructions.
Conclusion
In this article, we have learned How to install Java with “apt-get” on Ubuntu / Debian. By following the step-by-step instructions, you should now have Java installed and ready to use on your Ubuntu system. Remember to keep your Java installation up to date to benefit from the latest features and security patches. Happy coding!
Alternative Solutions for Installing Java on Ubuntu/Debian
While apt-get
provides a straightforward method for installing Java, there are alternative approaches that offer different advantages, such as greater control over the installation process or access to specific Java distributions. Here are two alternative methods:
1. Installing Java Manually from a Tarball (.tar.gz)
This method provides the most control over the Java installation process. It involves downloading a compressed archive (tarball) of the desired Java Development Kit (JDK) or Java Runtime Environment (JRE) from Oracle or another vendor and manually extracting it to a directory on your system.
Steps:
-
Download the Java distribution: Navigate to the official Oracle Java download page or the download page of your preferred Java vendor (e.g., Adoptium, Azul). Choose the appropriate Linux version in
.tar.gz
format. -
Extract the archive: Open a terminal and navigate to the directory where you downloaded the tarball. Use the
tar
command to extract the contents:tar -xzvf <jdk-or-jre-filename>.tar.gz
Replace
<jdk-or-jre-filename>.tar.gz
with the actual name of the downloaded file. -
Move the extracted directory: Move the extracted directory to a suitable location, such as
/opt/java
:sudo mv <extracted-directory> /opt/java/
Replace
<extracted-directory>
with the name of the extracted directory. -
Set environment variables: As with the
apt-get
method, you’ll need to set theJAVA_HOME
environment variable. Edit your.bashrc
file:nano ~/.bashrc
Add the following lines, adjusting the path to reflect your actual installation directory:
export JAVA_HOME=/opt/java/<jdk-or-jre-directory> export PATH=$JAVA_HOME/bin:$PATH
Replace
<jdk-or-jre-directory>
with the actual name of the JDK or JRE directory within/opt/java
. -
Apply the changes: Reload your
.bashrc
file:source ~/.bashrc
-
Verify the installation: Check the Java version:
java -version
Advantages:
- Control: You have complete control over the installation location and the specific Java version.
- Flexibility: Allows you to install Java distributions not available through the
apt
package manager.
Disadvantages:
- Manual updates: You are responsible for manually downloading and installing updates.
- More complex: The installation process is more involved than using
apt-get
.
2. Using SDKMAN! (Software Development Kit Manager)
SDKMAN! is a command-line tool that simplifies the installation and management of multiple Software Development Kits (SDKs), including Java, on Unix-like systems. It provides a convenient way to install, switch between, and manage different Java versions.
Steps:
-
Install SDKMAN!: Open a terminal and run the following command:
curl -s "https://get.sdkman.io" | bash
Follow the on-screen instructions to complete the installation. You may need to open a new terminal session for SDKMAN! to be properly initialized.
-
Install Java using SDKMAN!: Use the
sdk install java
command to install a specific Java version. For example, to install the latest version of Java 11, you would use:sdk install java 11.0.20-open
(Replace
11.0.20-open
with the actual version you desire. Usesdk list java
to see available versions.) -
Set the default Java version (optional): If you have multiple Java versions installed, you can set a default version:
sdk default java 11.0.20-open
(Again, replace
11.0.20-open
with your desired default.) -
Verify the installation: Check the Java version:
java -version
Advantages:
- Simplified management: Simplifies the process of installing, switching between, and managing multiple Java versions.
- Convenience: Provides a command-line interface for common tasks.
- Support for multiple SDKs: Can be used to manage other SDKs besides Java.
Disadvantages:
- Requires SDKMAN!: Adds an additional dependency (SDKMAN! itself).
- Relies on SDKMAN!’s version availability: You are limited to the Java versions available through SDKMAN!.
Example:
Here’s a simple shell script to install Java using SDKMAN!:
#!/bin/bash
# Install SDKMAN! if not already installed
if ! command -v sdk >/dev/null 2>&1; then
echo "Installing SDKMAN!..."
curl -s "https://get.sdkman.io" | bash
echo "Please open a new terminal for SDKMAN! to be initialized."
exit 1
fi
# Update SDKMAN!
sdk update
# List available Java versions
echo "Available Java versions:"
sdk list java
# Install a specific Java version (replace with your desired version)
JAVA_VERSION="17.0.8-tem"
echo "Installing Java version $JAVA_VERSION..."
sdk install java $JAVA_VERSION
# Set the installed version as the default
echo "Setting Java $JAVA_VERSION as default..."
sdk default java $JAVA_VERSION
# Verify the installation
echo "Java version:"
java -version
This script automates the process of installing SDKMAN!, updating it, listing available Java versions, installing a specific version, setting it as the default, and verifying the installation. Remember to replace "17.0.8-tem"
with your desired Java version.
These alternative methods provide different approaches to installing Java on Ubuntu/Debian, catering to various needs and preferences. This comprehensive guide illustrates How to install Java with “apt-get” on Ubuntu / Debian as well as other methods.