Installation of Java in Ubuntu 18.04
In this guide:
- default JRE/JDK installation;
- installation of specific OpenJDK versions;
- Oracle JDK installation;
- switching between Java versions;
- setting the JAVA_HOME environment variable.
Java is an object-oriented programming language that Sun Microsystems started developing in 1991. It was officially released on May 23, 1995. The language was originally called Oak (by its creator James Gosling) and was designed for home electronics. Later it was renamed Java and became popular for applets, desktop applications and server software.
A key feature of Java is its flexible security model: programs run under the control of the Java virtual machine, and any operation beyond the permissions of the program, such as unauthorized access to data or connections to other computers, is stopped immediately.
Default JRE/JDK Installation
The easiest way is to install the OpenJDK version from the Ubuntu repository. OpenJDK is the open-source implementation of the JRE and JDK. On Ubuntu 18.04 the default version is OpenJDK 11.
Update the package index:
sudo apt updateCheck whether Java is already installed:
java -versionIf Java is not installed, you will see:
Command 'java' not found, but can be installed with:
apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headlessInstall the Java Runtime Environment (JRE), which is enough to run most Java applications:
sudo apt install default-jreCheck the installation:
java -versionopenjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing)To compile and run some Java programs, you also need the Java Development Kit (JDK). This command installs the JDK together with the JRE:
sudo apt install default-jdkCheck the version of the Java compiler:
javac -versionjavac 11.0.6Installing Specific OpenJDK Versions
OpenJDK 8
sudo apt install openjdk-8-jdkTo install only the JRE:
sudo apt install openjdk-8-jreOpenJDK 11
sudo apt install openjdk-11-jdkTo install only the JRE:
sudo apt install openjdk-11-jreOracle JDK Installation
Old guides use the webupd8team/java PPA and the oracle-java8-installer package. This PPA was closed in 2019 and no longer works. Oracle now provides .deb packages, so download the package from oracle.com and install it with apt, for example:
wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb
sudo apt install ./jdk-21_linux-x64_bin.debSwitching Between Java Versions
One server can have several Java versions. To set the default one, use the update-alternatives command:
sudo update-alternatives --config javaThere are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
Press <enter> to keep the current choice[*], or type selection number:Enter the number of the default Java version, or press Enter to keep the current setting. You can do the same for other Java commands, for example the javac compiler:
sudo update-alternatives --config javacSetting the JAVA_HOME Environment Variable
First, find the installation directory with update-alternatives (see above). Copy the path of the version you need without /bin/java at the end. Then open /etc/environment in a text editor:
sudo nano /etc/environmentAdd the following line at the end of the file. JAVA_HOME must point to the JDK root directory, not to its bin directory:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"This sets JAVA_HOME for all users. Save the file and exit nano (Ctrl+O, Enter, Ctrl+X). Reload the file to apply the changes to the current session:
source /etc/environmentCheck the variable:
echo $JAVA_HOME/usr/lib/jvm/java-11-openjdk-amd64Other users need to run source /etc/environment or log in again to apply the setting. For newer Ubuntu versions, see our guide on how to install Java on Ubuntu.
Blog