Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting environment variables in Yosemite

What is the proper way to modify environment variables like PATH in Yosemite?

This is this question Setting environment variables in OS X? but specifically for yosemite since it doesn't work anymore.

like image 709
Carlos Bribiescas Avatar asked Feb 06 '15 17:02

Carlos Bribiescas


3 Answers

Have you tried editing ~/.bash_profile?

Adding a line like this to bash_profile ought to do it:

export PATH=/usr/local/bin:$PATH

like image 124
domino Avatar answered Sep 24 '22 00:09

domino


What shell are you using? I'm assuming you're using the default Bash shell. There's also Csh, Ksh, and Zsh.

The Terminal.app on Mac OS X by default starts a new login shell each time a window is open. This means that $HOME/.bash_profile or $HOME/profile is always executed when you open a new terminal window. You can set particular defaults in here. For example, I set PS1 and set -o vi.

NOTE: This may not be the case if you're using other Terminal apps like xterm. These open new terminal windows as just new shells. This means that you may not see the changes made in .bash_profile until you log out and log back in.

You can try editing $HOME/.bashrc and see if that helps.

What about other shells?

If you're using Kornshell (ksh), you need to edit the $HOME/profile and not $HOME/.bash_profile. If you're using Zshell (zsh), you're on your own. It's too wacky to describe here. Read the manpage for zsh and search for ZDOTDIR.

When you run a shell script, the $HOME/.bashrc is executed. Most people put something like this in their .bash_profile, so their .bashrc settings are included in a new terminal window:

[[ -x $HOME/.bashrc ]] && source "$HOME/.bashrc"

Some people set things they want to be set when they run a shell script, for example export $PS4="\$LINENO> ".

The $PATH is a bit different. You can set it in .bash_profile (I would not set it in .bashrc), But, Mac OS X has an automated why on how systemwide paths are set. A file called /etc/paths is used to set the default path for all users using either Bash or Kornshell via the /usr/libexec/path_helper program.

On my Mac, I set my $PATH to:

/usr/local/bin:/usr/share/bin:/bin:/usr/bin:/usr/sbin:/sbin:$HOME/bin

When I install programs, I usually install them under /opt when possible. Then, I link their binaries (where ever they're placed) to /usr/local/bin. This way, I don't have to keep building my PATH. Plus, it allows me to override system defaults. For example, /usr/bin/git is at 1.9.3. while my installed /usr/local/bin/git is at version 2.2.1.

One thing you should not do is modify /etc/profile because changes there may be replaced on OS X upgrades.

like image 34
David W. Avatar answered Sep 23 '22 00:09

David W.


The problem is not with environment variables set and accessed from within /bin/bash or /bin/sh, but with envars that should be set for programs NOT executed from the shell; i.e. normal apps executed from the dock or Finder.

After finally getting things right with launchctl in Mavericks, Apple is in the process of changing things again. The useful subcommands of launchctl are now labelled as "legacy subcommands", some of which are no longer supported. That leaves a question mark over the others.

In any case, the most important subcommands are still available for now.

launchctl allows for the setting of the overall environment in which user processes execute. It appears that the overall user environment is inherited by all Terminal processes; i.e. all setenv variables are exported. It's a bit tricky to confirm that. In any case, you will still need your .profile and .bashrc to define functions and aliases, which are not supported by launchctl.

I go to some lengths to ensure that all of my launchctl vars are also defined in my profile. This enables me to set up the same environment on remote or VM linux systems, with a few minor tweaks.

Al of my setup is described in this blog post .

like image 42
pbw Avatar answered Sep 24 '22 00:09

pbw