Инсталиране на Java на Ubuntu

1. Общ преглед

В този урок ще представим различни методи за инсталиране на JDK на Ubuntu . След това ще сравним накратко методите. И накрая, ще покажем как да управлявате множество инсталации на Java в система на Ubuntu.

Като предпоставка за всеки метод се нуждаем

  • система на Ubuntu
  • да сте влезли като некорен потребител със sudo привилегии

Инструкциите, описани по-долу, са тествани на Ubuntu 18.10, 18.04 LTS, 16.04 LTS и 14.04 LTS. За Ubuntu 14.04 LTS има някои разлики, които са споменати в текста.

Моля, обърнете внимание, че както пакетите, които можете да изтеглите от OpenJDK и Oracle, така и пакетите, налични в хранилищата, се актуализират редовно. Точните имена на пакети вероятно ще се променят в рамките на няколко месеца, но основните методи за инсталиране ще останат същите.

2. Инсталиране на JDK 11

Ако искаме да използваме най-новата и най-добрата версия на JDK, често ръчната инсталация е пътят. Това означава да изтеглите пакет от OpenJDK или сайта на Oracle и да го настроите така, че да се придържа към конвенциите как apt настройва пакетите JDK.

2.1. Инсталиране на OpenJDK 11 Ръчно

Първо, нека изтеглим tar архива на наскоро издадения OpenJDK 11:

$ wget //download.java.net/java/ga/jdk11/openjdk-11_linux-x64_bin.tar.gz

И сравняваме сумата sha256 на изтегления пакет с тази, предоставена на сайта OpenJDK:

$ sha256sum openjdk-11_linux-x64_bin.tar.gz

Нека извлечем архива на tar :

$ tar xzvf openjdk-11_linux-x64_bin.tar.gz

След това нека преместим директорията jdk-11, която току-що извадихме, в поддиректория на / usr / lib / jvm . Най -склонни пакети, описани в следващия раздел също поставят своите JDKs в тази директория:

$ sudo mkdir /usr/lib/jvm $ sudo mv jdk-11 /usr/lib/jvm/openjdk-11-manual-installation/ 

Сега искаме да направим достъпни командите java и javac . Една от възможностите би била да се създадат символни връзки за тях, например в директорията / usr / bin . Но вместо това ще инсталираме алтернатива и за двамата. По този начин, ако някога пожелаем да инсталираме допълнителни версии на JDK, те ще играят добре заедно:

$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/openjdk-11-manual-installation/bin/java 1 $ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/openjdk-11-manual-installation/bin/javac 1

Нека проверим инсталацията:

$ java -version

Както можем да видим от резултата, ние наистина инсталирахме най-новата версия на OpenJDK JRE и JVM:

openjdk version "11" 2018-09-25 OpenJDK Runtime Environment 18.9 (build 11+28) OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode) 

Нека да разгледаме и версията на компилатора:

$ javac -version
javac 11

2.2. Инсталиране на Oracle JDK 11 Ръчно

Ако искаме да се уверим, че използваме най-новата версия на Oracle JDK, можем да следваме подобен работен процес за ръчно инсталиране, както при OpenJDK. За да изтеглите архива на tar за JDK 11 от уебсайта на Oracle, първо трябва да приемем лицензионно споразумение . По тази причина изтеглянето чрез wget е малко по-сложно, отколкото за OpenJDK:

$ wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie" \ //download.oracle.com/otn-pub/java/jdk/11.0.1+13/90cf5d8f270a4347a95050320eef3fb7/jdk-11.0.1_linux-x64_bin.tar.gz

Примерът по-горе изтегля пакета за 11.0.1 Точната връзка за изтегляне се променя за всяка малка версия.

Следващите стъпки са същите като за OpenJDK:

$ sha256sum jdk-11.0.1_linux-x64_bin.tar.gz $ tar xzvf jdk-11.0.1_linux-x64_bin.tar.gz $ sudo mkdir /usr/lib/jvm $ sudo mv jdk-11.0.1 /usr/lib/jvm/oracle-jdk-11-manual-installation/ $ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/oracle-jdk-11-manual-installation/bin/java 1 $ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/oracle-jdk-11-manual-installation/bin/javac 1

Проверката също е същата. Но резултатите показват, че този път сме инсталирали не OpenJDK, а Java (TM):

$ java -version
java version "11.0.1" 2018-10-16 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

И за компилатора:

$ javac -version
javac 11.0.1

2.3. Инсталиране на Oracle JDK 11 от PPA

В момента Oracle JDK 11 се предлага и в PPA (личен архив на пакети). Тази инсталация включва 2 стъпки: добавяне на хранилището към нашата система и инсталиране на пакета от хранилището чрез apt:

$ sudo add-apt-repository ppa:linuxuprising/java $ sudo apt update $ sudo apt install oracle-java11-installer

Стъпките за проверка трябва да показват същия резултат като след ръчната инсталация в раздел 2.2.1:

$ java -version
java version "11.0.1" 2018-10-16 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

И за компилатора:

$ javac -version
javac 11.0.1

В Ubuntu 14.04 LTS командата add-apt-repository не е налична по подразбиране. За да добавим хранилище, първо трябва да инсталираме пакета software-properties-common .

$ sudo apt update $ sudo apt install software-properties-common

След това можем да продължим с add-apt-repository, apt update и apt install, както е показано по-горе.

3. Инсталиране на JDK 8

3.1. Инсталиране на OpenJDK 8 на Ubuntu 16.04 LTS и по-нови

JDK 8 is an LTS version that has been around for a while. For this reason, we can find an up-to-date version of OpenJDK 8 in the “Main” repository on most of the supported Ubuntu versions. Of course, we can also head to the OpenJDK website, grab a package there, and install it the same way we've seen in the previous section.

But using the apt tooling and the “Main” repository provides some benefits. The “Main” repository is available by default on all Ubuntu systems. It's supported by Canonical — the same company that maintains Ubuntu itself.

Let's install OpenJDK 8 from the “Main” repository with apt:

$ sudo apt update $ sudo apt install openjdk-8-jdk

Now, let's verify the installation:

$ java -version

The result should list a Runtime Environment and a JVM:

openjdk version "1.8.0_181" OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.18.04.1-b13) OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

Let's check that the javac executable is available as well:

$ javac -version

Now we should see the same version number as shown above:

javac 1.8.0_181

3.2. Installing OpenJDK 8 on Ubuntu 14.04 LTS

On Ubuntu 14.04 LTS, the OpenJDK packages aren't available in the “Main” repository, so we'll install them from the openjdk-r PPA. As we've seen in section 2.3 above, the add-apt-repository command isn't available by default. We need the software-properties-common package for it:

$ sudo apt update $ sudo apt install software-properties-common $ sudo add-apt-repository ppa:openjdk-r/ppa $ sudo apt update $ sudo apt install openjdk-8-jdk

3.3. Installing Oracle JDK 8 from a PPA

The “Main” repository does not contain any proprietary software. If we want to install Oracle Java with apt, we'll have to use a package from a PPA. We've already seen how to install Oracle JDK 11 from the linuxuprising PPA. For Java 8, we can find the packages in the webupd8team PPA.

First, we need to add the PPA apt repository to our system:

$ sudo add-apt-repository ppa:webupd8team/java

Then we can install the package the usual way:

$ sudo apt update $ sudo apt install oracle-java8-installer

During the installation, we have to accept Oracle's license agreement. Let's verify the installation:

$ java -version

The output shows a Java(TM) JRE and JVM:

java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

We can also verify that the compiler has been installed:

$ javac -version
javac 1.8.0_181

4. Installing JDK 10

The versions Java 10 and Java 9 aren't supported anymore. You can install them manually, following similar steps as in section 2. You can grab the packages from:

  • //jdk.java.net/archive/
  • //www.oracle.com/technetwork/java/javase/archive-139210.html

Both sites contain the same warning:

These older versions of the JDK are provided to help developers debug issues in older systems. They are not updated with the latest security patches and are not recommended for use in production.

4.1. Installing OpenJDK 10 Manually

Let's see how to install OpenJDK 10.0.1:

$ wget //download.java.net/java/GA/jdk10/10.0.1/fb4372174a714e6b8c52526dc134031e/10/openjdk-10.0.1_linux-x64_bin.tar.gz $ sha256sum openjdk-10.0.1_linux-x64_bin.tar.gz $ tar xzvf openjdk-10.0.1_linux-x64_bin.tar.gz $ sudo mkdir /usr/lib/jvm $ sudo mv jdk-10.0.1 /usr/lib/jvm/openjdk-10-manual-installation/ $ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/openjdk-10-manual-installation/bin/java 1 $ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/openjdk-10-manual-installation/bin/javac 1 $ java -version $ javac -version

4.2. Installing Oracle JDK 10 Manually

As we've seen in section 2.2., in order to download a package from the Oracle website, we must accept a license agreement first. Contrary to the supported versions, we can't download the older Oracle JDKs via wget and a cookie. We need to head to //www.oracle.com/technetwork/java/javase/downloads/java-archive-javase10-4425482.html and download the tar.gz file. Afterward, we follow the familiar steps:

$ sha256sum jdk-10.0.2_linux-x64_bin.tar.gz $ tar xzvf jdk-10.0.2_linux-x64_bin.tar.gz $ sudo mkdir /usr/lib/jvm $ sudo mv jdk-10.0.2 /usr/lib/jvm/oracle-jdk-10-manual-installation/ $ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/oracle-jdk-10-manual-installation/bin/java 1 $ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/oracle-jdk-10-manual-installation/bin/javac 1 $ java -version $ javac -version

5. Installing JDK 9

5.1. Installing OpenJDK 9 Manually

Just like we saw above with OpenJDK 10.0.1, we download the OpenJDK 9 package via wget and set it up according to the conventions:

$ wget //download.java.net/java/GA/jdk9/9.0.4/binaries/openjdk-9.0.4_linux-x64_bin.tar.gz $ sha256sum openjdk-9.0.4_linux-x64_bin.tar.gz $ tar xzvf openjdk-9.0.4_linux-x64_bin.tar.gz $ sudo mkdir /usr/lib/jvm $ sudo mv jdk-9.0.4 /usr/lib/jvm/openjdk-9-manual-installation/ $ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/openjdk-9-manual-installation/bin/java 1 $ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/openjdk-9-manual-installation/bin/javac 1 $ java -version $ javac -version

5.2. Installing Oracle JDK 9 Manually

Once again, we use the same method as for JDK 10. We need to head to //www.oracle.com/technetwork/java/javase/downloads/java-archive-javase9-3934878.html and download the tar.gz file. Afterward, we follow the familiar steps:

$ sha256sum jdk-9.0.4_linux-x64_bin.tar.gz $ tar xzvf jdk-9.0.4_linux-x64_bin.tar.gz $ sudo mkdir /usr/lib/jvm $ sudo mv jdk-9.0.4 /usr/lib/jvm/oracle-jdk-9-manual-installation/ $ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/oracle-jdk-9-manual-installation/bin/java 1 $ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/oracle-jdk-9-manual-installation/bin/javac 1 $ java -version $ javac -version

6. Comparison

We've seen three different ways of installing a JDK on Ubuntu. Let's have a quick overview of each of them, pointing out the advantages and disadvantages.

6.1. “Main” Repository

This is the “Ubuntu native” way of installation. A big advantage is that we update the packages via the “usual apt workflow” with apt update and apt upgrade.

Furthermore, the “Main” repository is maintained by Canonical, which provides reasonably fast (if not immediate) updates. For example, OpenJDK versions 10.0.1 and 10.0.2 were both synced within a month of release.

6.2. PPA

PPAs are small repositories maintained by an individual developer or a group. This also means that the update frequency depends on the maintainer.

Packages from PPAs are considered riskier than the packages in the “Main” repository. First, we have to add the PPA explicitly to the system's repository list, indicating that we trust it. Afterward, we can manage the packages via the usual apt tooling (apt update and apt upgrade).

6.3. Manual Installation

We download the package directly from the OpenJDK or Oracle site. Although this method offers a great deal of flexibility, updates are our responsibility. If we want to have the latest and greatest JDK, this is the way to go.

7. Exploring Other Versions of JDKs

The examples in sections 2 and 3 reflect the current status on Ubuntu 18.04 LTS. Keep in mind that the JDKs and the corresponding packages are updated regularly. Thus it's useful to know how to explore our current possibilities.

In this section, we'll focus on surveying the OpenJDK packages in the “Main” repository. If we've already added a PPA with add-apt-repository, we can explore it in a similar manner with apt list and apt show.

To discover which PPAs are available, we can head to //launchpad.net/. If we don't find what we're looking for in the “Main” repository and in the PPAs, we'll have to fall back to manual installation.

If we'd like to use an unsupported version, even that can be difficult. As of this writing, we didn't find any packages for Java 9 or Java 10 on the OpenJDK and Oracle websites.

Let's see which other JDK packages exist in the “Main” repository:

$ apt list openjdk*jdk

On Ubuntu 18.04 LTS, we can choose between the two current LTS Java versions:

Listing... Done openjdk-11-jdk/bionic-updates,bionic-security,now 10.0.2+13-1ubuntu0.18.04.2 amd64 [installed,automatic] openjdk-8-jdk/bionic-updates,bionic-security 8u181-b13-0ubuntu0.18.04.1 amd64

It's also worth noting that although the package is called openjdk-11-jdk, as of this writing, it actually installs version 10.0.2. This is likely to change soon. We can see that if we inspect the package:

$ apt show openjdk-11-jdk

Let's have a look at the “Depends” section of the output. Note that these packages (e.g. a JRE) also get installed alongside openjdk-11-jdk:

Depends: openjdk-11-jre (= 10.0.2+13-1ubuntu0.18.04.2), openjdk-11-jdk-headless (= 10.0.2+13-1ubuntu0.18.04.2), libc6 (>= 2.2.5)

Let's explore which other packages we have at our disposal besides the default jdk package:

$ apt list openjdk-11*
Listing... Done openjdk-11-dbg/bionic-updates,bionic-security 10.0.2+13-1ubuntu0.18.04.2 amd64 openjdk-11-demo/bionic-updates,bionic-security 10.0.2+13-1ubuntu0.18.04.2 amd64 openjdk-11-doc/bionic-updates,bionic-updates,bionic-security,bionic-security 10.0.2+13-1ubuntu0.18.04.2 all openjdk-11-jdk/bionic-updates,bionic-security 10.0.2+13-1ubuntu0.18.04.2 amd64 openjdk-11-jdk-headless/bionic-updates,bionic-security 10.0.2+13-1ubuntu0.18.04.2 amd64 openjdk-11-jre/bionic-updates,bionic-security,now 10.0.2+13-1ubuntu0.18.04.2 amd64 [installed,automatic] openjdk-11-jre-headless/bionic-updates,bionic-security,now 10.0.2+13-1ubuntu0.18.04.2 amd64 [installed,automatic] openjdk-11-jre-zero/bionic-updates,bionic-security 10.0.2+13-1ubuntu0.18.04.2 amd64 openjdk-11-source/bionic-updates,bionic-updates,bionic-security,bionic-security 10.0.2+13-1ubuntu0.18.04.2 all

We may find some of these packages useful. For example, openjdk-11-source contains source files for the classes of the Java core API, while openjdk-11-dbg contains the debugging symbols.

Besides the openjdk-* family, there's the default-jdk package, that is worth exploring:

$ apt show default-jdk

At the end of the output, the description says:

“This dependency package points to the Java runtime, or Java compatible development kit recommended for this architecture…”

In the case of Ubuntu 18.04 LTS, it's the package openjdk-11-jdk at the moment.

8. Overview: Java Versions and Packages

Now, let's have a look at how different versions of Java could be installed on Ubuntu 18.04 LTS as of this writing:

Version OpenJDK Oracle Java
11 manual installation manual installation

oracle-java11-installer in the linuxuprising PPA

10 manual installation – not supported manual installation – not supported
9 manual installation – not supported manual installation – not supported
8 openjdk-8-jdk in the “Main” repository oracle-java8-installer in the webupd8team PPA

9. Multiple Java Versions on an Ubuntu System

The standard way for managing multiple versions of the same software on Ubuntu is via the Debian Alternatives System. Most of the time we create, maintain and display alternatives via the update-alternatives program.

When apt installs a JDK package, it automatically adds the entries for the alternatives. In the case of manual installation, we've seen how to add the alternatives for java and javac respectively.

Let's have a look at our alternatives:

$ update-alternatives --display java

On our test system, where we've installed two different versions of OpenJDK, the output lists both alternatives with their respective priorities:

java - auto mode link best version is /usr/lib/jvm/java-11-openjdk-amd64/bin/java link currently points to /usr/lib/jvm/java-11-openjdk-amd64/bin/java link java is /usr/bin/java slave java.1.gz is /usr/share/man/man1/java.1.gz /usr/lib/jvm/java-11-openjdk-amd64/bin/java - priority 1101 slave java.1.gz: /usr/lib/jvm/java-11-openjdk-amd64/man/man1/java.1.gz /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java - priority 1081 slave java.1.gz: /usr/lib/jvm/java-8-openjdk-amd64/jre/man/man1/java.1.gz

Now that we've seen our alternatives, we can also switch between them:

$ sudo update-alternatives --config java

Освен това получаваме интерактивен изход, където можем да превключваме между алтернативите чрез клавиатурата:

There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press  to keep the current choice[*], or type selection number:

Ако работим по множество приложения, написани в различни версии на Java, има вероятност да имаме нужда и от различни версии на друг софтуер (напр. Maven, някакъв сървър за приложения). В този случай може да помислим за използване на по-големи абстракции като контейнери на Docker.

10. Заключение

За да обобщим, в тази статия видяхме примери за инсталиране на JDK от хранилището „Main“, от PPA и ръчно. Накратко сравнихме тези три метода за инсталиране.

И накрая, видяхме как да управляваме множество инсталации на Java в системата на Ubuntu с алтернативи за актуализиране .

Като следваща стъпка може да е полезно да зададете променливата на средата JAVA_HOME .