265

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.

2
  • 1
    you could just create ~/.profile most *nix systems recognize the file and use it if it exists. you might need a reboot though
    – trve.fahad
    Commented Jun 6, 2016 at 12:02
  • 1
    ~/.MacOSX/environment.plist is deprecated. Commented Apr 8, 2018 at 17:46

15 Answers 15

447

You have to add it to /etc/paths.

Reference (which works for me) : Here

7
  • 9
    This is the only solution that works on El Capitan. Better than modifying .bash_profile and .profile. Commented Nov 29, 2015 at 21:06
  • 1
    Upvoted the answer for the reference added which explains how to remove elements from path (in the comments) too.
    – Amudhan
    Commented Dec 14, 2015 at 10:03
  • 14
    Do not forget to 'restart' your terminal.
    – Nono
    Commented Apr 12, 2020 at 16:23
  • 1
    Works fine on macOS Catalina as well. Restart the terminal once the changes are made. Commented Sep 14, 2020 at 8:09
  • 2
    Worked great, however I feel the steps should be copied to the answer here on SO Commented Jan 30, 2022 at 10:18
69

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

4
  • 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
    Commented Jun 18, 2018 at 15:55
  • what i did not realize was that i was using zsh instead of bash. thanks!
    – Lioness
    Commented May 25, 2020 at 0: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. Commented Jul 9, 2022 at 4:05
  • Thanks, git and other softwares were using /etc/paths.d/.
    – adesai
    Commented Jun 17 at 16:23
37

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

4
  • 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
    Commented Sep 27, 2018 at 0:45
  • How to save file, which key to press? Commented Jan 3, 2022 at 13:56
  • To save in terminal follow - cyberciti.biz/faq/linux-unix-vim-save-and-quit-command Commented Jan 3, 2022 at 14:06
  • 2
    yea, to save file and quit, I just use Esc, followed by :wq and Enter
    – Rishabh
    Commented Mar 26, 2022 at 8:23
28

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 !!!

1
  • 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. Commented Dec 1, 2022 at 4:48
27

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"
17
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.

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:.
7
  • 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
    Commented Mar 17, 2014 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
    Commented Mar 17, 2014 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
    Commented Mar 18, 2014 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
    Commented Mar 18, 2014 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
    Commented Mar 18, 2014 at 21:01
6

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.

2
  • 3
    This answer needs more attention. This works best with no need to make changes or creating a .profile file. Thx Commented May 13, 2023 at 15:01
  • @YoracoGonzales: While I appreciate your cmt and this answer, I do wonder if making the PATH change permanently might inadvertently break something. We all know there is much that is un-documented in every macOS ever released. And adding to ~/.bash_profile requires only a source invocation.
    – Seamus
    Commented Mar 2 at 16:52
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.

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.

1

There are many places from where the value of $PATH variable is influenced. Following is a way to add the path to it permanently in zsh:

  1. Open the terminal
  2. cd /etc/paths.d
  3. Create a new file with any name. For instance you want to create a path for docker then sudo vi dockerpath
  4. Paste your path in this file /Applications/Docker.app/Contents/Resources/bin
  5. Write and close. Press esc and :wq
  6. Open a new terminal to check if the newly added path is present. echo $PATH
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.

0

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

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/).

0

For MacOS

  • Set a environment variable in zsh:
    export VARIABLE_NAME=<value>
    Eg: export PATH=$PATH:/usr/local/mysql-8.3.0-macos14-arm64/bin
  • Save the file and restart ur terminal
    source ~/.zshenv
    After executing the above restart the terminal.
    Then, run echo $PATH to check whether the PATH is added or not.

Not the answer you're looking for? Browse other questions tagged or ask your own question.