Set environment variables on Mac OS X Lion

后端 未结 16 1226
余生分开走
余生分开走 2020-11-22 11:01

When someone says \"edit your .plist file\" or \"your .profile\" or \".bash_profile\" etc, this just confuses me. I have no idea where these files are, how to create them if

相关标签:
16条回答
  • 2020-11-22 11:34

    What worked for me is to create a .launchd.conf with the variables I needed:

    setenv FOO barbaz
    

    This file is read by launchd at login. You can add a variable 'on the fly' to the running launchd using:

    launchctl setenv FOO barbaz`
    

    In fact, .launchd.cond simply contains launchctl commands.

    Variables set this way seem to be present in GUI applications properly.

    If you happen to be trying to set your LANG or LC_ variables in this way, and you happen to be using iTerm2, make sure you disable the 'Set locale variables automatically' setting under the Terminal tab of the Profile you're using. That seems to override launchd's environment variables, and in my case was setting a broken LC_CTYPE causing issues on remote servers (which got passed the variable).

    (The environment.plist still seems to work on my Lion though. You can use the RCenvironment preference pane to maintain the file instead of manually editing it or required Xcode. Still seems to work on Lion, though it's last update is from the Snow Leopard era. Makes it my personally preferred method.)

    0 讨论(0)
  • 2020-11-22 11:37

    Adding Path Variables to OS X Lion

    This was pretty straight forward and worked for me, in terminal:

    $echo "export PATH=$PATH:/path/to/whatever" >> .bash_profile #replace "/path/to/whatever" with the location of what you want to add to your bash profile, i.e: $ echo "export PATH=$PATH:/usr/local/Cellar/nginx/1.0.12/sbin" >> .bash_profile 
    $. .bash_profile #restart your bash shell
    

    A similar response was here: http://www.mac-forums.com/forums/os-x-operating-system/255324-problems-setting-path-variable-lion.html#post1317516

    0 讨论(0)
  • 2020-11-22 11:41

    I had problem with Eclipse (started as GUI, not from script) on Maverics that it did not take custom PATH. I tried all the methods mentioned above to no avail. Finally I found the simplest working answer based on hints from here:

    1. Go to /Applications/eclipse/Eclipse.app/Contents folder

    2. Edit Info.plist file with text editor (or XCode), add LSEnvironment dictionary for environment variable with full path. Note that it includes also /usr/bin etc:

      <dict>
        <key>LSEnvironment</key>
        <dict>
              <key>PATH</key>
              <string>/usr/bin:/bin:/usr/sbin:/sbin:/dev/android-ndk-r9b</string>
        </dict>
        <key>CFBundleDisplayName</key>
        <string>Eclipse</string>
        ...
      
    3. Reload parameters for app with

      /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.fra‌​mework/Support/lsregister -v -f /Applications/eclipse/Eclipse.app
      
    4. Restart Eclipse

    0 讨论(0)
  • 2020-11-22 11:41

    Let me illustrate you from my personal example in a very redundant way.

    1. First after installing JDK, make sure it's installed.
    2. Sometimes macOS or Linux automatically sets up environment variable for you unlike Windows. But that's not the case always. So let's check it. The line immediately after echo $JAVA_HOME would be empty if the environment variable is not set. It must be empty in your case.

    3. Now we need to check if we have bash_profile file. You saw that in my case we already have bash_profile. If not we have to create a bash_profile file.

    4. Create a bash_profile file.

    5. Check again to make sure bash_profile file is there.

    6. Now let's open bash_profile file. macOS opens it using it's default TextEdit program.

    7. This is the file where environment variables are kept. If you have opened a new bash_profile file, it must be empty. In my case, it was already set for python programming language and Anaconda distribution. Now, i need to add environment variable for Java which is just adding the first line. YOU MUST TYPE the first line VERBATIM. JUST the first line. Save and close the TextEdit. Then close the terminal.

    8. Open the terminal again. Let's check if the environment variable is set up.

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