How to Add Environment Variable in Ubuntu Linux Operating System

Posted on

This is an article specified to add an environment variable in a Linux operating system where the case is specifically executed in an Ubuntu Linux operating system. The main purpose is to avoid the warning or it might be considered as an error message, where the warning or the message shown is triggered upon executing ‘mvn compile’. In this context, as an example, there is an environment variable named ‘JAVA_HOME’ where it is needed to execute certain command related with maven which is ‘mvn’. If the environment variable is not defined, it will trigger warning or error. For general information, ‘mvn compile’ is a tool command which is originally exist in maven. In the command itself, ‘mvn’ is for ‘maven’ and the additional parameter, ‘compile’ is for maven tool to compile the resources defined in the pom.xml which can be considered as the maven configuration file for the project folder. In the command execution, there is a need for defining the JAVA_HOME environment variable.

In order to solve the above problem or any problem related to the requirement of JAVA_HOME environment variable to be defined, below are the steps taken :

1. Just edit file .bashrc in the home folder of the user. Inside the file, just define the environment variable by inserting the following lines of script :

export JAVA_HOME="/opt/jdk1.8.0_172"
export PATH=$PATH:$JAVA_HOME

export : it is used as a command in bash script or bash prompt for exporting environment variables
JAVA_HOME : the name of the environment variable
"/opt/jdk1.8.0_172" : the value of the environment variable named 'JAVA_HOME'
PATH=$PATH:$JAVA_HOME : it is a line definition where the purpose of the definition itself is just to add or append the value of the environment JAVA_HOME to the existing environment variable named $PATH and then passed all the combination of the values into the environment variable named PATH and export it.   

2.After editing the file that type source .bashrc. It is located in the home folder or directory. Just type cd go to the home folder of the user currently logged in. It can be shown as follows :

user@hostname:~$ cd
user@hostname:~$ 

3.After editing the file, just type source .bashrc. It can be shown as follows :

user@hostname:~$ source .bashrc
user@hostname:~$

4.Check whether the environment variable has been successfully exported or not by typing the following in the bash prompt which it can be shown as follows :

user@hostname:~$ echo $JAVA_HOME
/opt/jdk1.8.0_172
user@hostname:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/jdk1.8.0_172
user@hostname:~$

Leave a Reply