Changing Java versions on Debian based Linux distros
I had a pretty hard time figuring out how to change the default Java version on my Debian based Pop OS. I read many articles and finally figured out how to do it for a single user on the system as well as for all the users. I will try to keep myself brief and summarize the approach.
Retrieve all the available Java versions
Firstly we need to see which Java versions are installed on the system. To get all versions as well as their paths, we need to issue the following command:
sudo update-alternatives --config java
“sudo” is necessary if the terminal is opened without admin privileges. The above command will list all Java versions as below:
The Java version with the asterisk(*) sign is the default version for this user. My current default version was Java 11 but I wanted Java 8 as my default Java version so I input selection “2” which points to Java 8 path. We can verify the modification by issuing the below command:
java -version
However, we still need to update the JAVA_HOME and PATH environment variables so that projects/IDEs can use the appropriate Java version.
From the screenshot above, we can see that although java -version shows Java 8 being used, however the JAVA_HOME environment variable is still set to Java 11 path.
Modifying environment variables for single user
To modify the environment variables for a single user, we need to open the .bashrc file in privileged mode. It can be done by issuing the below command:
sudo nano .bashrc
You can use Vim, gedit or any other text editor of your choice. Add the following two lines at the end of the file and then close the editor.
The paths can vary depending upon the Java version you wish to choose. To bring the changes in effect, issues the below command:
source ~/.bashrc
Verify the changes by echoing the JAVA_HOME environment variable using echo $JAVA_HOME respectively.
Modifying environment variables for all users
To modify environment variables for all users, we need to modify the profile file located in the /etc directory. Open /etc/profile using following commands:
sudo nano /etc/profile
After opening the editor, add the same two lines at the end of the file as before.
Issue the below command to bring changes into effect.
source /etc/profile
This command will modify the profile file and the settings will be updated for all the users on the system. You can verify the changes by issuing the echo $JAVA_HOME command
Thanks for reading my first Substack post. You can connect with me on below handles: