231

I have read several answers on how to set environment variables on OSX 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 advises 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 setting them again and again each time I open a new terminal session.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
patti_jane
  • 3,293
  • 5
  • 21
  • 26

13 Answers13

406

You have to add it to /etc/paths.

Reference (which works for me) : Here

Nitish
  • 6,358
  • 1
  • 15
  • 15
61

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,872
  • 19
  • 31
iplus26
  • 2,518
  • 15
  • 26
  • 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
  • 1
    Just in case, it's prudent NOT "to restart [ALL] the terminal windows" until you are sure there is absolutely nothing broken. You may observe weird new behaviour and having one terminal window with the older environment can help figure it out. – ShpielMeister Jul 09 '22 at 04:05
30

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
  • 10,995
  • 16
  • 38
  • 54
Rish
  • 804
  • 8
  • 15
  • 3
    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
  • 2
    yea, to save file and quit, I just use `Esc`, followed by `:wq` and `Enter` – Rish Mar 26 '22 at 08:23
24

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"
wjandrea
  • 28,235
  • 9
  • 60
  • 81
19

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
  • 359
  • 5
  • 8
  • 1
    finally worked on Apple Sillicon M2 Mac Air after hours of trying. Had to install oh-my-zsh to get the .zshrc file in home, afterwards did what you have suggested. Thank You. – Denver Shenal Dec 01 '22 at 04:48
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
  • 849
  • 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
12
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
  • 1,503
  • 14
  • 11
2

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Jin 허
  • 21
  • 1
2

The accepted answer works. But it is missing a few important steps.

Step 1: Update the /etc/paths file with your path

You need sudo for this. Use the following command and add a new line with your path sudo vim /etc/paths

Step 2: Restart your terminal

This is very important.

Keet Sugathadasa
  • 11,595
  • 6
  • 65
  • 80
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,284
  • 7
  • 16
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

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
  • 821
  • 9
  • 22
0

It depends on the shell you're using. If you use ZSH (the default shell for Catalina and newer systems) you should edit your .zshrc file (see more at Adding a new entry to the PATH variable in ZSH).

If you use BASH I would edit both your .bashrc and your .bash_profile files (supplementary information under https://scriptingosx.com/2017/04/about-bash_profile-and-bashrc-on-macos/).