Setting environment variables on OS X

后端 未结 30 3154
你的背包
你的背包 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:41

    Just did this really easy and quick. First create a ~/.bash_profile from terminal:

    touch .bash_profile
    

    then

    open -a TextEdit.app .bash_profile
    

    add

    export TOMCAT_HOME=/Library/Tomcat/Home
    

    save documement and you are done.

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

    After chasing the Environment Variables preference pane and discovering that the link is broken and a search on Apple's site seems to indicate they've forgotten about it... I started back onto the trail of the elusive launchd process.

    On my system (Mac OS X 10.6.8) it appears that variables defined in environment.plist are being reliably exported to apps launched from Spotlight (via launchd). My trouble is that those vars are not being exported to new bash sessions in Terminal. I.e. I have the opposite problem as portrayed here.

    NOTE: environment.plist looks like JSON, not XML, as described previously

    I was able to get Spotlight apps to see the vars by editing ~/MacOSX/environment.plist and I was able to force the same vars into a new Terminal session by adding the following to my .profile file:

    eval $(launchctl export)
    
    0 讨论(0)
  • 2020-11-21 05:44

    Sometimes all of the previous answers simply don't work. If you want to have access to a system variable (like M2_HOME) in Eclipse or in IntelliJ IDEA the only thing that works for me in this case is:

    First (step 1) edit /etc/launchd.conf to contain a line like this: "setenv VAR value" and then (step 2) reboot.

    Simply modifying .bash_profile won't work because in OS X the applications are not started as in other Unix'es; they don't inherit the parent's shell variables. All the other modifications won't work for a reason that is unknown to me. Maybe someone else can clarify about this.

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

    /etc/launchd.conf is not used in OS X v10.10 (Yosemite), OS X v10.11 (El Capitan), macOS v10.12 (Sierra), or macOS v10.13 (High Sierra).


    From the launchctl man page:

    /etc/launchd.conf file is no longer consulted for subcommands to run during early boot time;
    this functionality was removed for security considerations.
    

    The method described in this Ask Different answer works for me (after a reboot): applications launched from the Dock or from Spotlight inherit environment variables that I set in ~/Library/LaunchAgents/my.startup.plist. (In my case, I needed to set LANG, to en_US.UTF-8, for a Sublime Text plugin.)

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

    It's quite simple. Edit file .profile (vi, nano, Sublime Text or other text editor) file. You can found it at the ~/ directory (user directory) and set like this:

    export MY_VAR=[your value here]
    

    Example with Java home:

    export JAVA_HOME=/Library/Java/JavaVirtualMachines/current
    

    Save it and return to the terminal.

    You can reload it with:

    source .profile
    

    Or close and open your terminal window.

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

    Bruno is right on track. I've done extensive research and if you want to set variables that are available in all GUI applications, your only option is /etc/launchd.conf.

    Please note that environment.plist does not work for applications launched via Spotlight. This is documented by Steve Sexton here.

    1. Open a terminal prompt

    2. Type sudo vi /etc/launchd.conf (note: this file might not yet exist)

    3. Put contents like the following into the file

      # Set environment variables here so they are available globally to all apps
      # (and Terminal), including those launched via Spotlight.
      #
      # After editing this file run the following command from the terminal to update
      # environment variables globally without needing to reboot.
      # NOTE: You will still need to restart the relevant application (including
      # Terminal) to pick up the changes!
      # grep -E "^setenv" /etc/launchd.conf | xargs -t -L 1 launchctl
      #
      # See http://www.digitaledgesw.com/node/31
      # and http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x/
      #
      # Note that you must hardcode the paths below, don't use environment variables.
      # You also need to surround multiple values in quotes, see MAVEN_OPTS example below.
      #
      setenv JAVA_VERSION 1.6
      setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
      setenv GROOVY_HOME /Applications/Dev/groovy
      setenv GRAILS_HOME /Applications/Dev/grails
      setenv NEXUS_HOME /Applications/Dev/nexus/nexus-webapp
      setenv JRUBY_HOME /Applications/Dev/jruby
      
      setenv ANT_HOME /Applications/Dev/apache-ant
      setenv ANT_OPTS -Xmx512M
      
      setenv MAVEN_OPTS "-Xmx1024M -XX:MaxPermSize=512m"
      setenv M2_HOME /Applications/Dev/apache-maven
      
      setenv JMETER_HOME /Applications/Dev/jakarta-jmeter
      
    4. Save your changes in vi and reboot your Mac. Or use the grep/xargs command which is shown in the code comment above.

    5. Prove that your variables are working by opening a Terminal window and typing export and you should see your new variables. These will also be available in IntelliJ IDEA and other GUI applications you launch via Spotlight.

    0 讨论(0)
提交回复
热议问题