The Java Development Kit (JDK) is required to develop and run Java applications. It contains essential tools, including the Java compiler and runtime libraries. In Ubuntu, you can install JDK from two primary sources: OpenJDK and Oracle JDK. OpenJDK is open-source and can be installed directly from the Ubuntu package manager, while Oracle JDK requires manual installation from Oracle’s website.
By default, OpenJDK is included in Ubuntu’s software repositories. It offers multiple versions, such as JDK 8, JDK 11, and JDK 17, which you can install depending on your project’s requirements. Oracle JDK is an alternative that includes some additional features, but it must be downloaded and installed manually.
Installing OpenJDK or Oracle JDK on Ubuntu will also install the Java Runtime Environment (JRE), which is necessary to run Java applications. You can easily manage different versions of JDK and switch between them using Ubuntu’s terminal commands.
Steps to install JDK on Ubuntu:
- Launch the terminal.
- Update the apt 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 available 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 ###
- Verify the installed JDK version.
$ javac -version javac 11.0.17
- List other available versions of OpenJDK in the repository.
$ 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 a different version of OpenJDK 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.
- Set the default JDK version if multiple versions are 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
- Verify if the selected JDK version is the default.
$ javac -version javac 11.0.17
This guide is tested on Ubuntu:
Version | Code Name |
---|---|
22.04 LTS | Jammy Jellyfish |
23.10 | Mantic Minotaur |
24.04 LTS | Noble Numbat |
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.