How to Permanently Set $Path on Linux/Unix [Closed]
On Linux, How Can I Add a Directory to the $Path So It Remains Persistent Across Different Sessions? Background I'm Trying to Add a Directory to My Path So It...
On Linux, how can I add a directory to the $PATH so it remains persistent across different sessions?
Background
I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:
export PATH=$PATH:/path/to/dir
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?
24 Answers
You need to add it to your ~/.profile or ~/.bashrc file.
export PATH="$PATH:/path/to/dir"
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
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 export or set commands.
System wide
/etc/environmentList of unique assignments. Allows references. Perfect for adding system-wide directories like/usr/local/something/bintoPATHvariable or definingJAVA_HOME. Used by PAM and systemd./etc/environment.d/*.confList of unique assignments. Allows references. Perfect for adding system-wide directories like/usr/local/something/bintoPATHvariable or definingJAVA_HOME. The configuration can be split into multiple files, usually one per each tool (Java, Go, and Node.js). 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 forPATHentries 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/profileand/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.