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.
This question is related to
macos
bash
unix
path
environment-variables
You have to add it to /etc/paths
.
Reference (which works for me) : Here
For setting up path in Mac two methods can be followed.
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.
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
source $HOME/.zshrc
You're done !!!
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 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.
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.
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 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:.
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"
Source: Stackoverflow.com