How to Install Java on Ubuntu 2021

How to Install Java on Ubuntu 2021

Hello Readers, In this “How to Install Java on Ubuntu 2021” blog post, we will learn to install Java on Ubuntu or Linux flavors like Linux mint using update-alternatives.

Introduction

Every beginner has spent many hours in Java installation in Linux, Java installation in Ubuntu, and Windows is sometimes a complex job. I tried to cover all steps with a command description. The purpose of the blog post is to help developers to install java quickly.

What is update-alternatives 

Use of update-alternatives

  1. Using the update-alternatives command, you can create a new executable java (/usr/local/bin/java) and add all the available java versions to the alternatives database. 
  2. You can easily set which version of java to use by default. You can switch between the java versions very easily.

Steps for Install Java on Ubuntu

1. Download the JDK 8 from the Oracle website

For Java 16: https://www.oracle.com/java/technologies/javase-jdk16-downloads.html

For Java 8: Recommend buying the Oracle subscription and access java 7 or 8 from the oracle website.

Liferay CDN: https://files-cdn.liferay.com/mirrors/download.oracle.com/otn-pub/java/jdk/8u121-b13/

  • For 64bit: jdk-8u121-linux-x64.tar.gz
  • For 32bit: jdk-8u121-linux-i586.tar.gz

2. Create a folder and change the directory

sudo mkdir /usr/lib/jvm

cd /usr/lib/jvm

If you are the same location like /usr/lib/jvm

3. Extract downloaded tar file to specific location 

sudo tar -xvzf ~/Downloads/jdk-8u221-linux-x64.tar.gz

If you are not in the same location in the terminal. Move JDK folder through the below ‘mv’ linux command

sudo mv ~/Downloads/jdk1.8.0_221 /usr/lib/jvm

4. Set JAVA_HOME in .bashrc file 

Configure environment variables for java using the default ‘nano’ editor. Open bashrc file using the below command.

sudo nano ~/.bashrc

# For Java Home: Copy-paste below two lines of code and after press CTRL+X. After Press Y for saving the changes. Well done.

export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_221
export PATH=${PATH}:${JAVA_HOME}/bin

5. Reload configuration using the source command 

[Important step] – Below command will reflect changes in the same terminal, you don’t need to restart the system to see reflected changes.

source ~/.bashrc

6. Install update-alternatives for java and javac

Syntax: 

sudo update-alternatives --install <link> <name> <path> <priority>

Commands:

sudo update-alternatives --install "/usr/lib/java" "java" "/usr/lib/jvm/jdk1.8.0_221/bin/java" 0
sudo update-alternatives --install "/usr/lib/javac" "javac" "/usr/lib/jvm/jdk1.8.0_221/bin/javac" 0

* For check and List down installed java and javac [here you can check installed java versions]

sudo update-alternatives --config java

7. Set update-alternatives for java and javac

sudo update-alternatives --set java /usr/lib/jvm/jdk1.8.0_221/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/jdk1.8.0_221/bin/javac

8. List and verify java versions 

sudo update-alternatives --list java
sudo update-alternatives --list javac

9. Verify Java and javac installation

Great!!! One Final step, Check Java, javac version and, JAVA_HOME as below commands.

java --version
javac --version
echo $JAVA_HOME

Output:

java 1.8.0_221

javac 1.8.0_221

/usr/lib/jvm/jdk1.8.0_221

Conclusion

In the conclusion, I tried and covered java installation on ubuntu in a quick and easy way. If you face any issues, please share in the comment section.

Don’t forget to share with friends on social profiles. feedback is a great thing for the improvements, share your feedback in the comment section.

About Author

I am Vinod Luhar (Software Engineer, Book Reader), founder of vinodluhar.com. I like to write, code, and contribute knowledge to the coding community. I love to read books, do yoga, and exercise.

Thanks. Have a great day.

Leave a Comment

Share via
Copy link
Powered by Social Snap