195

I have read several answers on how to set environmental variables on OSX as permanently.

First, I tried this, How to permanently set $PATH on Linux/Unix? but I had an error message saying no such file and directory, so I thought I could try ~/.bash_profile instead of ~/.profile but it did not work.

Second, I found this solution How to set the $PATH as used by applications in os x , which advices to make changes in

~/.MacOSX/environment.plist

but again I had no such file and directory error.

I need a way to set these variables such that it won't require to set them again and again each time I open a new terminal session.

codeforester
  • 34,080
  • 14
  • 96
  • 122
patti_jane
  • 2,863
  • 4
  • 18
  • 25

11 Answers11

341

You have to add it to /etc/paths.

Reference (which works for me) : Here

Nitish
  • 5,668
  • 1
  • 14
  • 15
46

I've found that there are some files that may affect the $PATH variable in macOS (works for me, 10.11 El Capitan), listed below:

  1. As the top voted answer said, vi /etc/paths, which is recommended from my point of view.

  2. Also don't forget the /etc/paths.d directory, which contains files may affect the $PATH variable, set the git and mono-command path in my case. You can ls -l /etc/paths.d to list items and rm /etc/paths.d/path_you_dislike to remove items.

  3. If you're using a "bash" environment (the default Terminal.app, for example), you should check out ~/.bash_profile or ~/.bashrc. There may be not that file yet, but these two files have effects on the $PATH.

  4. If you're using a "zsh" environment (Oh-My-Zsh, for example), you should check out ~./zshrc instead of ~/.bash* thing.

And don't forget to restart all the terminal windows, then echo $PATH. The $PATH string will be PATH_SET_IN_3&4:PATH_SET_IN_1:PATH_SET_IN_2.

Noticed that the first two ways (/etc/paths and /etc/path.d) is in / directory which will affect all the accounts in your computer while the last two ways (~/.bash* or ~/.zsh*) is in ~/ directory (aka, /Users/yourusername/) which will only affect your account settings.

Read more: Mac OS X: Set / Change $PATH Variable - nixCraft

Matthew Hinea
  • 1,772
  • 16
  • 28
iplus26
  • 2,337
  • 14
  • 25
  • 3
    I quite like editing .bash_profile in some cases, because then if you had two user accounts on your machine, you could add scripts just for one of them in e.g. ~/dev/scripts – PeteW Jun 18 '18 at 15:55
  • what i did not realize was that i was using zsh instead of bash. thanks! – Lioness May 25 '20 at 00:13
  • Thanks for this, I wasn't aware of `/etc/paths` and `/etc/paths.d` – Matthew Jan 14 '22 at 15:22
23

For a new path to be added to PATH environment variable in MacOS just create a new file under /etc/paths.d directory and add write path to be set in the file. Restart the terminal. You can check with echo $PATH at the prompt to confirm if the path was added to the environment variable.

For example: to add a new path /usr/local/sbin to the PATH variable:

cd /etc/paths.d
sudo vi newfile

Add the path to the newfile and save it.

Restart the terminal and type echo $PATH to confirm

Patrick Mevzek
  • 9,491
  • 15
  • 33
  • 49
XXX
  • 731
  • 6
  • 15
  • 2
    I'm not sure why this answer is getting downvoted. It seems like a reasonable and sane way to organize path variables. I have used paths.d to create individual files for every non-standard path variable. – p_q Sep 27 '18 at 00:45
  • How to save file, which key to press? – user9088454 Jan 03 '22 at 13:56
  • To save in terminal follow - https://www.cyberciti.biz/faq/linux-unix-vim-save-and-quit-command/ – user9088454 Jan 03 '22 at 14:06
  • 1
    yea, to save file and quit, I just use `Esc`, followed by `:wq` and `Enter` – XXX Mar 26 '22 at 08:23
17

You can open any of the following files:

/etc/profile
~/.bash_profile
~/.bash_login   (if .bash_profile does not exist)
~/.profile      (if .bash_login does not exist)

And add:

export PATH="$PATH:your/new/path/here"
15

You could also add this

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

to ~/.bash_profile, then create ~/.bashrc where you can just add more paths to PATH. An example with .

export PATH=$PATH:.
omoman
  • 829
  • 6
  • 10
  • Thank you. I used the previous solution, but if I apply this method, will it mean that I can also set DYLD_LIBRARY_PATH variables as permanent? – patti_jane Mar 17 '14 at 21:28
  • Yes you should be able to. If you want to see the changes, close your terminal and reopen it, or type `. ~/.bashrc` – omoman Mar 17 '14 at 21:41
  • I add the code above to `. ~/.bash_profile`, then reopen the terminal and type `. ~/.bashrc`, but it says no such file or directory? – patti_jane Mar 18 '14 at 12:50
  • You need to create it. The above lines tells your `.bash_profile` to load `.bashrc` if it exists. Then in the blank `.bashrc`, you can add anything you want. – omoman Mar 18 '14 at 14:24
  • Thanks a lot! I know I am asking too much, but do you know a link where it is properly stated how to create `.bashrc`? I could not manage to find a good one. – patti_jane Mar 18 '14 at 21:01
  • Its just a normal text file. Just open a text file and save it as `.bashrc` and put it in your home directory. – omoman Mar 18 '14 at 23:51
  • The usual place for additional PATH definitions is `~/,bash_profile`. Therefore you could and should insert the `PATH=$PATH:/usr/local/bin` directly to that file. Adding the actual directory (AKA `.`) is in general no good idea. It can cause severe harm! If you add `.` to your PATH, than add it always as the last element (as you did :-)). – Jan Jun 18 '18 at 17:28
10

If you are using zsh do the following.

  1. Open .zshrc file nano $HOME/.zshrc

  2. You will see the commented $PATH variable here

    # If you come from bash you might have to change your $PATH.
    # export PATH=$HOME/bin:/usr/local/...

  3. Remove the comment symbol(#) and append your new path using a separator(:) like this.

export PATH=$HOME/bin:/usr/local/bin:/Users/ebin/Documents/Softwares/mongoDB/bin:$PATH

  1. Activate the change source $HOME/.zshrc

You're done !!!

Ebin Xavier
  • 219
  • 5
  • 7
8
sudo nano /etc/paths

now find the path of command i am giving an example of setting path for flutter.

/Users/username/development/flutter/bin

now cntrol+x and then y . reopen the terminal and check.

Amit Kumar
  • 939
  • 9
  • 6
1
launchctl setenv environmentvariablename environmentvariablevalue

or

launchctl setenv environmentvariablename `command that will generate value`

use proper ` and remember to restart application or terminal for the environment variable to take effect.

you can check environment variable by printenv command.

note : environment variable named path is already set by someone else so we are not appending anything to that path at all here.

Jainil Patel
  • 1,166
  • 4
  • 15
1

shows all hidden files like .bash_profile and .zshrc $ ls -a

Starting with macOS Catalina, mac uses zsh instead of bash. so by default mac uses zsh. Check which shell running:

$ echo $SHELL
/usr/zsh
$ cd $HOME
$ open -e .zshrc

or if using vim

$ vi .zshrc

Then add it like this

$ export my_var="/path/where/it/exists"
$ export PATH=$PATH:/$my_var/bin

For example: if installed app named: myapp in /Applications Then

export MYAPP_HOME=/Applications/myapp
export PATH=$PATH:$MYAPP_HOME/bin

or shortcut

export PATH=${PATH}:/Applications/myapp/bin

TADA you set for life !!! Thank me later

1

19 October 2021.

Confirming iplus26's answer with one correction.

Test environment

OS: macOS 11.6 (Big Sur) x86_64

Shell: zsh 5.8


Below is the order in which the $PATH environment variable is modified:

  1. each line in etc/paths text file gets appended
  2. each line in each text file inside etc/paths.d directory gets appended
  3. finally, the $PATH is further modified in ~/.zshrc

iplus26's answer stated "when (you run) echo $PATH, The $PATH string will be PATH_SET_IN_3&4:PATH_SET_IN_1:PATH_SET_IN_2" but this isn't always the case. It will have to depend on what the script is doing inside .zshrc. E.g. If we do something like

PATH="/new/path:${PATH}"

then, the new path will be in the beginning of the path list. However, if we do something like

PATH="${PATH}:/new/path"

then, the new path will be appended at the end of the path list.

Of course, you'll have to make sure you export the modified path in the ~/.zshrc file.

export PATH=$PATH

One handy command you could use to "pretty print" your path list is

print -l $path

This will print each path on a new line for better readability. Note $path is like $PATH except it's delimited by a single space, instead of a colon, :.

Hopefully this can further clarify for newcomers to this thread.

Jin 허
  • 11
  • 1
0

For setting up path in Mac two methods can be followed.

  1. Creating a file for variable name and paste the path there under /etc/paths.d and source the file to profile_bashrc.
  2. Export path variable in ~/.profile_bashrc as

    export VARIABLE_NAME = $(PATH_VALUE)

AND source the the path. Its simple and stable.

You can set any path variable by Mac terminal or in linux also.

Marian Nasry
  • 806
  • 10
  • 21