linux - [Solved-5 Solutions] How to permanently set $PATH on Linux/Unix ? - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

How to permanently set $PATH on Linux/Unix ?

Linux - Solution 1:

You need to add it to your ~/.profile file.

export PATH=$PATH:/path/to/dir
click below button to copy the code. By - Linux tutorial - team

You also may want to symlink to binaries:

cd /usr/bin
sudo ln -s /path/to/binary binary-name
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 2:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
click below button to copy the code. By - Linux tutorial - team

So you can just open up this file as root and add whatever you want.

Run:

source /etc/environment && export PATH
click below button to copy the code. By - Linux tutorial - team

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

source /etc/environment
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 3:

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.

System wide

  • /etc/environment List of unique assignments. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME.
  • /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.
  • /etc/.rc. Shell script. This is a poor choice because it is single shell specific.

User session

  • ~/.pam_environment. List of unique assignments. 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 variable including HOME or PATH so it has limited use.
  • ~/.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 script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems.
  • ~/.rc. Shell script. This is a poor choice because it is single shell specific.

Distribution specific documentation

  • Ubuntu
  • archlinux

Linux - Solution 4:

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

export PATH=/var/lib/gems/1.8/bin:/home/fraxtil/.bin:$PATH
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 5:

You may set $PATH permanently in 2 ways.

  • 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 this case set java path in tomcat user profile

[tomcat]$ echo "export PATH=$PATH:/path/to/dir" >> /home/tomcat/.bash_profile
click below button to copy the code. By - Linux tutorial - team
  • 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
click below button to copy the code. By - Linux tutorial - team

Related Searches to - linux - linux tutorial - How to permanently set $PATH on Linux/Unix?