How to permanently set $PATH on Linux/Unix?
I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:
This works, however each time I exit the terminal and start a new terminal instance, this path is lost, and I need to run the export command again.
How can I do it so this will be set permanently?
There are multiple ways to do it. The actual solution depends on the purpose.
The variable values are usually stored in either a list of assignments or a shell script that is run at the start of the system or user session. In case of the shell script you must use a specific shell syntax and
/etc/environmentList of unique assignments, allows references. Perfect for adding system-wide directories like
PATHvariable or defining
JAVA_HOME. Used by PAM and SystemD.
/etc/environment.d/*.confList of unique assignments, allows references. Perfect for adding system-wide directories like
PATHvariable or defining
JAVA_HOME. The configuration can be split into multiple files, usually one per each tool (Java, Go, NodeJS). Used by SystemD that by design do not pass those values to user login shells.
/etc/xprofileShell script executed while starting X Window System session. This is run for every user that logs into X Window System. It is a good choice for
PATHentries that are valid for every user like
/usr/local/something/bin. The file is included by other script so use POSIX shell syntax not the syntax of your user shell.
/etc/profile.d/*Shell script. This is a good choice for shell-only systems. Those files are read only by shells in login mode.
/etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.
~/.pam_environment. List of unique assignments, no references allowed. Loaded by PAM at the start of every user session irrelevant if it is an X Window System session or shell. You cannot reference other variables including
PATHso it has limited use. Used by PAM.
~/.xprofileShell script. This is executed when the user logs into X Window System system. The variables defined here are visible to every X application. Perfect choice for extending
PATHwith values such as
~/go/binor defining user specific
NPM_HOME. The file is included by other script so use POSIX shell syntax not the syntax of your user shell. Your graphical text editor or IDE started by shortcut will see those values.
~/.<shell>_loginShell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems. Used by shells in login mode.
~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.
Gnome on Wayland starts user login shell to get the environment. It effectively uses login shell configurations
Distribution specific documentation
You need to add it to your
Depending on what you're doing, you also may want to symlink to binaries:
cd /usr/bin sudo ln -s /path/to/binary binary-name
Note that this will not automatically update your path for the remainder of the session. To do this, you should run:
source ~/.profile or source ~/.bashrc
Read more… Read less…
In Ubuntu, edit
/etc/environment. Its sole purpose is to store Environment Variables. Originally the $PATH variable is defined here.
This is a paste from my
So you can just open up this file as root and add whatever you want.
For Immediate results, Run (try as normal user and root):
source /etc/environment && export PATH
If you use
zsh (a.k.a Z Shell), add this line right after the comments in
I encountered this little quirk on Ubuntu 15.10, but if your zsh is not getting the correct PATH, this could be why
export declaration in
~/.bashrc. My .bashrc contains this:
You may set
$PATH permanently in 2 ways.
To set path for particular user : You may need to make the entry in
.bash_profilein home directory in the user.
e.g in my case I will set java path in tomcat user profile
[tomcat]$ echo "export PATH=$PATH:/path/to/dir" >> /home/tomcat/.bash_profile
To set common path for ALL system users, you may need to set path like this :
[root~]# echo "export PATH=$PATH:/path/to/dir" >> /etc/profile
You can use on Centos or RHEL for local user:
echo $"export PATH=\$PATH:$(pwd)" >> ~/.bash_profile
This add the current directory(or you can use other directory) to the PATH, this make it permanent but take effect at the next user logon.
If you don't want do a re-logon, then can use:
That reload the
# User specific environment and startup programs this comment is present in
You can also set permanently, editing one of these files:
/etc/profile (for all users)
~/.bash_profile (for current user)
~/.bash_login (for current user)
~/.profile (for current user)
You can also use
/etc/environment to set a permanent PATH environment variable, but it does not support variable expansion.
Extracted from: http://www.sysadmit.com/2016/06/linux-anadir-ruta-al-path.html