How to set environment variable on Ubuntu
On Ubuntu, there are two system-wide environment variables, both files need admin or sudo
to modify it.
/etc/environment
– It is not a script file, purely assignment expressions, one per line.
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
JAVA_HOME=/usr/lib/jvm/adoptopenjdk-11-hotspot-amd64
LD_LIBRARY_PATH=/usr/local/lib:/usr/lib/postgresql/8.3/lib
/etc/profile.d/*.sh
– Files with .sh
extension in the /etc/profile.d/
folder.
export JAVA_HOME=JAVA_HOME=/usr/lib/jvm/adoptopenjdk-11-hotspot-amd64
export LD_LIBRARY_PATH=/usr/local/lib:/usr/lib/postgresql/8.3/lib
export PATH=$PATH:$JAVA_HOME/bin
Further Reading: Ubuntu – EnvironmentVariables
1. /etc/environment
1.1 Add a new environment variable MY_HOME=/home/mkyong
in the /etc/environment
file and source it to reflect the changes.
$ sudo vim /etc/environment
1.2 Modify, save and exit.
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
JAVA_HOME=/usr/lib/jvm/adoptopenjdk-11-hotspot-amd64
LD_LIBRARY_PATH=/usr/local/lib:/usr/lib/postgresql/8.3/lib
MY_HOME=/home/mkyong
1.3 Test.
$ source /etc/environment
$ echo $MY_HOME
/home/mkyong
Note
The new changes in /etc/environment
will disappear if we close the current session or reopen a new terminal because a new shell does not trigger the /etc/environment
. Try to restart the Ubuntu or login again; the new changes in /etc/environment
will apply automatically.
2. /etc/profile.d/new-env.sh
2.1 Add a new environment variable MY_HOME=/home/mkyong
in the /etc/profile.d/new-env.sh
file and source it to reflect the changes.
$ sudo vim /etc/environment
2.2 Create, save and exit.
export MY_HOME=/home/mkyong
2.3 Test.
$ source /etc/profile
$ echo $MY_HOME
/home/mkyong
2.4 Why source /etc/profile
? Read the source code, it will execute all files with .sh
extension.
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
Hi Dear young,
Im win user and in a job install fedora and a program on it due to speed up run. I need to add a environmental variable to run that program like this:
FLXDM
this variable point a license file in Home directory or where there is:
/HOME/license.dat
can you help me how set this environment variable?
Dear Yong, as understanding this page well, i have solved my problem. Thanks for preparing such a Page.
/etc/environment – This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.
Article is updated, thanks for your feedback.
Dear Editor,
I am not professional at neither linux nor Ubuntu, only a new ubuntu 20 LTS user. I saw your web page while surfing. I need help from someone expertised. My problem is that: I am a new UBUNTU user. I was using the VIM editor to prepare a bashrc file. I made a wrong path in that file. Here are the commands that I have used in the bashrc via the VIM editor:
PATH=/home/user/NBO/NBO6
export=$PATH/nbo6.exe
export=$PATH/genbo6.exe
after closing the bashrc, I can not reach to Neither the VIM nor some Ubuntu Linux commands any way: Let me give several examples:
user@sedatg:~$ ls
the ‘ls’ command can be used in the following places
* /bin/ls
* /usr /bin/ls
The command can not be positioned because ‘/bin:/usr/bin’ PATH has not been included into enviroment variables.
ls: the command can not be found.
secondly,
user@sedatg:~$ vim
the system reply is the same as above.
But,
user@sedatg:~$ cd $PATH
user@sedatg:~/NBO/NBO6$
My request from you is that: I want my terminal of the UBUNTU linux sys. to come back and to remove the definition of the variable “PATH” .
Could you help and explain to me the steps for the solution, with the command instruction ?
I will be thankful if you can save me from this trouble.
Dr. Sedat GUMUS
export PATH=/usr/java/bin:$PATH
I tested your this command. It didnt recover the previous case. I want to enter my bashrc file again and to come back the previous stiuation, to remove my definition in the basch file. What can i do?
Thank you a lot for your hard work and simpler explanation
Thanks!
Can you tell me how to add environment variable for laravel installer in Ubuntu?
Hi Dude,
Nice Info!! Keep it up with good work…
Thanks,
Natarajan