+5 votes
337 views
How to install Java with APT command on Ubuntu 18.04

in Linux / Unix by (551k points)
reopened | 337 views

1 Answer

+3 votes
Best answer

1. How to install Java on Ubuntu 18.04
2. How to install specific versions of JDK on Ubuntu 18.04
3. Installation of Oracle JDK on Ubuntu 18.04
4. How to manage Java in Ubuntu 18.04
5. How to configure the JAVA_HOME environment variable in Ubuntu 18.04

Java is one of the necessary software elements to execute multiple actions at the software level, especially if we work with web servers, since Java, developed by Oracle, is a global standard designed to develop and distribute mobile applications, games, content based on web and many other software with which its omission can be a cause of execution errors.

Java advantages

Some of the advantages of using java are:

  • Write software on a defined platform and be able to run it virtually on another
  • Create programs that are capable of being executed in a browser and access available Web services
  • Combine various applications or services that make use of the Java language to create applications or services
  • Develop server applications for online forums, surveys, HTML form processing and much more all in a simple and secure way
  • Write powerful applications for mobile phones, remote processors, microcontrollers and much more.

Ubuntu 18.04 is the latest version of this versatile software and today we will learn to install Java there using the apt command.

Upgrade system

The first step to take is to update the system packages by executing the following:

 sudo apt update 

image

1. How to install Java on Ubuntu 18.04

Step 1


By default, Ubuntu 18.04 includes Open JDK, which is an open source variant of JRE and JDK, and this package will install OpenJDK 10 or 11 as follows:

  • Before September 2018, OpenJDK 10 will be installed
  • After September 2018, OpenJDK 11 will be installed

We will check if we have Java installed by running the following:

 java -version 

image

Step 2


As we see, at this moment we do not have Java, we proceed to the installation of OpenJDK by executing the following:

 sudo apt install default-jre 

image

Step 3


Enter the letter S to confirm the download and installation of OpenJDK, with which Java Runtime will be installed

 Environment (JRE). 

Once finished, we will check the Java version by running:

 java -version 

image

Step 4


Now it's time to install the Java Development Kit (JDK) which, in addition to the JRE, allows you to compile and run specific Java-based software. To install the JDK, we will execute the following command:

 sudo apt install default-jdk 

image

Step 5


Enter the letter S again to confirm the download and installation and once it is finished, we will validate the JDK version by running:

 javac -version 

image

2. How to install specific versions of JDK on Ubuntu 18.04

Step 1


It is possible that in some cases it is necessary to install only some special version of JDK and not the most recent one, we have the following options:

 OpenJDK 8 

Java 8 is the current version of long-term support and is compatible even with many applications. To install OpenJDK 8, we will execute the following command:

 sudo apt install openjdk-8-jdk 

image

Step 2


Finally we can check this version by running:

 java -version 

3. Installation of Oracle JDK on Ubuntu 18.04

Step 1


If the objective is to install Oracle JDK, which is the official version distributed by Oracle, it will be necessary to add a new package repository for the version that we want to use, in the case of Java 8 which is the LTS version, we will execute what next:

 sudo add-apt-repository ppa: webupd8team / java 

image

Step 2


Press Enter to confirm the download. Then we update the PPA packages by running:

 sudo apt update 

Step 3


Finally we install JDK by running the following command:

 sudo apt install oracle-java8-installer 

image

Enter the letter S to confirm the action.

4. How to manage Java in Ubuntu 18.04



It is possible to have multiple Java installations on a server. We can configure which version is the default to use on the command line using the update-alternative command like this:

 sudo update-alternatives --config java 

image

There we select the number associated with the version of Java that we want to use as default, or press ENTER to leave the current setting.

This option is also possible with other Java components such as javac:

 sudo update-alternatives --config javac 

5. How to configure the JAVA_HOME environment variable in Ubuntu 18.04


Some programs written with Java make use of the JAVA_HOME environment variable to determine the installation location of Java.

Step 1


We can set this environment variable, but first we will define where Java is installed, for this we will use the update-alternatives command like this:

 sudo update-alternatives --config java 

image

Step 2


There we find the Route column where the route where java is installed in Ubuntu 18.04 is specified.
Usually these routes are:

 OpenJDK 11: / usr / lib / jvm / java-11-openjdk-amd64 / bin / java. 
 OpenJDK 8: / usr / lib / jvm / java-8-openjdk-amd64 / jre / bin / java. 
 Oracle Java 8: / usr / lib / jvm / java-8-oracle / jre / bin / java. 

Step 3


We will copy the desired installation path and then we will open / etc / environment using nano or some text editor like this:

 sudo nano / etc / environment 

In the deployed file we will paste the following line at the end:

 JAVA_HOME = "/ usr / lib / jvm / java-11-openjdk-amd64 / bin / java" 

image

Step 4


We save the changes using the Ctrl + O keys and exit the editor using the Ctrl + X keys. We must apply the changes using the following command:

 source / etc / environment 

We confirm the route by executing the following command:

 echo $ JAVA_HOME 

image

We have learned to install Java and configure some of its parameters in Ubuntu 18.04.


by (3.5m points)
edited by

Related questions

+5 votes
1 answer
asked Oct 15, 2019 in Linux / Unix by backtothefuture (551k points) | 194 views
+3 votes
1 answer
asked Nov 6, 2020 in Linux / Unix by backtothefuture (551k points) | 764 views
+4 votes
1 answer
asked Mar 19, 2020 in Linux / Unix by backtothefuture (551k points) | 942 views
+5 votes
1 answer
asked Oct 15, 2019 in Linux / Unix by backtothefuture (551k points) | 217 views
+5 votes
1 answer
asked Oct 15, 2019 in Linux / Unix by backtothefuture (551k points) | 270 views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,634 questions
10,766 answers
510 comments
3 users