How do I set environment variables on Mac OS X?

327 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 28 Jan 2021
Edited: MathWorks Support Team on 27 Feb 2023
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.
  1 Comment
Walter Roberson
Walter Roberson on 2 May 2018
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!