Skip to content

Instantly share code, notes, and snippets.

@rcarmonad
Last active April 11, 2023 15:30
Show Gist options
  • Save rcarmonad/3818e9b02b617f068abe5b72dade9a40 to your computer and use it in GitHub Desktop.
Save rcarmonad/3818e9b02b617f068abe5b72dade9a40 to your computer and use it in GitHub Desktop.
Manually install java on Ubuntu

Install java on ubuntu /tested in 16.04, 18.04, 20.04

Java 6

For java 6, download bin file from oracle archive

Create a folder:

$ sudo mkdir -p /opt/java

Move bin file to that folder. Then:

$ sudo chmod +x jdk-6u45-linux-x64.bin
$ sudo ./jdk-6u45-linux-x64.bin

Now, you have to use update-alternatives to install java

$ sudo update-alternatives --install /usr/bin/java java /opt/java/jdk1.6.0_45/bin/java 0
$ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.6.0_45/bin/javac 0

Java 7

Donwload the java 7 file from the Oracle Archive and place it into de /opt/java folder. This is a tar file, so, we have to untar manually:

$ sudo tar zxvf jdk-7u80-linux-x64.tar.gz

Finally, we have to do the same with update-alternatives:

$ sudo update-alternatives --install /usr/bin/java java /opt/java/jdk1.7.0_80/bin/java 0
$ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.7.0_80/bin/javac 0

Java 8

Donwload the java 8 file from the Oracle Archive and place it into de /opt/java folder. This is a tar file, so, we have to untar manually:

$ sudo tar zxvf jdk-8u241-linux-x64.tar.gz

Finally, we have to do the same with update-alternatives:

$ sudo update-alternatives --install /usr/bin/java java /opt/java/jdk1.8.0_241/bin/java 0
$ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.8.0_241/bin/javac 0

Change active java

Using update-alternatives we can choose the installation that is in use:

$ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                             Priority   Status
------------------------------------------------------------
* 0            /opt/java/jdk1.6.0_45/bin/java    0         auto mode
  1            /opt/java/jdk1.6.0_45/bin/java    0         manual mode
  2            /opt/java/jdk1.7.0_80/bin/java    0         manual mode
  3            /opt/java/jdk1.8.0_241/bin/java   0         manual mode

Press <enter> to keep the current choice[*], or type selection number: 

You choose the selection typing the number. Then you can test it:

$ java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)

Remove alternative

$ sudo update-alternatives --remove java /path/to/java
$ sudo update-alternatives --remove javac /path/to/javac

Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment