[linux] 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:

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?

This question is related to linux bash unix path zsh

The answer is


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


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


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.


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.


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 be there permanently.


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.


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
    

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.


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)


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


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


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


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


It can be directly added by using the following command:

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

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.


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)


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


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


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

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

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

This is a one-liner. It adds a line to the .bashrc. Tha 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

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.


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

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to unix

Docker CE on RHEL - Requires: container-selinux >= 2.9 What does `set -x` do? How to find files modified in last x minutes (find -mmin does not work as expected) sudo: npm: command not found How to sort a file in-place How to read a .properties file which contains keys that have a period character using Shell script gpg decryption fails with no secret key error Loop through a comma-separated shell variable Best way to find os name and version in Unix/Linux platform Resource u'tokenizers/punkt/english.pickle' not found

Examples related to path

Get Path from another app (WhatsApp) How to serve up images in Angular2? How to create multiple output paths in Webpack config Setting the correct PATH for Eclipse How to change the Jupyter start-up folder Setting up enviromental variables in Windows 10 to use java and javac How do I edit $PATH (.bash_profile) on OSX? Can't find SDK folder inside Android studio path, and SDK manager not opening Get the directory from a file path in java (android) Graphviz's executables are not found (Python 3.4)

Examples related to zsh

Conda command not found Zsh: Conda/Pip installs command not found commands not found on zsh How do I update zsh to the latest version? npm global path prefix How to permanently set $PATH on Linux/Unix? zsh compinit: insecure directories Command not found after npm install in zsh Adding a new entry to the PATH variable in ZSH Where to place $PATH variable assertions in zsh?