MATLAB Answers

How do I set environment variables on Mac OS X?

100 ビュー (過去 30 日間)
How do I set environment variables on Mac OS X?

採用された回答

MathWorks Support Team
MathWorks Support Team 2015 年 8 月 19 日
There are multiple ways of setting environment variables on OS X, but the most reliable and supported way is to use a plist file and have system's launchctl service load it at login. This method is described below; for other methods please consult OS X product documentation.
Create a new .plist file. The name doesn't matter, but the extension must be .plist. For example, "SampleVariable.plist".
Save the file in ~/Library/LaunchAgents/ so it is read when when you log in with your specific username.
Paste the following contents into the .plist file you just created:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>Change To Label Of Your Choice</string>
<key>ProgramArguments</key>
<array>
<string>/bin/launchctl</string>
<string>setenv</string>
<string>Variable Name You Want To Set</string>
<string>Variable Value You Want to Set</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
 
There are three things you need to change before saving the .plist file: Label, Variable Name, and Variable Value. Make the changes, save the file and reboot(or see alternate method below) your Mac for the changes to take effect.
Please note this sets a user specific variable which will apply only to the user saving this file in their home directory.
Sample .plist file can be downloaded below.
------------------------------------------------------------------------------------------------------------------------------------------------ How the plist works:
This file tells /bin/launchctl to run commands in the next 3 strings below. Setenv is used to set an environment variable in OS X Terminal. Variable name has to be specific to whatever you are trying to set, it cannot be a random name you pick. For example, it can be either MLM_LICENSE_FILE or MATLAB_JAVA. Variable value has to indicate the path or parameter you want to change, such as port@host for MLM_LICENSE_FILE or path to a different version of Java for MATLAB_JAVA.
Alternate method to load the plist into your user account:
Instead of rebooting your computer you can run the following command in Terminal: launchctl load ~/Library/LaunchAgents/EnterNameOfYourFileHere.plist
This will load it into the system, but to check for it you will need to close your current Terminal session and open a new one, then run command printenv. You should then see your variable listed.
You can also unload it from the system by either running launchctl unload ~/Library/LaunchAgents/EnterNameOfYourFileHere.plist or removing it from that directory and restarting your computer.

  2 件のコメント

Abdul Majeed Mohammed
Abdul Majeed Mohammed 2018 年 5 月 1 日
Thanks! I followed these instructions previously and they worked like a charm. Unfortunately these no longer work on OS X High Sierra (10.13.4). Any suggestions? I don't see the variable when I type printenv Thanks!
Walter Roberson
Walter Roberson 2018 年 5 月 2 日
Abdul Majeed Mohammed: is it DYLD_LIBRARY_PATH that you are trying to set? The rules about how that is used changed, but the way of setting environment variables did not change.

サインインしてコメントする。

その他の回答 (1 件)

Tech Support
Tech Support 2018 年 5 月 2 日
Hi,
If you are still experiencing this issue, please contact MathWorks support.
-Justin

  0 件のコメント

サインインしてコメントする。

タグ

タグが未入力です。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by