Question

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.

Was it helpful?

Solution

You have to add it to /etc/paths.

Reference (which works for me) : Here

OTHER TIPS

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

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

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"

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

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:.
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.

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.

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.

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

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top