Asked 12 years, 6 months ago
Viewed 2.2m times
Closed 5 years ago.
The community reviewed whether to reopen this question 2 years ago and left it closed:
Original close reason(s) were not resolved
On Linux, how can I add a directory to the $PATH so it remains persistent across different sessions?
BackgroundI'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?
Amin Ya1,99911 gold badge2525 silver badges3535 bronze badges
asked Feb 1, 2013 at 0:57
AliAli268k270270 gold badges593593 silver badges786786 bronze badges
1You 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
answered Feb 1, 2013 at 1:01
mpoweredmpowered13.6k22 gold badges1717 silver badges1919 bronze badges
30There 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.
/etc/environment
List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin
to PATH
variable or defining JAVA_HOME
. Used by PAM and systemd.
/etc/environment.d/*.conf
List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin
to PATH
variable or defining JAVA_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/xprofile
Shell 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 PATH
entries 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
and /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 HOME
or PATH
so it has limited use. Used by PAM.
~/.xprofile
Shell 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 PATH
with values such as ~/bin
or ~/go/bin
or defining user specific GOPATH
or 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.
~/.profile
, ~/.<shell>_profile
, ~/.<shell>_login
Shell 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 a user login shell to get the environment. It effectively uses the login shell configurations ~/.profile
, ~/.<shell>_profile
, ~/.<shell>_login
files.
Difference between Login Shell and Non-Login Shell?
Peter Mortensen31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Nov 16, 2014 at 21:29
Grzegorz ŻurGrzegorz Żur49.4k1717 gold badges119119 silver badges112112 bronze badges
11In 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 /etc/environment
file:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
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 Z shell (zsh
), add this line right after the comments in /etc/zsh/zshenv
file:
source /etc/environment
I encountered this little quirk on Ubuntu 15.10 (Wily Werewolf), but if your zsh is not getting the correct PATH, this could be why.
answered May 27, 2014 at 16:27
trve.fahadtrve.fahad4,49622 gold badges1919 silver badges2525 bronze badges
13For Bash, you can put the export
declaration in ~/.bashrc
. For example, my .bashrc contains this line:
export PATH=/var/lib/gems/1.8/bin:/home/ash/.bin:$PATH
answered Feb 1, 2013 at 0:59
ashastralashastral2,86011 gold badge2222 silver badges3333 bronze badges
10You may set $PATH
permanently in two ways.
To set the path for a particular user:
You may need to make the entry in file .bash_profile
in the home directory for the user.
E.g, in my case I will set the java
path in the Tomcat user profile*
echo "export PATH=$PATH:/path/to/dir" >> /home/tomcat/.bash_profile
To set a common path for all system users, you may need to set the path like this:
echo "export PATH=$PATH:/path/to/dir" >> /etc/profile
31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Jan 3, 2014 at 11:35
Mohit MMohit M83788 silver badges1616 bronze badges
3You can use on CentOS or Red Hat Linux (RHEL) for the local user:
echo $"export PATH=\$PATH:$(pwd)" >> ~/.bash_profile
This adds the current directory (or you can use another directory) to the PATH. This makes it permanent, but it takes effect at the next user logon.
If you don't want do a re-logon, then you can use:
source ~/.bash_profile
That reloads the # User specific environment and startup programs
. This comment is present in file .bash_profile
.
31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Oct 21, 2016 at 4:11
You can also set it 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: Linux: Añadir ruta al PATH
Peter Mortensen31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Jun 29, 2016 at 8:59
DelucaramosDelucaramos16111 silver badge22 bronze badges
1Modify the "/etc/profile" file:
vi /etc/profile
Press the I key to enter editing mode and move the cursor to the end of the file. Additional entries:
export PATH=$PATH:/path/to/dir;
Press the Esc key to exit edit mode, and :wq to save the file.
Make the configuration effective
source /etc/profile
Explanation:
The profile file works for all users. If you want it to be valid only for the active user, change the ".bashrc" file.
31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Nov 23, 2018 at 9:24
JiaJia35433 silver badges44 bronze badges
I think the most elegant way is:
Add this in the ~/.bashrc file.
Run this command:
gedit ~/.bashrc
Add your path inside it:
export PATH=$PATH:/opt/node/bin
source ~/.bashrc
(Ubuntu)
Peter Mortensen31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Nov 4, 2017 at 13:40
Himanshu sharmaHimanshu sharma7,98566 gold badges4747 silver badges8282 bronze badges
1I stumbled across this question yesterday when searching for a way to add a folder containing my own scripts to the PATH - and was surprised to find out that my own ~/.profile
file (on Linux Mint 18.1) already contained this:
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
Thus, all I had to do was create the folder ~/bin
and put my scripts there.
31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Mar 4, 2017 at 11:09
RobertGRobertG1,72411 gold badge2626 silver badges4646 bronze badges
2After so much research, I found a simple solution for this (I am using Elementary OS), inspired by Flutter – Step by Step Installation on Linux – Ubuntu.
Run the following command to open the .bashrc file in edit mode. (You may also use vi or any other editor).
~$ sudo nano ~/.bashrc
Add the following line at the end of the file and save.
export PATH="[FLUTTER_SDK_PATH]/flutter/bin:$PATH"
For example:
export PATH="/home/rageshl/dev/flutter/bin:$PATH"
I believe this is the permanent solution for setting the path in Flutter in a Ubuntu distribution.
Peter Mortensen31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Nov 10, 2019 at 10:27
1You can add that line to your console configuration files (e.g., .bashrc, or to .profile).
Peter Mortensen31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Feb 1, 2013 at 0:59
aquaaqua3,4053232 silver badges4141 bronze badges
3It can be directly added by using the following command:
echo 'export PATH=$PATH:/new/directory' >> ~/.zshrc
source ~/.zshrc
abdusco
11.3k33 gold badges3737 silver badges4949 bronze badges
answered Jul 11, 2016 at 11:31
2Permanently add to the PATH variable
Global:
echo "export PATH=$PATH:/new/path/variable" >> /etc/profile
Local (for the current user only):
echo "export PATH=$PATH:/new/path/variable" >> ~/.profile
For global, restart. For local, relogin.
Example
Before:
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin
After:
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable
Alternatively you can just edit file "profile":
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable
Another way (thanks gniourf_gniourf):
echo 'PATH=$PATH:/new/path/variable' >> /etc/profile
Peter MortensenYou shouldn't use double quotes here! echo 'export PATH=$PATH:/new/path/variable'... And by the way, the export keyword is very likely useless as the PATH variable is very likely already marked as exported. – gniourf_gniourf
31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Nov 14, 2014 at 17:35
user3439968user34399683,53811 gold badge2121 silver badges1515 bronze badges
4One way to add a permanent path, which worked for me, is:
cd /etc/profile.d
touch custom.sh
vi custom.sh
export PATH=$PATH:/path according to your setting/
Restart your computer and here we go; the path will be there permanently.
Peter Mortensen31.6k2222 gold badges110110 silver badges134134 bronze badges
answered May 28, 2016 at 3:19
user6393373user63933735111 silver badge11 bronze badge
3Add script file [name_of_script].sh
to the /etc/profile.d
folder with the line:
export PATH=$PATH:/dir
Every script within the /etc/profile.d
folder is automatically executed by /etc/profile
on login.
31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Apr 10, 2015 at 12:12
4My answer is in reference to the setting up of a Go environment on Ubuntu Linux (amd64). I have faced the same trouble of setting the path of environment variables (GOPATH
and GOBIN
), losing it on terminal exit and rebuilding it using the source <file_name>
every time.
The mistake was to put the path (GOPATH
and GOBIN
) in ~/.bash_profile
file. After wasting a few good hours, I found that the solution was to put GOPATH
and GOBIN
in the ~/.bash_rc
file in the manner:
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOPATH:$GOBIN
And in doing so, the Go installation worked fine and there were no path losses.
The reason with which this issue can be related is that settings for non-login shells, like your Ubuntu terminal or GNOME terminal where we run the Go code, are taken from the ~./bash_rc
file and the settings for login shells are taken from ~/.bash_profile
file. And from the ~/.profile
file if the ~/.bash_profile
file is unreachable.
31.6k2222 gold badges110110 silver badges134134 bronze badges
answered May 4, 2017 at 21:13
The files where you add the export command depends on if you are in login-mode or non-login-mode.
If you are in login-mode, the files you are looking for are either /etc/bash or /etc/bash.bashrc.
If you are in non-login-mode, you are looking for the file /.profile or for the files within the directory /.profiles.d
The files mentioned above is where the system variables are.
Peter Mortensen31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Nov 5, 2013 at 13:35
3Zues77 has the right idea. The OP didn't say "How can I hack my way through this?". The OP wanted to know how to permanently append to $PATH:
sudo nano /etc/profile
This is where it is set for everything and is the best place to change it for all things needing $PATH.
Peter Mortensen31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Sep 25, 2015 at 2:26
Joe DJoe D3911 bronze badge
1Let's say you're running macOS. You have a binary you trust and would like to make available across your system, but don't necessarily want the directory in which the binary is to be added to your PATH.
You can opt to copy/move the binary to /usr/local/bin
, which should already be in your PATH. This will make the binary executable like any other binary you may already have access to in your terminal.
31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Jul 4, 2019 at 13:25
LeoLeo46966 silver badges55 bronze badges
This is a one-liner. It adds a line to the .bashrc
. That line is going to check if the directory has already been added to the path and append if not. This will prevent duplicating your directory in the path every time you source .bashrc
.
echo "[[ \":\$PATH:\" != *\":$(pwd)/path/to/add:\"* ]] && export PATH=\"\${PATH:+\${PATH}}:$(pwd)/path/to/add\"" >> ~/.bashrc
source ~/.bashrc
Peter Mortensen
31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Feb 1, 2020 at 11:08
sr9yarsr9yar5,35055 gold badges5757 silver badges6262 bronze badges
1The simplest way is the following line,
PATH="<directory you want to include>:$PATH"
in your .bashrc file in the home directory.
It will not get reset even if you close the terminal or reboot your PC. It's permanent.
Peter Mortensen31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Oct 18, 2013 at 17:00
Alex JonesAlex Jones8381010 silver badges2424 bronze badges
0I think the most elegant way is:
Add this in the ~./bashrc file:
if [ -d "new-path" ]; then
PATH=$PATH:new-path
fi
source *~/.bashrc*
(Ubuntu)
Peter Mortensen31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Jul 17, 2016 at 2:50
1For a Debian distribution, you have to:
~/.bashrc
. E.g: vim ~/.bashrc
export PATH=$PATH:/path/to/dir
~/.bashrc
as root, your environment variable you added will work only for root31.6k2222 gold badges110110 silver badges134134 bronze badges
answered Dec 27, 2017 at 21:18
onlymeonlyme4,08222 gold badges2424 silver badges1818 bronze badges
3 Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting the
association bonus). The reputation requirement helps protect this question from spam and non-answer activity.
Start asking to get answers
Find the answer to your question by asking.
Ask questionExplore related questions
See similar questions with these tags.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4