31

I'm not clear how to add permanently paths to the PATH env var. I've found several questions for this each time with a different answers. I created a .bash_profile in my home dir, but each time I reboot I have to manualy export my paths again.

source ~/.bash_profile doesn't even work.

What am I missing?

This is currently my .bash_profile

export PATH="/usr/local/opt/python/libexec/bin:$PATH"
export PATH="/usr/local/opt/openssl/bin:$PATH"
export PATH="/Applications/XAMPP/xamppfiles/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

1 Answers1

37

What you've laid out is the proper way to add additional directories to your user's $PATH.

(NOTE: These instructions are for Macs that use the bash shell. As of macOS 10.15 (Catalina), Macs use zsh by default. All the steps below still apply with the exception that the file is ~/.zshrc instead of ~/.bash_profile. If you're unsure which shell you're using you should be able to run the command ps -p $$ to figure it out)

Step 1 - ~/.bash_profile

To start make edits to your ~/.bash_profile adding whatever locations you'd like to have amended to your $PATH.

export PATH="/usr/local/opt/python/libexec/bin:$PATH"
export PATH="/usr/local/opt/openssl/bin:$PATH"
export PATH="/Applications/XAMPP/xamppfiles/bin:$PATH"

Step 2 - source ~/.bash_profile

After making the above edits to this file you can either use the source command or the . notation to "reload" and changes made to this file in your current shell's context.

$ . ~/.bash_profile

-or-

$ source ~/.bash_profile

Step 3 - Evaluate changes

After making the edits and sourcing them you can confirm they had the effect you desired by echoing the contents of the $PATH varible.

$ echo $PATH | tr ':' '\n'
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/opt/X11/bin
/Applications/Wireshark.app/Contents/MacOS
/usr/local/sbin
/Users/smingolelli/bin
/usr/local/opt/go/libexec/bin
/Applications/Visual Studio Code.app/Contents/Resources/app/bin
/Users/smingolelli/projects/kubebuilder/kubebuilder_1.0.5_darwin_amd64/bin/

The order matters, so directories that occur first will be searched first. If a binary lives in multiple places, the first place encountered will be the one that is used.

Also keep in mind that multiple sourcings of this file will have a negative effect of continuing to add the same changes, so it's often the case that you'll want to completely se the $PATH to a consistent known initial state and then amend it with these types of commands:

export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin"
export PATH="/some/new/dir:$PATH"

Using path_helper

macOS also includes a helper to assistance in the management of your $PATH. It's located here /usr/libexec/path_helper.

So instead of manually crafting your base $PATH as mentioned above you can instead use this snippet to get a known good starting point for your $PATH.

[ -x /usr/libexec/path_helper ] && eval $(/usr/libexec/path_helper -s)

This will take care to initialize $PATH so any directories listed in /etc/paths and /etc/paths.d/ get added automatically.

Alan W. Smith
  • 819
  • 4
  • 8
  • 19
slm
  • 4,798
  • 2
    It might be better to use [ -x /usr/libexec/path_helper ] && eval $(/usr/libexec/path_helper -s) to initialize the path so any directories listed in /etc/paths and /etc/paths.d/ get added automatically. – nohillside Apr 28 '19 at 13:46
  • 1
    @nohillside makes sense. I wasn't aware that macOS had this helper, thanks, I'll add to A'er. – slm Apr 28 '19 at 13:48
  • 1
    Have a look at /etc/profile. It's not much going on there, but initialising PATH is part of it. – nohillside Apr 28 '19 at 13:52
  • Just a head's up, my ~./bash_profile just routes to ~/.bashrc which is the actual file I had to modify. – EHorodyski Sep 24 '19 at 13:08
  • @slm [ -x /usr/libexec/path_helper ] && eval $(/usr/libexec/path_helper -s) removing the second part gives me the same result as just using the first part when I source ~/.bash_profile more than once. Maybe an explanation of what each part does would be helpful. – John Sep 03 '20 at 21:02
  • @slm The advice under "Using path_helper" causes me to have duplicates of any paths I have exported in ~/.bash_profile. If we are adding paths on a per user basis inside ~/.bash_profile how do we avoid duplicating them with this solution? – John Sep 03 '20 at 21:46
  • A note when adding a path via export it makes a difference where you put "$PATH". export PATH=/usr/local/opt/nano/bin:"$PATH" prefixes to path instead of appending and export PATH="$PATH":/usr/local/opt/nano/bin appends to path instead of prefixing. – John Sep 03 '20 at 21:47
  • This does not seem to "permanently" add the paths to PATH. I need to source this .bash_profile every time I reopen the terminal. Any suggestions? – Nick Rolando Dec 09 '20 at 19:16
  • 3
    I noticed that the macOS shell is now ZSH. How do I do this for ZSH? – Aaron Franke Apr 19 '21 at 22:13
  • 1
    @AaronFranke - instead of adding the lines in step one to ~/.bash_profile, you add them to ~/.zshrc – Alan W. Smith Mar 06 '22 at 19:02