How to install Java Development Kit (JDK) on Ubuntu
Java Developer Kit (JDK) is required for developing and compiling Java applications. Some of the popular JDKs are provided by OpenJDK and Oracle. OpenJDK is an open-source implementation of the Java SE platform, while Oracle JDK is the official version from Oracle. Both versions are similar, but Oracle JDK may have some additional features unavailable in OpenJDK.
OpenJDK's Java Developer Kit (JDK) is available in apt's default repositories, while Oracle JDK needs to be manually downloaded and installed from Oracle's website. Installing the JDK will also install the Java Runtime Environment (JRE) as a dependency.
Multiple versions of OpenJDK's JDK are also available in apt's repository, and you can choose the version you want to install. You can install OpenJDK's JDK at the terminal.
Steps to install Java Development Kit (JDK) on Ubuntu:
-
Launch terminal.
-
Update apt's package list from the repositories.
$ sudo apt update [sudo] password for user: Hit:1 http://ports.ubuntu.com/ubuntu-ports kinetic-security InRelease Hit:2 http://us.ports.ubuntu.com/ubuntu-ports kinetic InRelease Hit:3 http://us.ports.ubuntu.com/ubuntu-ports kinetic-updates InRelease Hit:4 http://us.ports.ubuntu.com/ubuntu-ports kinetic-backports InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done 6 packages can be upgraded. Run 'apt list --upgradable' to see them.
-
Install the default version of OpenJDK's JDK for your version of Ubuntu.
$ sudo apt install --assume-yes default-jdk Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: ca-certificates-java default-jdk-headless default-jre default-jre-headless fonts-dejavu-extra java-common libatk-wrapper-java libatk-wrapper-java-jni libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-11-jdk openjdk-11-jdk-headless openjdk-11-jre openjdk-11-jre-headless x11proto-dev xorg-sgml-doctools xtrans-dev Suggested packages: libice-doc libsm-doc libx11-doc libxcb-doc libxt-doc openjdk-11-demo openjdk-11-source visualvm fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei The following NEW packages will be installed: ca-certificates-java default-jdk default-jdk-headless default-jre default-jre-headless fonts-dejavu-extra java-common libatk-wrapper-java libatk-wrapper-java-jni libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-11-jdk openjdk-11-jdk-headless openjdk-11-jre openjdk-11-jre-headless x11proto-dev xorg-sgml-doctools xtrans-dev 0 upgraded, 24 newly installed, 0 to remove and 6 not upgraded. Need to get 250 MB of archives. ### snipped ###
-
Check the installed JDK version.
$ javac -version javac 11.0.17
-
Check other available JDK versions for your Ubuntu installation.
$ sudo apt-cache search openjdk | grep "jdk\ " openjdk-11-jdk - OpenJDK Development Kit (JDK) default-jdk - Standard Java or Java compatible Development Kit openjdk-17-jdk - OpenJDK Development Kit (JDK) openjdk-18-jdk - OpenJDK Development Kit (JDK) openjdk-19-jdk - OpenJDK Development Kit (JDK) openjdk-20-jdk - OpenJDK Development Kit (JDK) openjdk-8-jdk - OpenJDK Development Kit (JDK)
-
Install other available JDK version if required.
$ sudo apt install --assume-yes openjdk-18-jdk Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: openjdk-18-jdk-headless openjdk-18-jre openjdk-18-jre-headless Suggested packages: openjdk-18-demo openjdk-18-source visualvm fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei The following NEW packages will be installed: openjdk-18-jdk openjdk-18-jdk-headless openjdk-18-jre openjdk-18-jre-headless 0 upgraded, 4 newly installed, 0 to remove and 6 not upgraded. Need to get 260 MB of archives. After this operation, 413 MB of additional disk space will be used.
-
Change the default JDK version if multiple versions of JDK were installed.
$ sudo update-alternatives --config javac There are 2 choices for the alternative javac (providing /usr/bin/javac). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-18-openjdk-arm64/bin/javac 1811 auto mode 1 /usr/lib/jvm/java-11-openjdk-arm64/bin/javac 1111 manual mode 2 /usr/lib/jvm/java-18-openjdk-arm64/bin/javac 1811 manual mode Press <enter> to keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/lib/jvm/java-11-openjdk-arm64/bin/javac to provide /usr/bin/javac (javac) in manual mode
-
Check if the selected JDK version is the default.
$ javac -version javac 11.0.17