{"id":16995,"date":"2023-05-15T01:54:30","date_gmt":"2023-05-14T23:54:30","guid":{"rendered":"https:\/\/contabo.com\/blog\/?p=16995"},"modified":"2023-08-11T05:20:36","modified_gmt":"2023-08-11T03:20:36","slug":"how-to-install-java-on-ubuntu-using-apt-get","status":"publish","type":"post","link":"https:\/\/contabo.com\/blog\/how-to-install-java-on-ubuntu-using-apt-get\/","title":{"rendered":"How To Install Java on Ubuntu 18.04, 20.04 &#038; 22.04 using Apt-Get"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-install-Java-on-Ubuntu-with-Apt-Get1.jpg\" alt=\"How To Install Java on Ubuntu using Apt-Get\" class=\"wp-image-16996\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-install-Java-on-Ubuntu-with-Apt-Get1.jpg 1200w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-install-Java-on-Ubuntu-with-Apt-Get1-600x315.jpg 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-install-Java-on-Ubuntu-with-Apt-Get1-768x403.jpg 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><a href=\"https:\/\/www.java.com\/en\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Java <\/a>is a widely used programming language that has become an essential tool for developing and running applications on various platforms. If you are a developer or a system administrator using Ubuntu 20.04, you may need to install Java on your machine to run Java-based applications or develop Java programs. Fortunately, Ubuntu 20.04 provides an easy and convenient way to install Java using the Apt-Get package manager. In this article, we will walk you through the step-by-step process of installing Java on Ubuntu 20.04 using Apt-Get, so you can get started with your Java projects right away. The process <strong>is the same<\/strong> for <strong>Ubuntu 18.04 <\/strong>and <strong>Ubuntu 22.04<\/strong>.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-installation-of-the-default-jre-jdk\">Installation of the Default JRE\/JDK<\/h2>\n\n\n\n<p>If you are planning to use the current version of Java, which at the time of this writing is Openjdk 11.0.18 you can use the following two commands to either install the default JRE which stands for Java Runtime Envoirement or the JDK which stands for Java Developement Kit and is usually needed to compile Java applications.&nbsp;<\/p>\n\n\n\n<p>For the default JRE the command is:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install default-jre<\/code><\/pre>\n\n\n\n<p>And for the default JDK the command is:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install default-jdk<\/code><\/pre>\n\n\n\n<p><em>Pro-Tip:<\/em> The JDK always contains the JRE, so you do nothing wrong when always going with the JDK apart from its larger file size.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-installing-other-java-versions\">Installing other Java Versions<\/h2>\n\n\n\n<p>It may happen that you need older versions of Java for your project. Because the commands shown above only install the latest version of either the JDK or the JRE, you need to specifically install the older versions. But no worries! We got you covered!&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-installing-java-8-jdk-amp-jre\">Installing Java 8 (JDK &amp; JRE)<\/h3>\n\n\n\n<p>To install Java 8 JDK use this command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install openjdk-8-jdk \u2013y<\/code><\/pre>\n\n\n\n<p>And to install Java 8 JRE use this command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install openjdk-8-jre \u2013y<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-installing-java-16-jdk-amp-jre-nbsp\">Installing Java 16 (JDK &amp; JRE)&nbsp;<\/h3>\n\n\n\n<p>Installing the Java 16 JDK or JRE are again very easy and can be done with one single command.&nbsp;<\/p>\n\n\n\n<p>For Java 16 JDK this command would be:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install openjdk-16-jdk \u2013y&nbsp;<\/code><\/pre>\n\n\n\n<p>And for Java 16 JRE it would be:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install openjdk-16-jre \u2013y&nbsp;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-installing-java-17-jdk-amp-jre-nbsp\">Installing Java 17 (JDK &amp; JRE)&nbsp;<\/h3>\n\n\n\n<p>Ok&#8230; now it&#8217;s getting repetitive but you may have guessed it: Java 17 is installed in the same way, but with \u201c17\u201d as the version number.&nbsp;&nbsp;<\/p>\n\n\n\n<p>So to install Java 17 JDK use this command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install openjdk-17-jdk \u2013y&nbsp;<\/code><\/pre>\n\n\n\n<p>And for Java 17 JRE use this command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install openjdk-17-jre \u2013y&nbsp;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-switching-between-different-java-versions-nbsp\">Switching between different Java Versions&nbsp;<\/h2>\n\n\n\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. This is especially important when you want to set up your own Minecraft Server and you are often switching between Minecraft versions. If you want to learn more about how to run your own Minecraft Server, read our beginner-friendly Minecraft-Server guide <a href=\"https:\/\/contabo.com\/blog\/minecraft-server-setup-ultimate-guide\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.&nbsp;<\/p>\n\n\n\n<p>You can find out which version is currently active with this command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java -version<\/code><\/pre>\n\n\n\n<p>The output will look simillar to this:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"853\" height=\"186\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image.png\" alt=\"How To Install Java on Ubuntu using Apt-Get\" class=\"wp-image-16999\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image.png 853w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-600x131.png 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-768x167.png 768w\" sizes=\"auto, (max-width: 853px) 100vw, 853px\" \/><\/figure>\n\n\n\n<p>As you can see, although I\u2019ve installed multiple versions of Java, Java 17 is currently active. To switch between the active versions use this command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo update-alternatives --config java&nbsp;<\/code><\/pre>\n\n\n\n<p>Then this table will show up:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"859\" height=\"269\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-1.png\" alt=\"How To Install Java on Ubuntu using Apt-Get\" class=\"wp-image-16998\" 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\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" \/><\/figure>\n\n\n\n<p>If you want to switch to a different version of java, just type in the \u201cSelection\u201d-Number of the corresponding version of Java (e.g. \u201c4\u201d for Java 8) and hit enter.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-set-the-java-home-environment-variable\">How to set the \u201cJAVA_HOME\u201d environment Variable<\/h2>\n\n\n\n<p>The JAVA_HOME variable is an environment variable that points to the root directory of the Java installation on your system. It is used by Java-based applications to locate the Java installation and execute Java commands.&nbsp;<\/p>\n\n\n\n<p>Setting the JAVA_HOME variable is important for development environments or when running applications that require a specific Java version. For example, if you have multiple Java versions installed on your system, setting the JAVA_HOME variable to the desired version can ensure that the correct Java version is used when running an application.&nbsp;<\/p>\n\n\n\n<p>To set the JAVA_HOME variable, follow these steps:&nbsp;<\/p>\n\n\n\n<p>Run this command to get an overview of every installed version and their path:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo update-alternatives --config java&nbsp;<\/code><\/pre>\n\n\n\n<p>This window will pop up again:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"859\" height=\"314\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/image-2.png\" alt=\"How To Install Java on Ubuntu using Apt-Get\" class=\"wp-image-17002\" 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\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" \/><\/figure>\n\n\n\n<p>Here you can see every installed Java version and the corresponding path, e.g. the Path for the Java 17 JDK is \u201c\/usr\/lib\/jvm\/java-17-openjdk-amd64\/bin\/java\u201d&nbsp;<\/p>\n\n\n\n<p>If you don\u2019t want to change anything here and just wanted to find the path to copy it, press <strong>[Enter]<\/strong> to close the menu again.&nbsp;<\/p>\n\n\n\n<p>Now open the file, which will contain the variable with this command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/environment&nbsp;<\/code><\/pre>\n\n\n\n<p>And add the following line:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JAVA_HOME=\"YOUR_PATH\"&nbsp;<\/code><\/pre>\n\n\n\n<p>(Of course, replace \u201cYOUR PATH\u201d with the actual path of the java version)&nbsp;<\/p>\n\n\n\n<p>Save the file with <strong>[CTRL]+O<\/strong> and close the editor with <strong>[CTRL]+X<\/strong>.&nbsp;<\/p>\n\n\n\n<p>For the changes to take effect, reload the config with this command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>source \/etc\/environment&nbsp;<\/code><\/pre>\n\n\n\n<p>To verify that everything worked, use this command to test it:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo $JAVA_HOME&nbsp;<\/code><\/pre>\n\n\n\n<p>It should return the path you\u2019ve just entered.&nbsp;<\/p>\n\n\n\n<p><strong>Congrats!<\/strong> Now you know how to install different versions of Java and how to switch between them.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this comprehensive step-by-step guide you will learn how to install different versions of Java on your Ubuntu server using Apt-Get.<\/p>\n","protected":false},"author":50,"featured_media":16996,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[],"ppma_author":[1491],"class_list":["post-16995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"uagb_featured_image_src":{"full":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-install-Java-on-Ubuntu-with-Apt-Get1.jpg",1200,630,false],"thumbnail":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-install-Java-on-Ubuntu-with-Apt-Get1-150x150.jpg",150,150,true],"medium":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-install-Java-on-Ubuntu-with-Apt-Get1-600x315.jpg",600,315,true],"medium_large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-install-Java-on-Ubuntu-with-Apt-Get1-768x403.jpg",768,403,true],"large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-install-Java-on-Ubuntu-with-Apt-Get1.jpg",1200,630,false],"1536x1536":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-install-Java-on-Ubuntu-with-Apt-Get1.jpg",1200,630,false],"2048x2048":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-install-Java-on-Ubuntu-with-Apt-Get1.jpg",1200,630,false]},"uagb_author_info":{"display_name":"Tobias Mildenberger","author_link":"https:\/\/contabo.com\/blog\/author\/tobias\/"},"uagb_comment_info":0,"uagb_excerpt":"In this comprehensive step-by-step guide you will learn how to install different versions of Java on your Ubuntu server using Apt-Get.","authors":[{"term_id":1491,"user_id":50,"is_guest":0,"slug":"tobias","display_name":"Tobias Mildenberger","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/077178d5dce6c3d4c0c0396857a7e544bfdf8adf04145fff5160b33a22e28b1f?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/16995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/users\/50"}],"replies":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/comments?post=16995"}],"version-history":[{"count":5,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/16995\/revisions"}],"predecessor-version":[{"id":17269,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/16995\/revisions\/17269"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media\/16996"}],"wp:attachment":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media?parent=16995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/categories?post=16995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/tags?post=16995"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=16995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}