Setting environment variables on OS X

后端 未结 30 2937
你的背包
你的背包 2020-11-21 05:15

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

I\'ve looked on Google a little bit and found three different files to edit:

相关标签:
30条回答
  • 2020-11-21 05:50

    One thing to note in addition to the approaches suggested is that, in OS X 10.5 (Leopard) at least, the variables set in launchd.conf will be merged with the settings made in .profile. I suppose this is likely to be valid for the settings in ~/.MacOSX/environment.plist too, but I haven't verified.

    0 讨论(0)
  • 2020-11-21 05:51

    Up to and including OS X v10.7 (Lion) you can set them in:

    ~/.MacOSX/environment.plist
    

    See:

    • https://developer.apple.com/legacy/library/qa/qa1067/_index.html
    • https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPRuntimeConfig/Articles/EnvironmentVars.html

    For PATH in the Terminal, you should be able to set in .bash_profile or .profile (you'll probably have to create it though)

    For OS X v10.8 (Mountain Lion) and beyond you need to use launchd and launchctl.

    0 讨论(0)
  • 2020-11-21 05:51

    Here is a very simple way to do what you want. In my case, it was getting Gradle to work (for Android Studio).

    • Open up Terminal.
    • Run the following command:

      sudo nano /etc/paths or sudo vim /etc/paths

    • Enter your password, when prompted.

    • Go to the bottom of the file, and enter the path you wish to add.
    • Hit Control + X to quit.
    • Enter 'Y' to save the modified buffer.
    • Open a new terminal window then type:

      echo $PATH

    You should see the new path appended to the end of the PATH.

    I got these details from this post:

    Add to the PATH on Mac OS X 10.8 Mountain Lion and up

    0 讨论(0)
  • 2020-11-21 05:53

    Any of the Bash startup files -- ~/.bashrc, ~/.bash_profile, ~/.profile. There's also some sort of weird file named ~/.MacOSX/environment.plist for environment variables in GUI applications.

    0 讨论(0)
  • 2020-11-21 05:54

    Another, free, opensource, Mac OS X v10.8 (Mountain Lion) Preference pane/environment.plist solution is EnvPane.

    EnvPane's source code available on GitHub. EnvPane looks like it has comparable features to RCEnvironment, however, it seems it can update its stored variables instantly, i.e. without the need for a restart or login, which is welcome.

    As stated by the developer:

    EnvPane is a preference pane for Mac OS X 10.8 (Mountain Lion) that lets you set environment variables for all programs in both graphical and terminal sessions. Not only does it restore support for ~/.MacOSX/environment.plist in Mountain Lion, it also publishes your changes to the environment immediately, without the need to log out and back in. <SNIP> EnvPane includes (and automatically installs) a launchd agent that runs 1) early after login and 2) whenever the ~/.MacOSX/environment.plist changes. The agent reads ~/.MacOSX/environment.plist and exports the environment variables from that file to the current user's launchd instance via the same API that is used by launchctl setenv and launchctl unsetenv.

    Disclaimer: I am in no way related to the developer or his/her project.

    P.S. I like the name (sounds like 'Ends Pain').

    0 讨论(0)
  • 2020-11-21 05:55
    1. Do:

      vim ~/.bash_profile
      

      The file may not exist (if not, you can just create it).

    2. Type in this and save the file:

      export PATH=$PATH:YOUR_PATH_HERE
      
    3. Run

      source ~/.bash_profile
      
    0 讨论(0)
提交回复
热议问题