Setting PATH environment variable in OSX permanently
MacosBashUnixPathEnvironment VariablesMacos Problem Overview
I have read several answers on how to set environmental variables on OSX as permanently.
First, I tried this, https://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux 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 https://stackoverflow.com/questions/460835/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.
Macos Solutions
Solution 1 - Macos
You have to add it to /etc/paths
.
Reference (which works for me) : Here
Solution 2 - Macos
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:
-
As the top voted answer said,
vi /etc/paths
, which is recommended from my point of view. -
Also don't forget the
/etc/paths.d
directory, which contains files may affect the$PATH
variable, set thegit
andmono-command
path in my case. You canls -l /etc/paths.d
to list items andrm /etc/paths.d/path_you_dislike
to remove items. -
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
. -
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.
Solution 3 - Macos
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
Solution 4 - Macos
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"
Solution 5 - Macos
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:.
Solution 6 - Macos
If you are using zsh do the following.
-
Open .zshrc file
nano $HOME/.zshrc
-
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/...
-
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
- Activate the change
source $HOME/.zshrc
You're done !!!
Solution 7 - Macos
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.
Solution 8 - Macos
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.
Solution 9 - Macos
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:
- each line in
etc/paths
text file gets appended - each line in each text file inside
etc/paths.d
directory gets appended - 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.
Solution 10 - Macos
For setting up path in Mac two methods can be followed.
-
Creating a file for variable name and paste the path there under /etc/paths.d and source the file to profile_bashrc.
-
Export path variable in
~/.profile_bashrc
asexport 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.
Solution 11 - Macos
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