By Click Upvote


2013-02-01 00:57:08 8 Comments

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?

23 comments

@Grzegorz Żur 2014-11-16 21:29:08

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

  1. /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.
  2. /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, NodeJS). Used by SystemD.
  3. /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.
  4. /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.
  5. /etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.

User session

  1. ~/.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.
  2. ~/.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.
  3. ~/.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.
  4. ~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.

Notes

Gnome on Wayland starts user login shell to get the environment. It effectively uses login shell configurations ~/.profile, ~/.<shell>_profile, ~/.<shell>_login files.

Manuals

  • environment
  • environment.d
  • bash
  • dash

Distribution specific documentation

Related

Difference between Login Shell and Non-Login Shell?

@James Ko 2016-11-11 23:12:15

Thank you for the detailed answer, this should be higher up. Maybe .bash_profile should be added to the list as well?

@trve.fa7ad 2016-11-14 04:11:49

@JamesKo that was number 4

@banan3'14 2018-04-08 20:22:58

I think the best answer is suggested /etc/environment. But can I refresh it without logging out and in? Sometimes I don't use bash or sh so source /etc/environment doesn't work.

@Peter Gloor 2018-06-13 18:23:42

Great and in my opinion the most complete answer. Should be much higher up.

@Peter Gloor 2018-06-13 18:34:03

My reason to search for this topic was actually Go. Glad to see I'm not the only one who realised that .bashrc is not the right place. ;)

@Ragesh S 2019-11-10 10:27:46

After so much research, I found a simple solution for this ( I am using elementary OS), inspired by the following link.

Run the following command to open .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"

enter image description here

I believe this is the permanent solution for setting path in flutter in Ubuntu distro

Hope this will helpful.

@user6393373 2016-05-28 03:19:04

one way to add 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 path will there permanently cheers.

@Warwick 2019-12-18 21:42:06

You don't actually need to restart your computer. Logging out and back in again is sufficient. This is quite an elegant solution as it is very easy to backout any changes without needing to edit files. It is also easy to specify an if statement so that if a directory doesn't exist on the system, it isn't added to the PATH variable.

@Anoop Nagabhushan 2016-07-11 11:31:39

It can be directly added by using the following command:

echo 'export PATH=$PATH:/new/directory' >> ~/.zshrc
source ~/.zshrc

@Laurenz Albe 2016-07-11 11:38:14

The question is labeled bash, so that is not very helpful.

@Carlos Saltos 2018-02-02 15:04:13

This is a valid answer -> the title only mentions Linux, so bash and ALSO zsh will do ... if the tagging is only "bash" we need to add "zsh" to the tags too

@Leo 2019-07-04 13:25:26

Let's say you're running MacOS and 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.

@Jia 2018-11-23 09:24:45

1.modify "/etc/profile" file.

#vi /etc/profile

Press "i" key to enter editing status and move cursor to the end of the file,Additional entries:

export PATH=$PATH:/path/to/dir;

Press "Esc" key exit edit status,':wq' save the file.

2.Make configuration effective

source /etc/profile

Explain: profile file works for all users,if you want to be valid only for the active user, set the ".bashrc" file

@trve.fa7ad 2014-05-27 16:27:44

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 /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

UPDATE:

If you use zsh (a.k.a Z Shell), add this line right after the comments in /etc/zsh/zshenv:

source /etc/environment

I encountered this little quirk on Ubuntu 15.10, but if your zsh is not getting the correct PATH, this could be why

@user3439968 2014-11-14 17:20:06

Not all system have /etc/environment

@trve.fa7ad 2014-11-15 00:33:59

@user3439968 While you might be correct, I never encountered one without it :D Then again I only mess around with OS that I am comfortable using.

@user3439968 2014-11-15 14:24:58

My Ubuntu and Debian has it. My FreeBSD and OpenWRT don't. I use /etc/profile

@Sparhawk 2015-02-24 01:57:26

FWIW $PATH is also defined in /etc/profile in Arch Linux.

@e-sushi 2015-02-25 16:48:25

Funny enough, my Ubuntu 14.04 doesn’t have that file either as it uses ~/.bashrc. On the other hand, not having a /etc/environment makes sense from my point of view me as I don’t have a single variable I need to set for “everyone”… and it’s generally best to separate the core system configurations from individual user configurations anyway. ;)

@trve.fa7ad 2015-02-28 12:18:53

@e-sushi I am actually shocked at that. I'm on ubuntu 14.04.1 myself. and I can promise you the file came built in.

@Chockomonkey 2015-03-24 16:21:12

After trying every suggestion under the sun but /etc/environment, and having them all NOT work, i finally stumbled across this. I am also on Ubuntu 14.04 and this is the only one that actually changed the PATH variable after reboots.

@Developerium 2015-09-27 05:16:39

it took a restart, for the system to realize I added that line! was not immediately recognized

@BlackBrain 2016-01-12 21:14:47

/etc/environment is for system-wide environment variables. This is the only reason to change it. If you make a mistake while you are editing this file, you'll probably end up with an empty PATH variable (I wouldn't wish on any man). A good way to add paths to your PATH variable that affect the system as a whole is to use /etc/profile.d directory (this link should be an useful link).

@trve.fa7ad 2016-01-18 18:00:42

@BlackBrain, yes I am aware of the dangers that come with modifying /etc/environment. but if you use anything like gedit, you should, by default, have a backup file. not to mention, you could always end up overriding PATH to a blank variable in ANY system-wide file. my point is, if you are willing to modify PATH, you should be already acquainted with the dangers of doing it and modifying /etc/environment is not any more dangerous than say something like /etc/profile :)

@Harish_N 2016-03-24 17:50:18

User should restart the PC after updating environment file.

@Brian Jackson 2017-10-04 18:58:45

For many of us who use ~/.bashrc you can easily load (e.g. if you have changed your $PATH variable but don't want to restart) the same way using source ~/.bashrc

@Tankman六四 2018-10-08 02:46:07

Debian has /etc/environment but setting PATH there has no effect - /etc/profile and /etc/login.defs re-set them backs. Tested on Debian 9.5

@Delucaramos 2016-06-29 08:59:52

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

@mpowered 2013-02-01 01:01:27

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

@Batandwa 2014-01-11 00:16:04

A couple of questions. 1) Shouldn't there be a colon between $PATH and /usr/bin. 2) Should /usr/bin even be there. 3) Shouldn't you rather use /usr/local/bin?

@erewok 2014-01-14 00:16:00

Please note: it's often considered a security hole to leave a trailing colon at the end of your bash PATH because it makes it so that bash looks in the current directory if it can't find the executable it's looking for. Users who find this post looking for more information should be advised of this.

@Lily Chung 2014-02-27 00:39:17

@AdamRobertson It is unsafe- consider the scenario when you unpack a tarball, then cd to the directory you unpacked it in, then run ls---and then realize that the tarball had a malicious program called ls in it.

@donquixote 2014-04-09 01:08:27

For me it was .bash_profile, not .profile. Seems this is different for everyone.

@max pleaner 2014-06-07 18:21:10

@donquixote see here for the difference between those files stackoverflow.com/questions/415403/…

@mpowered 2014-06-10 17:56:12

@Istvan Chung In that case, the system's ls would take precedence, but I see what you're saying...

@Lily Chung 2014-06-13 21:27:15

@AdamRobertson There are other, more nasty versions. For instance, creating a malicious script called sl and waiting for someone to mistype ls.

@patryk.beza 2014-06-20 15:07:41

FYI: In Ubuntu and Debian there is already added ~/bin directory in ~/.profile.

@Erick Robertson 2014-09-04 17:43:47

I think I significantly improved the quality of this answer, and addressed a few issues which other users brought up. Every path export, or every command which adjusts the path, should always make sure to separate an existing path with a colon. Leading or trailing colons should never be used, and the current directory should never be in the path.

@JonnyRaa 2015-01-22 10:47:44

just to clarify. the path setup now shown here is ok and isn't dangerously including the current directory... took me a while to figure out from the other comments here, but you can see from the edit history what the bad version looks like

@Aaron McDaid 2015-07-29 14:36:29

Shouldn't it have quotes, like export PATH="$PATH:/path/to/dir", in case there are spaces in any of the directory names? I agree it's a bit unlikely in the case of PATH. But in any case, it's a good habit to put " around all expansions anyway.

@mpowered 2015-08-01 18:19:28

@AaronMcDaid If you have spaces in your directories, you should probably be escaping them. It's a matter of preference, really.

@Alex 2016-02-06 13:49:55

Sorry to necromance this, but this did not work for me on Arch. Instead i added the line into .bashrc.

@Greg Price 2017-04-20 17:22:40

@Alex that would work on rhel I know for sure. Perhaps you can also alter the scripts in the /etc/bashrc file depending on access for various users. As long as a script that sets the path var each time the shell opens you get the right path var. As far security goes as long you ensure that no commands can be run in your export directory, unintentionally, exporting would secure..

@Antony 2017-06-12 12:44:30

In my Debian 8 system, I just had to put the path in ~/.profile file and then run source .profile in order to make the changes effective immediately for the current terminal session. This is set permanently and is available after logoff/reboot.

@thinwybk 2017-07-28 07:58:51

Run source ~/.profile after editing ~/.profile that you do not need to reboot.

@sZpak 2018-02-09 23:04:53

How about path on Android? See my question

@aog 2018-03-29 08:18:47

This is a temporary solution because all changes would be lost on system boot. Correct solution is to edit the PATH variable in the /etc/environment file.

@mpowered 2018-03-30 11:56:17

@aog that’s incorrect, this is executed at the start of shell session and thus persists across logins.

@Peter Gloor 2018-06-13 18:16:37

In newer Ubuntu releases (16.04/18.04) I'm using .profile to extend the $PATH variable, because the entries will be duplicated in new bash shells when added in .bashrc.

@Loves Probability 2018-06-19 06:31:23

Expanding @erewok's most popular comment (like I needed to). The trailing colon (i.e. colon as last character) is treated as a ZERO-length folder name, similar to that of a pair of two consecutive colons. And the convention is to treat all ZERO-length folder name as equivalent to current directory.

@StackAttack 2018-06-20 17:26:26

on cent os7, i seem to have .bash_profile instead of .profile

@yobro97 2018-07-17 12:10:01

Just for everyone's information, I realized just now this can be used as an exploit for exploit-exercises.com/nebula/level01

@Himanshu sharma 2017-11-04 13:40:53

I think the most elegant way is:

1.add this in ~/.bashrc file Run this command

gedit ~/.bashrc

add your path inside it

export PATH=$PATH:/opt/node/bin

2.source ~/.bashrc

(Ubuntu)

@onlyme 2017-12-27 21:18:00

For debian distribution, you have to:

    - edit ~/.bashrc  e.g: vim ~/.bashrc 
    - add export PATH=$PATH:/path/to/dir
    - then restart your computer. Be aware that if you edit ~/.bashrc  as root, your environment variable you added will work only for root

@Abhiroj Panwar 2017-05-04 21:13:24

My answer is in reference to the setting-up of go-lang 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 folder. After wasting a few good hours, I found that the solution was to put GOPATH and GOBIN in ~/.bash_rc file in the manner:

export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOPATH:$GOBIN

and doing so, the go installation worked fine and there were no path losses.

EDIT 1: 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 ~./bash_rc file and the settings for login shells are taken from ~/.bash_profile file, and from ~/.profile file if ~/.bash_profile file is unreachable.

@RobertG 2017-03-04 11:09:57

I 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.

@Daniel Antonio Nuñez Carhuayo 2016-10-21 04:11:17

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:

source ~/.bash_profile

That reload the # User specific environment and startup programs this comment is present in .bash_profile

@Gimcuan Hui 2016-07-17 02:50:27

I think the most elegant way is:

1.add this in ~./bashrc file

if [ -d "new-path" ]; then
  PATH=$PATH:new-path
fi

2.source ~/.bashrc

(Ubuntu)

@Robot70 2018-06-15 18:44:24

And to display the path after above: printf "%s\n" $PATH

@Joe D 2015-09-25 02:26:23

Zues77 has the right idea. The OP didn't say "how can i hack my way through this". 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

@Yura 2015-04-10 12:12:15

Add to /etc/profile.d folder script [name_of_script].sh with line: export PATH=$PATH:/dir. Every script within /etc/profile.d folder is automaticaly executed by /etc/profile on login.

@Yura 2015-04-10 12:14:35

It's recommended way of how to customize your environment

@mpowered 2015-04-10 20:25:49

This is only if you want the settings to be system-wide, which is probably not the most common use case. Most people want (or should want) the path to be set locally, because most users/roles are doing contextually different operations, and the fewer assumptions you make, the better.

@Yura 2015-04-15 09:49:05

@mpowered, yeah, this is only for system-wide. If you want localy change PATH you should add the same export in ~/.profile or ~/.bashrc. Here you should consider that login shells read ~/.profile and interactive shells read ~/.bashrc. This is very important because ssh for example does not do an login, therefore ~/.profile will not be read. Several distibution like suse source ~/.bashrc in /etc/profile. But it's not common for all linux'

@Mohit M 2014-01-03 11:35:39

You may set $PATH permanently in 2 ways.

  1. To set path for particular user : You may need to make the entry in .bash_profile in 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
    
  2. 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
    

@Chris Johnson 2014-10-16 13:36:41

Is the file named /etc/profiles with an s on your distro? Mine has no s. I think you have a typo.

@BuvinJ 2016-01-20 15:12:37

You probably want to escape the $ you are writing to the profile file. e.g. echo "export PATH=\$PATH:/path/to/dir" >> /etc/profile, that way you actually append to the variable when that script runs rather than setting it to a literal value based on it's value at the time of executing this initial command.

@user3439968 2014-11-14 17:35:15

Permanently add PATH variable

Global:

echo "export PATH=$PATH:/new/path/variable" >> /etc/profile

Local(for 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 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

You 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

@gniourf_gniourf 2014-11-14 17:48:08

Nope. You 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.

@user3439968 2014-11-14 17:58:22

Nope, you should use double quotes because $PATH in single quotes not interpolated. And BTW export also useful.

@user3439968 2014-11-14 18:14:50

I got it. You can use double quotes or use single quotes, because $PATH interpolated when the echo executed or interpolate when /etc/profile execute.

@trve.fa7ad 2015-01-27 16:20:10

@user3439968 actually, Double quotes will cause a lot of issues if you were to append to $PATH from multiple files. Consider: when you use double quotes, $PATH gets translated to a static string with all the previously defined PATH directories. say you append /usr/local to it using ~/.bashrc. now if you intend to append /opt/bin to the same variable using /etc/bash.bashrc; $PATH will translate to the same static string, as a result $PATH will be replaced instead of appended to... It will be a matter of system's preference to one file over another

@Edward Torvalds 2013-10-18 17:00:29

the best simple way is the following line:
PATH="<directory you want to include>:$PATH"
in your .bashrc file in home directory.
It will not get reset even if you close the terminal or reboot your PC. Its permanent

@Edward Torvalds 2014-10-04 16:37:25

@quant if you do what is said, it will set your settings permanently. it will work even if you close the terminal.

@Dikinha 2013-11-05 13:35:33

the files where you add the export command depends if you are in login-mode or non-login-mode.

if you are in login-mode, the files you are looking for is 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 if where the system variables are.

@Fraxtil 2013-02-01 00:59:39

Put the export declaration in ~/.bashrc. My .bashrc contains this:

export PATH=/var/lib/gems/1.8/bin:/home/fraxtil/.bin:$PATH

@Click Upvote 2013-02-01 01:01:45

restart needed?

@Click Upvote 2013-02-01 01:10:59

Worked when I put this in the .profile', didn't find .bashrc

@Fraxtil 2013-02-01 02:10:02

It might be dependent on the exact system; I'm not sure exactly what conditions determine which file is executed. Glad the problem was solved, though.

@BigSack 2014-04-06 06:02:31

@Click Upvote You need to do source ~/.bashrc to to reload .bashrc configuration. Then it will work

@Towhid 2014-07-21 18:40:51

How I'll Put the export declaration in ~/.bashrc. I'm using Ubuntu 14.04 LTS. I'm very newbie to Linux.

@Charles Duffy 2014-10-04 17:20:35

The export keyword is only needed if PATH is not already flagged as an environment variable -- which it almost unconditionally will be. Simply PATH=/var/lib/gems/1.8/bin:/home/fraxtil/.bin:$PATH would have the same effect.

@trve.fa7ad 2014-11-15 00:37:54

@Towhid open up the file (~/.bashrc) in a text editor like nano and type up that line at the endo of the file. The path will be modified as soon as you open a new bash instance

@G_Style 2018-08-21 14:21:37

This worked for me on a CentOs system. I'm sure its different for different distros.

@aqua 2013-02-01 00:59:22

You can add that line to your console config file (e.g. .bashrc) , or to .profile

@Click Upvote 2013-02-01 01:03:23

I have neither of those files in /home/(username)

@David Schwartz 2013-02-01 01:05:35

@ClickUpvote: What shell do you use? (And files that start with a dot are hidden, you need something like ls -a to see them.)

@trve.fa7ad 2014-09-07 01:20:05

Incase you don't have any of those files (bashrc or profile) you can manually create them and they will automatically be used

Related Questions

Sponsored Content

30 Answered Questions

[SOLVED] How do I prompt for Yes/No/Cancel input in a Linux shell script?

25 Answered Questions

[SOLVED] How can I safely create a nested directory?

48 Answered Questions

[SOLVED] How do I find all files containing specific text on Linux?

17 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 500223 View
  • 1760 Score
  • 17 Answer
  • Tags:   c++ linux profiling

7 Answered Questions

[SOLVED] Setting PATH environment variable in OSX permanently

27 Answered Questions

[SOLVED] How to change the output color of echo in Linux

15 Answered Questions

[SOLVED] UTF-8 all the way through

Sponsored Content