Setting environment variables in Yosemite

10,638

Solution 1

Have you tried editing ~/.bash_profile?

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

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

Solution 2

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.

Solution 3

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 .

Share:
10,638

Related videos on Youtube

Carlos Bribiescas
Author by

Carlos Bribiescas

Updated on September 14, 2022

Comments

  • Carlos Bribiescas
    Carlos Bribiescas over 1 year

    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.

  • Carlos Bribiescas
    Carlos Bribiescas about 9 years
    That doesn't work in Yosemite unfortunately. Take a look at the linked post for more.
  • domino
    domino about 9 years
    Works on my Yosemite. Did you run source ~/.bash_profile after modifying?
  • David W.
    David W. about 9 years
    It depends on your shell. Bash shell is the default, but if you use Zsh or Ksh, setting .bash_profile won't do anything. It also depends on the Terminal application you're using. The default Terminal.app makes all new windows a login shell. Others might make each just a new shell which means that .bash_profile isn't executed. You may have to log out and log back into your Mac to see the change in .bash_profile.
  • domino
    domino about 9 years
    He has the bash tag so that's what I'm working with. Hasn't given much info about Terminal or if in fact he needs the variables for terminal or for access by some other app
  • SMBiggs
    SMBiggs almost 6 years
    Could you clarify on how you make sure that the most recent git is run before the apple-git?