{"id":7434,"date":"2023-03-14T15:02:30","date_gmt":"2023-03-14T15:02:30","guid":{"rendered":"https:\/\/blog.ssdnodes.com\/blog\/?p=7434"},"modified":"2025-05-18T13:16:24","modified_gmt":"2025-05-18T13:16:24","slug":"how-to-install-java-and-its-virtual-machine-on-ubuntu-22-04","status":"publish","type":"post","link":"https:\/\/www.ssdnodes.com\/blog\/how-to-install-java-and-its-virtual-machine-on-ubuntu-22-04\/","title":{"rendered":"How To Install Java and its Virtual Machine on Ubuntu 22.04"},"content":{"rendered":"<p>Java is a high-level, object-oriented programming language. The JVM or Java\u2019s virtual machine, is a virtual machine that enables a computer to run Java and programs that are written in a language that can be compiled to Java bytecode such as Scala and Kotlin programs. Java and JVM are required for many DevOps tools such as Jenkins, Cassandra, and Apache Tomcat.<\/p>\n<\/p>\n<p>In this tutorial, you will learn how to install the Java Runtime Environment (JRE) and the Java Developer Kit (JDK) using <code>apt<\/code>. The JDK provides software tools to develop Java programs. The JRE executes the programs you write in Java. In this tutorial, you will install OpenJDK, which is an open-source implementation of Java. However, you can also install the Oracle JDK, which is the original version. Both OpenJDK and OracleJDK are developed and maintained by Oracle, the developers of Java, and both are functionally identical since Java 11.<\/p>\n<h1>Prerequisites<\/h1>\n<ul>\n<li>Basic knowledge of the Linux command line.<\/li>\n<li>An Ubuntu 22.04 server with a non-root user with <code>sudo<\/code> privileges. You can get affordable, and powerful Ubuntu servers from <a href=\"https:\/\/www.ssdnodes.com\/\" target=\"external\">our website<\/a>, and you can check out our <a href=\"https:\/\/www.ssdnodes.com\/blog\/tutorial-setting-up-and-securing-ssh-based-authentication\/\">How to access your server using SSH<\/a> guide to learn how to access your server and create a <code>sudo<\/code> user.<\/li>\n<\/ul>\n<h1>Updating the Package Cache<\/h1>\n<p>Start by updating the <a href=\"https:\/\/www.ssdnodes.com\/blog\/build-your-ecommerce-platform\/ \">packages<\/a> in the package manager cache to the latest available versions using the following command:<\/p>\n<pre><code>sudo apt update<\/code><\/pre>\n<h1>Installing the JRE\/JDK using Apt<\/h1>\n<p>To install the JRE from the OpenJDK version of Java, use the following command:<\/p>\n<pre><code>sudo apt install default-jre<\/code><\/pre>\n<p>This will install the default JRE in the Ubuntu repositories, and will allow you to execute Java programs.<\/p>\n<p>Once the installation is finished, check your Java version to ensure that the JRE was successfully installed:<\/p>\n<pre><code>java -version<\/code><\/pre>\n<p>You should receive an output similar to the following:<\/p>\n<pre><code>openjdk version &quot;11.0.17&quot; 2022-10-18\nOpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu222.04)\nOpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu222.04, mixed mode, sharing)<\/code><\/pre>\n<p>Next, install the JDK to compile and run some Java software that requires it. To do so, use the following command:<\/p>\n<pre><code>sudo apt install default-jdk<\/code><\/pre>\n<p>To ensure that the JDK is successfully installed, check the version of the Java compiler <code>javac<\/code>:<\/p>\n<pre><code>javac -version<\/code><\/pre>\n<p>You should receive an output similar to the following:<\/p>\n<pre><code>javac 11.0.17<\/code><\/pre>\n<p>With this, you now have Java installed on your system with OpenJDK. If you are looking to install the Official Oracle JDK, follow the next option.<\/p>\n<h1>(Alternative Option) Installing Oracle JDK 11<\/h1>\n<p>If you want to install and use OracleJDK, and not OpenJDK, or switch between the two depending on your requirements, use the following instructions.<\/p>\n<p>First, install some required packages:<\/p>\n<pre><code>sudo apt install -y libc6-x32 libc6-i386<\/code><\/pre>\n<p>Go to the <a href=\"https:\/\/www.oracle.com\/java\/technologies\/downloads\" target=\"_blank\" rel=\"noopener\">Oracle downloads page<\/a> and download the <code>x64 Debian Package<\/code> version using <code>wget<\/code>:<\/p>\n<pre><code>wget https:\/\/download.oracle.com\/java\/19\/latest\/jdk-19_linux-x64_bin.deb<\/code><\/pre>\n<p>Once your <code>.deb<\/code> package is downloaded, install it using <code>apt<\/code>:<\/p>\n<pre><code>sudo apt install .\/jdk-19_linux-x64_bin.deb<\/code><\/pre>\n<p>Once installed, use the following commands to set these new packages for <code>update-alternatives<\/code>:<\/p>\n<pre><code>sudo update-alternatives --install \/usr\/bin\/java java \/usr\/lib\/jvm\/jdk-19\/bin\/java 100\nsudo update-alternatives --install \/usr\/bin\/javac javac \/usr\/lib\/jvm\/jdk-19\/bin\/javac 100<\/code><\/pre>\n<h1>Managing Java Alternatives<\/h1>\n<p>If you've installed one or more Java versions, then you can use the <code>update-alternatives<\/code> command to set the default version for your system.<\/p>\n<p>To select your default Java installation, run the following command:<\/p>\n<pre><code>sudo update-alternatives --config java<\/code><\/pre>\n<p>You'll receive a list of the Java versions you currently have installed on your system:<\/p>\n<pre><code>There are 2 choices for the alternative java (providing \/usr\/bin\/java).\n\n  Selection    Path                                         Priority   Status\n------------------------------------------------------------\n* 0            \/usr\/lib\/jvm\/java-11-openjdk-amd64\/bin\/java   1111      auto mode\n  1            \/usr\/lib\/jvm\/java-11-openjdk-amd64\/bin\/java   1111      manual mode\n  2            \/usr\/lib\/jvm\/jdk-19\/bin\/java                  100       manual mode\n\nPress &lt;enter&gt; to keep the current choice[*], or type selection number: <\/code><\/pre>\n<p>Press <code>Enter<\/code> to leave the current option, or type the number of the Java version you want to set.<\/p>\n<p>For example, type in <code>2<\/code> and press <code>Enter<\/code> to set the OracleJDK version.<\/p>\n<p>You can also use <code>update-alternatives<\/code> to configure the default version of your Java compiler:<\/p>\n<pre><code>sudo update-alternatives --config javac<\/code><\/pre>\n<p>You should receive an output similar to the following:<\/p>\n<pre><code>There are 2 choices for the alternative javac (providing \/usr\/bin\/javac).\n\n  Selection    Path                                          Priority   Status\n------------------------------------------------------------\n* 0            \/usr\/lib\/jvm\/java-11-openjdk-amd64\/bin\/javac   1111      auto mode\n  1            \/usr\/lib\/jvm\/java-11-openjdk-amd64\/bin\/javac   1111      manual mode\n  2            \/usr\/lib\/jvm\/jdk-19\/bin\/javac                  100       manual mode<\/code><\/pre>\n<p>Again, choose <code>2<\/code> to use the OracleJDK compiler as your default compiler and press <code>Enter<\/code>.<\/p>\n<p>To verify that Java was installed, check out its version:<\/p>\n<pre><code>java -version<\/code><\/pre>\n<p>You should receive an output similar to the following:<\/p>\n<pre><code>java version &quot;19.0.1&quot; 2022-10-18\nJava(TM) SE Runtime Environment (build 19.0.1+10-21)\nJava HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)<\/code><\/pre>\n<p>To verify that the Java compiler was installed, check out its version:<\/p>\n<pre><code>javac -version<\/code><\/pre>\n<p>You should receive an output similar to the following:<\/p>\n<pre><code>javac 19.0.1<\/code><\/pre>\n<h1>Configuring the Java Home Directory Environment Variable<\/h1>\n<p>Now that you've installed Java on your Ubuntu server, you need to to set up the <code>$JAVA_HOME<\/code> environment variable, which is a variable used by many Java packages and software programs written in Java to determine where Java is installed.<\/p>\n<p>First run the <code>update-alternatives<\/code> to get the path of your current Java installation:<\/p>\n<pre><code>sudo update-alternatives --config java<\/code><\/pre>\n<p>This will list paths of your different Java installations, which will be similar to the following:<\/p>\n<pre><code>  0            \/usr\/lib\/jvm\/java-11-openjdk-amd64\/bin\/java   1111      auto mode\n  1            \/usr\/lib\/jvm\/java-11-openjdk-amd64\/bin\/java   1111      manual mode\n  2            \/usr\/lib\/jvm\/jdk-19\/bin\/java                  100       manual mode<\/code><\/pre>\n<p>Here, OpenJDK is located at <code>\/usr\/lib\/jvm\/java-11-openjdk-amd64\/bin\/java<\/code> and OracleJDK is located at <code>\/usr\/lib\/jvm\/jdk-19\/bin\/java<\/code>.<\/p>\n<p>Choose and copy a path depending on your requirements, then open the <code>\/etc\/environment<\/code> file:<\/p>\n<pre><code>sudo nano \/etc\/environment<\/code><\/pre>\n<p>Add your path at the end of this file and assign it to a variable called <code>JAVA_HOME<\/code>, excluding the <code>\/bin\/java<\/code> portion, like so:<\/p>\n<pre><code>JAVA_HOME=&quot;\/usr\/lib\/jvm\/java-11-openjdk-amd64&quot;<\/code><\/pre>\n<p>Save and close <code>\/etc\/environment<\/code>.<\/p>\n<p>Reload <code>\/ect\/environment<\/code>:<\/p>\n<pre><code>source \/etc\/environment<\/code><\/pre>\n<p>Now check out the value of your Java Home variable:<\/p>\n<pre><code>echo $JAVA_HOME<\/code><\/pre>\n<p>This should print the path you've set:<\/p>\n<pre><code>\/usr\/lib\/jvm\/java-11-openjdk-amd64<\/code><\/pre>\n<h2>Congrats<\/h2>\n<p>You have now learned how to install Java on your Ubuntu system using two methods, one where you install the OpenJDK version available in your Ubuntu repositories, and one where you install the official OracleJDK. You've also learned how to install both versions and alternate between them as your requirements change.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is a high-level, object-oriented programming language. The JVM or Java\u2019s virtual machine, is a virtual machine that enables a computer to run Java and programs that are written in a language that can be compiled to Java bytecode such as Scala and Kotlin programs. Java and JVM are required for many DevOps tools such  &#8230;<\/p>\n","protected":false},"author":19,"featured_media":7915,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[18,30],"tags":[186,187,185],"class_list":["post-7434","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-tutorials","tag-java","tag-jvm","tag-ubuntu"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/7434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/comments?post=7434"}],"version-history":[{"count":6,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/7434\/revisions"}],"predecessor-version":[{"id":13017,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/7434\/revisions\/13017"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/media\/7915"}],"wp:attachment":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/media?parent=7434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/categories?post=7434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/tags?post=7434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}