{"id":3579,"date":"2023-11-17T17:23:39","date_gmt":"2023-11-17T09:23:39","guid":{"rendered":"https:\/\/www.servergigabit.com\/guide\/?post_type=kb&#038;p=3579"},"modified":"2026-03-16T14:11:15","modified_gmt":"2026-03-16T06:11:15","slug":"java-on-ubuntu-7-steps-install-java","status":"publish","type":"kb","link":"https:\/\/www.servergigabit.com\/guide\/kb\/java-on-ubuntu-7-steps-install-java","title":{"rendered":"Java on Ubuntu: 7 Easy and Powerful Steps to Install"},"content":{"rendered":"<header class=\"entry-header \">\n<div class=\"entry-meta\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-3580\" src=\"https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/images-10.jpeg\" alt=\"Java on Ubuntu\" width=\"652\" height=\"434\" \/><\/div>\n<\/header>\n<div class=\"entry-content clear\">\n<p><a href=\"https:\/\/www.java.com\/en\/\" rel=\"nofollow noopener\" target=\"_blank\">Java<\/a> is a widely used programming language that is essential for developing and running applications on various platforms. This guide provides a step-by-step process for installing Java on Ubuntu 18.04, 20.04, and 22.04 using the Apt-Get package manager. Additionally, it explains how to switch between different Java versions and set the JAVA_HOME environment variable.<\/p>\n<h2 id=\"h-installation-of-the-default-jre-jdk\" class=\"wp-block-heading\"><span id=\"installation-of-the-default-jrejdk\" class=\"uag-toc__heading-anchor\"><\/span><strong>Installation of the Default JRE\/JDK<\/strong><\/h2>\n<p data-sourcepos=\"9:1-9:70\">The default JRE and JDK can be installed using the following commands:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">apt-get install default-jre\r\napt-get install default-jdk<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"16:1-16:89\"><em>The JDK includes the JRE, so you can install the JDK instead of the JRE if you need both.<\/em><\/p>\n<h2 id=\"h-installing-other-java-versions\" class=\"wp-block-heading\"><span id=\"installing-other-java-versions\" class=\"uag-toc__heading-anchor\"><\/span><strong>Installing other Java Versions<\/strong><\/h2>\n<p data-sourcepos=\"20:1-20:69\">Older versions of Java can be installed using the following commands:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">apt-get install openjdk-8-jdk -y\r\napt-get install openjdk-8-jre -y<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"27:1-27:65\"><em>Replace &#8220;8&#8221; with the desired Java version, such as 11, 16, or 17.<\/em><\/p>\n<h2 id=\"h-switching-between-different-java-versions-nbsp\" class=\"wp-block-heading\"><span id=\"switching-between-different-java-versions\" class=\"uag-toc__heading-anchor\"><\/span><strong>Switching between different Java Versions\u00a0<\/strong><\/h2>\n<p>It is possible to have several Java versions installed at the same time, but only one Java version is selected or active at a time.<\/p>\n<p>You can switch between different Java versions using the following command:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">sudo update-alternatives --config java\r\n<picture><img decoding=\"async\" class=\"wp-image-16998 webpexpress-processed\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-1.png\" sizes=\"(max-width: 859px) 100vw, 859px\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-1.png 859w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-1-600x188.png 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-1-768x241.png 768w\" alt=\"How To Install Java on Ubuntu using Apt-Get\" width=\"859\" height=\"269\" \/><\/picture><\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"37:1-37:122\"><em><span class=\"animating\">This will display a list of installed Java versions.<\/span><span class=\"animating\"> Select the desired Java version by entering its corresponding number.<\/span><\/em><\/p>\n<h2 id=\"h-how-to-set-the-java-home-environment-variable\" class=\"wp-block-heading\"><span id=\"how-to-set-the-javahome-environment-variable\" class=\"uag-toc__heading-anchor\"><\/span><strong>How to set the \u201cJAVA_HOME\u201d environment Variable<\/strong><\/h2>\n<p data-sourcepos=\"41:1-41:140\"><span class=\"animating\">The JAVA_HOME environment variable points to the root directory of the Java installation.<\/span><span class=\"animating\"> To set the JAVA_HOME variable,<\/span><span class=\"animating\"> follow these steps:<\/span><\/p>\n<ol data-sourcepos=\"43:1-44:0\">\n<li class=\"animating\" data-sourcepos=\"43:1-44:0\"><span class=\"animating\">Run the following command to get the path of the desired Java version:<\/span><\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">sudo update-alternatives --config java\r\n<picture><img decoding=\"async\" class=\"wp-image-17002 webpexpress-processed\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-2.png\" sizes=\"(max-width: 859px) 100vw, 859px\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-2.png 859w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-2-600x219.png 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-2-768x281.png 768w\" alt=\"How To Install Java on Ubuntu using Apt-Get\" width=\"859\" height=\"314\" \/><\/picture><\/code><\/pre>\n<\/div>\n<ol start=\"2\" data-sourcepos=\"49:1-50:0\">\n<li class=\"animating\" data-sourcepos=\"49:1-50:0\"><span class=\"animating\">Open the following file:<\/span><\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">sudo nano \/etc\/environment<\/code><\/pre>\n<\/div>\n<ol start=\"3\" data-sourcepos=\"55:1-56:0\">\n<li class=\"animating\" data-sourcepos=\"55:1-56:0\"><span class=\"animating\">Add the following line to the file:<\/span><\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">JAVA_HOME=<span class=\"hljs-string\">\"YOUR_PATH\"<\/span><\/code><\/pre>\n<\/div>\n<ol start=\"4\" data-sourcepos=\"61:1-66:0\">\n<li class=\"animating\" data-sourcepos=\"61:1-62:0\">\n<p data-sourcepos=\"61:4-61:64\"><span class=\"animating\">Replace &#8220;YOUR_PATH&#8221; with the actual path of the Java version.<\/span><\/p>\n<\/li>\n<li class=\"animating\" data-sourcepos=\"63:1-64:0\">\n<p data-sourcepos=\"63:4-63:38\"><span class=\"animating\">Save the file and close the editor.<\/span><\/p>\n<\/li>\n<li class=\"animating\" data-sourcepos=\"65:1-66:0\">\n<p data-sourcepos=\"65:4-65:33\"><span class=\"animating\">Reload the configuration file:<\/span><\/p>\n<\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-built_in\">source<\/span> \/etc\/environment<\/code><\/pre>\n<\/div>\n<ol start=\"7\" data-sourcepos=\"71:1-72:0\">\n<li class=\"animating\" data-sourcepos=\"71:1-72:0\"><span class=\"animating\">Verify that the JAVA_HOME variable is set correctly:<\/span><\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-variable\">$JAVA_HOME<\/span><\/code><\/pre>\n<\/div>\n<p>Congrats!\u00a0Now you know how to install different versions of Java and how to switch between them.<\/p>\n<h2 data-sourcepos=\"77:1-77:14\"><strong class=\"animating\">Conclusion<\/strong><\/h2>\n<p data-sourcepos=\"79:1-79:98\"><span class=\"animating\">This guide has provided comprehensive instructions for installing and managing Java on Ubuntu 18.<\/span><span class=\"animating\">04,<\/span><span class=\"animating\"> 20.<\/span><span class=\"animating\">04,<\/span><span class=\"animating\"> and 22.<\/span><span class=\"animating\">04.<\/span><span class=\"animating\"> By following these steps,<\/span><span class=\"animating\"> you can easily install the desired Java version,<\/span><span class=\"animating\"> switch between different versions,<\/span><span class=\"animating\"> and set the JAVA_HOME environment variable.<\/span><\/p>\n<p data-sourcepos=\"79:1-79:98\">\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Java is a widely used programming language that is essential for developing and running applications on various platforms. This guide provides a step-by-step process for installing Java on Ubuntu 18.04, 20.04, and 22.04 using the Apt-Get package manager. Additionally, it explains how to switch between different Java versions and set the JAVA_HOME environment variable. Installation of the Default JRE\/JDK The&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"kbtopic":[1099],"kbtag":[1216,1109],"class_list":["post-3579","kb","type-kb","status-publish","hentry","kbtopic-tutorials","kbtag-java","kbtag-ubuntu"],"_links":{"self":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb\/3579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb"}],"about":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/types\/kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/comments?post=3579"}],"version-history":[{"count":7,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb\/3579\/revisions"}],"predecessor-version":[{"id":6308,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb\/3579\/revisions\/6308"}],"wp:attachment":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/media?parent=3579"}],"wp:term":[{"taxonomy":"kbtopic","embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kbtopic?post=3579"},{"taxonomy":"kbtag","embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kbtag?post=3579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}