Introduction
Java is one of the most popular programming languages. It is used for developing anything from lightweight mobile applications to desktop applications.
This step-by-step guide will show you how to install Java on Ubuntu 18.04, 20.04, or 22.04.
Prerequisites
- A user account with
sudo
privileges. - Access to the command line (Ctrl + Alt + T).
- The apt (apt-get) command.
Note: This guide provides instructions that work on Ubuntu 18.04, 20.04, 22.04 and any other Ubuntu-based distribution (including Linux Mint, Kubuntu, and Elementary OS). If you are looking for other Java installation guides, please refer to:
- Installing Java on Fedora
- Installing Java on CentOS 7 or CentOS 8
- Installing Java on Raspberry Pi
Java Versions
There are four Java platforms available:
- Java Standard Edition (Java SE).
- Java Micro Editions (Java ME).
- Java Enterprise Edition (Java EE).
- JavaFX.
In this tutorial, we look at different packages within the Java SE.
The Ubuntu repository offers two open-source Java packages, the Java Development Kit (Open JDK) and Java Runtime Environment (Open JRE). JRE runs Java-based applications, while JDK is for developing and programming with Java.
Oracle Java, another SE implementation with additional commercial features, is also available. You can find the official Oracle JDK through a third-party repository or on the official website as a .deb file. However, bear in mind the license only allows non-commercial use.
Installing Java on Ubuntu
You can install one or several Java packages on one system or choose the version you want by specifying a version number. By default, Ubuntu includes Open JDK 11, an open-source variant of the JRE and JDK. Follow the steps below to install Java packages on Ubuntu.
Install OpenJDK
1. Open the terminal and update the package repository to ensure you download the latest software version:
sudo apt update
Provide the root password when prompted and confirm the update by typing y.
2. Next, install the latest Java Development Kit by running the following command:
sudo apt install default-jdk
Confirm the installation by typing y and pressing Enter.
Wait for the installation to complete.
Install OpenJRE 11
Installing the OpenJDK package also installs the default JRE package. However, in case the package doesn’t install, or the installation files are corrupted, reinstall the JRE package by following the steps below:
1. Update the package repository :
sudo apt update
2. Run the following command to download and install Java RE:
sudo apt install default-jre
Type y and press Enter to confirm the installation.
Install Oracle Java 17
The official Oracle JDK is available in third-party repositories or through a manual installation. The upside of installing from a third-party repository is that there is no need to upgrade the packages manually since it is done automatically during system upgrades. However, the downside is that there is no way to know when the PPA maintainer will decide to abandon it.
The sections below provide instructions for installing JDK from a third-party repository or by downloading and installing a .deb file. You can decide which one you prefer to use based on your needs.
Option 1: Download Oracle Java from Third-Party Repository
1. First, make sure to install the required dependencies by running:
sudo apt install software-properties-common
2. Add the required package repository:
sudo add-apt-repository ppa:linuxuprising/java -y
3. Integrate the new repository in the list of the system’s package sources by running the following command:
sudo apt update
4. Install Java 17, the latest LTS version:
sudo apt install oracle-java17-installer
5. During the installation, the Java configuration wizard appears:
Press TAB to select <Ok> and press Enter to confirm if you agree to the terms of the license agreement.
6. The wizard prompts you to accept the Oracle license terms to continue with the installation. Press TAB to select <Yes> and Enter to confirm.
After accepting the terms, the installer completes the Java installation.
7. Optionally, to set this Java version as the default one, run:
sudo apt install oracle-java17-set-default
Option 2: Install Oracle Java from Deb File
The .deb files are available on the official Oracle website. You can download and manually install the .deb files using a browser or using the wget command in the terminal. In this tutorial, we will use the latter one.
Follow the steps below:
1. Install the required dependencies:
sudo apt install libc6-i386 libc6-x32 libxi6 libxtst6 -y
2. Download the Java 17 .deb package with the following command:
wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.deb
3. After the download completes, install Java by running:
sudo dpkg -i jdk-17_linux-x64_bin.deb
Verify Java is Installed on Your System
Now, verify whether the software is part of your system. Check the version of Java running on your system using the following command:
java -version
The successful installation of Java on Ubuntu outputs program details, as in the image above. If there is no Java version found, the system says it is unable to find the software.
Install Specific Version of Java
If, for some reason, you do not wish to install the default or latest version of Java, you can specify the version number you prefer.
Install Specific Version of OpenJDK
You may decide to use an earlier version of OpenJDK, instead of the default OpenJDK 20.
To do so, open the terminal and use the following syntax:
sudo apt install openjdk-#-jdk
Replace #
with your preferred version number. For example:
Install Specific Version of Oracle Java
When you download the Oracle Java packages from a third-party repository, you have to specify a version number as part of the code. Therefore, if you want other versions of Java Oracle on your system, change that number accordingly.
The syntax for installing Oracle JDK is:
sudo apt install oracle-java#-installer
The symbol #
represents the Java version.
For instance, if you want to install Java 17, run the following command:
sudo apt install oracle-java17-installer
How to Set Default Java Version
As you can have multiple versions of Java installed on your system, you can decide which one is the default one.
First, run a command that shows all the installed versions on your computer:
sudo update-alternatives --config java
The image above shows that there are four alternatives on the system. The current default version is marked by an asterisk (*
). You can change the default version if you type its associated number and press Enter.
How to Set JAVA_HOME Environment Variable
The JAVA_HOME
environment variable determines the location of your Java installation. The variable helps other applications access Java’s installation path easily.
1. To set up the JAVA_HOME
variable, find where Java is installed. Use the following command to locate it:
sudo update-alternatives --config java
The Path section shows the locations of each installed Java version.
2. Once you see all the paths, copy the path to your preferred Java version.
3. Then, open the /etc/environment file using any text editor. In this example, we use nano:
sudo nano /etc/environment
4. At the end of the file, add a line which specifies the JAVA_HOME
location in the following manner:
JAVA_HOME="/your/installation/path/"
For example, if we copied the installation path for Java 17 in the previous step, the added line in the text code editor would be:
How to Uninstall Java on Ubuntu
To remove any of the Java packages installed, use the apt remove
command.
Remove the default version of OpenJDK by running this command:
sudo apt remove default-jdk
To uninstall a specific version of OpenJDK:
sudo apt remove openjdk-#-jdk
Replace #
with the number of the version you wish to remove.
Once you run the command, confirm the continuation of the process by pressing y, and the packages will be removed.
Note: Refer to our comprehensive breakdown of Best Java IDEs to find the best one for your needs.
Conclusion
This guide has shown you how to install Java on Ubuntu. It also instructed you on how to change the default version and set the home environment variable.
For more tutorials on how to install Java on other systems, make sure to read our article on how to install Java on Windows.
Đăng ký liền tay Nhận Ngay Bài Mới
Subscribe ngay
Cám ơn bạn đã đăng ký !
Lỗi đăng ký !
Add Comment