This guide shows how to install Java for Debian 10 and above and on Ubuntu 18.04 and above.
We will first get rid of all versions of Java from the VPS to prevent conflicts and to make sure that the version of Java we want is always used.
We will do this by executing one simple command that will remove the dependencies required regardless of which version of Java and from which distributor we have, and therefore the packages that require it.
sudo apt purge java-common -y
It is also worth getting rid of other leftovers that are no longer needed:
sudo apt autoremove -y
First, we will make sure that there are applications on the system that will make it easy to add the necessary repositories:
sudo apt update sudo apt install -y software-properties-common gnupg
Next, we will add the repositories that are common to all versions:
wget -O- https://apt.corretto.aws/corretto.key | sudo apt-key add - sudo add-apt-repository 'deb https://apt.corretto.aws stable main' sudo apt update
Now install the Java version of your choice:
sudo apt install -y java-18-amazon-corretto-jdk
sudo apt install -y java-11-amazon-corretto-jdk
sudo apt install -y java-1.8.0-amazon-corretto-jdk
Execute the command:
java -version
If the Java version matches the one you have selected and the feedback shows the word Corretto, this means that the installation was successful.