I have a .profile in my home directory; it contains many export … statements for environment variables.

You can create such a file by opening a Terminal and issuing the command touch .profile.

Close Terminal.

Then you should open that file in a plain-text editor (TextWrangler for example). You can also use nano .profile in a Terminal window (current directory should be your home), which is much easier than vi. Insert lines such as export JAVA_HOME=…. Save, exit nano if you used that and quit a running Terminal.

Open Terminal and issue the command env to see all environment variables. Check that the ones you defined have the value you assigned to them. You should be good to go now but don't forget that environment variables defined in .profile are not passed to GUI applications.

Answer from Bhas on Stack Exchange
🌐
Apple Support
support.apple.com › guide › terminal › use-environment-variables-apd382cc5fa-4f58-4449-b20a-41c53c006f8f › mac
Use environment variables in Terminal on Mac - Apple Support
For example, to set the variable PATH to the value /bin:/sbin:/user/bin:/user/sbin:/system/Library/, you would enter the following command in a Terminal window: % PATH=/bin:/sbin:/user/bin:/user/sbin:/system/Library/ export PATH ... When you launch an app from a shell, the app inherits much of the shell’s environment, including exported environment variables.
Discussions

java - How exactly to set up and use environment variables on a Mac? - Stack Overflow
What is the proper (2021 way) of creating a permanent environment variable on a Mac (macOS Big Sur) and then use it within a Java project. There are many very old posts regarding this topic. None o... More on stackoverflow.com
🌐 stackoverflow.com
macos - Set environment variables on Mac OS X Lion - Stack Overflow
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 I have to do that, etc, and a... More on stackoverflow.com
🌐 stackoverflow.com
macos - Setting environment variables on OS X - Stack Overflow
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: /etc/paths ~/.profile ~/.tcshrc I don't e... More on stackoverflow.com
🌐 stackoverflow.com
Add system-wide environment-variables in MacOS Ventura
I have an answer, but I have to preface it with bad news: This approach does not seem to bet working for PATH specifically, at least on Sonoma (macOS 14). I'm here because I found this thread while looking for solutions. BUT, it does work for every other environment variable. Here's what I've got: As you've noticed, your shell (and apps launched from the shell) have the environment variable values specified in your .zshrcfile, because the shell processes that file on startup. However, app launched any other way (e.g. from Finder) don't get those settings. You can use the launchctl command to specify session-wide environment variable values, which finder-launched apps will inherit. Here's an example of using it from a shell script I have: # Set environment variables /bin/launchctl setenv TEXINPUTS ":/Users/andersoe/academics/tools/LaTeX/packages/:/Library/Frameworks/R.framework/Resources/share/texmf/tex/latex/" /bin/launchctl setenv BIBINPUTS ":/Users/andersoe/academics/generated_bibfiles:/Users/andersoe/academics/research/bibfiles/". Note that this will not change the environment for any currently-running program, only what's inherited by apps launched from Finder after you make the change. You can check these values with a corresponding /bin/launchctl getenv . Now, the annoying part. These changes are not persistent across sessions (e.g. logging out, rebooting, etc.) . You have to re-do it every time. There is not, as far as I know, any quick convenient place to put a shell script where it will be automatically run for every session. The best way I know of is this PITA: Make a shell script that actually sets the variables. Mine is called mac_env_setup.zsh, and it looks like what I put up above. Use Script Editor to write a one-line AppleScript that calls the shell script. Mine is called "setup_wrapper.app" and it reads do shell script "/Users/andersoe/personal/macOS_setup/mac_env_setup.zsh". Then, critically, you save this as an "application" file format (not script, script bundle, etc.). Finally, you go into System Settings -> General -> Login Items, hit the "+" at the bottom of the "Open at Login" list, and then you can select your new "app" that runs your AppleScript that runs your shell script that sets your environment variables. Now, unlike, say .zshrc or .xinitrc or what have you, this is not guaranteed to be evaluated before any other program starts. AFAIK, the order in which Login Items and background tasks are started is not guaranteed, so any automatically-started apps may run with the default, un-customized environment variables. That might go for interactively-started applications as well, if you happen to be really quick and unlucky about starting them, but I've never had an issue. Anything I start after it's "done logging in" seems to always have inherited my newly-set variables. Finally, if you're interested, I have another layer of nonsense wrapped around this, an Ansible playbook (with a template file and an awk script on the side) that generates / updates my mac_env_setup.zsh, runs it, and checks whether or not my setup_wrapper.app is in the login items. (It does not generate or update the setup_wrapper.app script, or configure it as a login item, unfortunately, but it does fetch the list of what's configured and tell you whether or not the app is on it.) I'll post that here or e-mail it to you or whatever, if you want it. More on reddit.com
🌐 r/MacOS
7
6
April 19, 2023
🌐
Medium
medium.com › @schoolofthought.tech › how-to-set-environment-variable-in-apple-mac-m1-73c2c6b4e1c1
How to set Environment Variable in Apple mac M1 - School Of Thought - Medium
June 13, 2022 - How to set Environment Variable in Apple mac M1 Open Terminal Run touch ~/.bash_profile; open ~/.bash_profile or In case you’re using zsh, you need to modify touch ~/.zshrc; open ~/.zshrc In …
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › how to set environment variables in macos
How to Set Environment Variables in MacOS | phoenixNAP KB
January 27, 2025 - This tutorial explains how to view, add, and remove temporary and permanent environment variables on macOS.
🌐
Medium
youngstone89.medium.com › setting-up-environment-variables-in-mac-os-28e5941c771c
Setting up Environment Variables in Mac OS | by 김영석 | Medium
March 19, 2022 - Temporary Variable is gone now. ... For permanent setting, you need to understand where to put the “export” script. Here in this practice, you are going to edit “.bash_profile” file under your home directory. ... For experiment, I am going to add a test directory to the PATH environment variable.
Top answer
1 of 16
406

First, one thing to recognize about OS X is that it is built on Unix. This is where the .bash_profile comes in. When you start the Terminal app in OS X you get a bash shell by default. The bash shell comes from Unix and when it loads it runs the .bash_profile script. You can modify this script for your user to change your settings. This file is located at:

~/.bash_profile

Update for Mavericks

OS X Mavericks does not use the environment.plist - at least not for OS X windows applications. You can use the launchd configuration for windowed applications. The .bash_profile is still supported since that is part of the bash shell used in Terminal.

Lion and Mountain Lion Only

OS X windowed applications receive environment variables from the your environment.plist file. This is likely what you mean by the ".plist" file. This file is located at:

~/.MacOSX/environment.plist

If you make a change to your environment.plist file then OS X windows applications, including the Terminal app, will have those environment variables set. Any environment variable you set in your .bash_profile will only affect your bash shells.

Generally I only set variables in my .bash_profile file and don't change the .plist file (or launchd file on Mavericks). Most OS X windowed applications don't need any custom environment. Only when an application actually needs a specific environment variable do I change the environment.plist (or launchd file on Mavericks).

It sounds like what you want is to change the environment.plist file, rather than the .bash_profile.

One last thing, if you look for those files, I think you will not find them. If I recall correctly, they were not on my initial install of Lion.

Edit: Here are some instructions for creating a plist file.

  1. Open Xcode
  2. Select File -> New -> New File...
  3. Under Mac OS X select Resources
  4. Choose a plist file
  5. Follow the rest of the prompts

To edit the file, you can Control-click to get a menu and select Add Row. You then can add a key value pair. For environment variables, the key is the environment variable name and the value is the actual value for that environment variable.

Once the plist file is created you can open it with Xcode to modify it anytime you wish.

2 of 16
227

Your .profile or .bash_profile are simply files that are present in your "home" folder. If you open a Finder window and click your account name in the Favorites pane, you won't see them. If you open a Terminal window and type ls to list files you still won't see them. However, you can find them by using ls -a in the terminal. Or if you open your favorite text editor (say TextEdit since it comes with OS X) and do File->Open and then press Command+Shift+. and click on your account name (home folder) you will see them as well. If you do not see them, then you can create one in your favorite text editor.

Now, adding environment variables is relatively straightforward and remarkably similar to windows conceptually. In your .profile just add, one per line, the variable name and its value as follows:

export JAVA_HOME=/Library/Java/Home
export JRE_HOME=/Library/Java/Home

etc.

If you are modifying your "PATH" variable, be sure to include the system's default PATH that was already set for you:

export PATH=$PATH:/path/to/my/stuff

Now here is the quirky part, you can either open a new Terminal window to have the new variables take effect, or you will need to type .profile or .bash_profile to reload the file and have the contents be applied to your current Terminal's environment.

You can check that your changes took effect using the "set" command in your Terminal. Just type set (or set | more if you prefer a paginated list) and be sure what you added to the file is there.

As for adding environment variables to GUI apps, that is normally not necessary and I'd like to hear more about what you are specifically trying to do to better give you an answer for it.

Find elsewhere
Top answer
1 of 16
695

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.

2 of 16
323

Don't expect ~/.launchd.conf to work

The man page for launchctl says that it never worked:

DEPRECATED AND REMOVED FUNCTIONALITY

launchctl no longer has an interactive mode, nor does it accept commands from stdin. The /etc/launchd.conf file is no longer consulted for subcommands to run during early boot time; this functionality was removed for security considerations. While it was documented that $HOME/.launchd.conf would be consulted prior to setting up a user's session, this functionality was never implemented.

How to set the environment for new processes started by Spotlight (without needing to reboot)

You can set the environment used by launchd (and, by extension, anything started from Spotlight) with launchctl setenv. For example to set the path:

launchctl setenv PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

Or if you want to set up your path in .bashrc or similar, then have it mirrored in launchd:

PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
launchctl setenv PATH $PATH

There's no need to reboot though you will need to restart an app if you want it to pick up the changed environment.

This includes any shells already running under Terminal.app, although if you're there you can set the environment more directly, e.g. with export PATH=/opt/local/bin:/opt/local/sbin:$PATH for bash or zsh.

How to keeping changes after a reboot

New method (since 10.10 Yosemite)

Use launchctl config user path /bin:/usr/bin:/mystuff. See man launchctl for more information.

Previous method

The launchctl man page quote at the top of this answer says the feature described here (reading /etc/launchd.conf at boot) was removed for security reasons, so ymmv.

To keep changes after a reboot you can set the environment variables from /etc/launchd.conf, like so:

setenv PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

launchd.conf is executed automatically when you reboot.

If you want these changes to take effect now, you should use this command to reprocess launchd.conf (thanks @mklement for the tip!)

egrep -v '^\s*#' /etc/launchd.conf | launchctl

You can find out more about launchctl and how it loads launchd.conf with the command man launchctl.

🌐
Reddit
reddit.com › r/macos › add system-wide environment-variables in macos ventura
r/MacOS on Reddit: Add system-wide environment-variables in MacOS Ventura
April 19, 2023 -

I tried multiple things like adding variables to a plist-file or adjusting /etc/paths, but I didn't figure out how to add system-wide environment-variables in MacOS Ventura.

I switched over to MacOS from Linux and the PATH-variable has a different value in my shell vs. from outside of the shell.

I notice this issue especially in Kitty. This is expected (as stated by the author), if some of the env-variables are different in the shell vs. in the system.

Top answer
1 of 2
2
I have an answer, but I have to preface it with bad news: This approach does not seem to bet working for PATH specifically, at least on Sonoma (macOS 14). I'm here because I found this thread while looking for solutions. BUT, it does work for every other environment variable. Here's what I've got: As you've noticed, your shell (and apps launched from the shell) have the environment variable values specified in your .zshrcfile, because the shell processes that file on startup. However, app launched any other way (e.g. from Finder) don't get those settings. You can use the launchctl command to specify session-wide environment variable values, which finder-launched apps will inherit. Here's an example of using it from a shell script I have: # Set environment variables /bin/launchctl setenv TEXINPUTS ":/Users/andersoe/academics/tools/LaTeX/packages/:/Library/Frameworks/R.framework/Resources/share/texmf/tex/latex/" /bin/launchctl setenv BIBINPUTS ":/Users/andersoe/academics/generated_bibfiles:/Users/andersoe/academics/research/bibfiles/". Note that this will not change the environment for any currently-running program, only what's inherited by apps launched from Finder after you make the change. You can check these values with a corresponding /bin/launchctl getenv . Now, the annoying part. These changes are not persistent across sessions (e.g. logging out, rebooting, etc.) . You have to re-do it every time. There is not, as far as I know, any quick convenient place to put a shell script where it will be automatically run for every session. The best way I know of is this PITA: Make a shell script that actually sets the variables. Mine is called mac_env_setup.zsh, and it looks like what I put up above. Use Script Editor to write a one-line AppleScript that calls the shell script. Mine is called "setup_wrapper.app" and it reads do shell script "/Users/andersoe/personal/macOS_setup/mac_env_setup.zsh". Then, critically, you save this as an "application" file format (not script, script bundle, etc.). Finally, you go into System Settings -> General -> Login Items, hit the "+" at the bottom of the "Open at Login" list, and then you can select your new "app" that runs your AppleScript that runs your shell script that sets your environment variables. Now, unlike, say .zshrc or .xinitrc or what have you, this is not guaranteed to be evaluated before any other program starts. AFAIK, the order in which Login Items and background tasks are started is not guaranteed, so any automatically-started apps may run with the default, un-customized environment variables. That might go for interactively-started applications as well, if you happen to be really quick and unlucky about starting them, but I've never had an issue. Anything I start after it's "done logging in" seems to always have inherited my newly-set variables. Finally, if you're interested, I have another layer of nonsense wrapped around this, an Ansible playbook (with a template file and an awk script on the side) that generates / updates my mac_env_setup.zsh, runs it, and checks whether or not my setup_wrapper.app is in the login items. (It does not generate or update the setup_wrapper.app script, or configure it as a login item, unfortunately, but it does fetch the list of what's configured and tell you whether or not the app is on it.) I'll post that here or e-mail it to you or whatever, if you want it.
2 of 2
1
It should be in your .zshrc file in your home directory.
🌐
Configu
configu.com › home › setting env variables in windows, linux & macos: beginner’s guide
Setting Env Variables in Windows, Linux & MacOS: Beginner’s Guide - Configu
March 13, 2025 - Several environment variables are commonly used across operating systems to manage system behavior and application configurations: PATH: Defines directories where executable files are located, allowing commands to be run without specifying full file paths. HOME (Linux/macOS) / USERPROFILE (Windows): Specifies the current user’s home directory, where user-specific configuration files are stored.
🌐
Apple Community
discussions.apple.com › thread › 253888143
How can I set root environment variables in M1 Mac?
May 11, 2022 - You should be able to set the environment variables using the root login. For web proxies, it’d be more typical to set those using the system setup, or an MDM profile, or WPAD and the DHCP server. Change proxy settings in Network preferences on Mac - Apple Support
🌐
Medium
mamk2118.medium.com › setting-up-environment-variables-in-macos-mojave-and-mac-os-catalina-27ea1bb032f3
Setting up Environment Variables in MacOS Mojave and Mac OS Catalina
June 11, 2024 - You can also move some settings from a bash profile to a zsh profile without modification. For example, to set environment variables: export MY_SETTING=1.
🌐
OS X Daily
osxdaily.com › 2015 › 07 › 28 › set-enviornment-variables-mac-os-x
Where to Set Environment Variables on Mac
December 21, 2021 - The zsh shell sets environmental variables through the zshenv file, located in the users home directory at: ... Thus you can add zsh environmental variables by modifying that file with nano, vim, etc, or by using echo like so: ... Because the Mac defaults to using bash shell, you can set environmental variables in the user directories .bash_profile, for an active user account the path to that file is locate at:
Top answer
1 of 15
521

You have to add it to /etc/paths.

Reference (which works for me) : Here


These are the steps from the referenced article:

  • Open up Terminal.
  • Run the following command: sudo nano /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.

That’s it! To test it, in new terminal window, type: echo $PATH

2 of 15
76

I've found that there are some files that may affect the $PATH variable in macOS (works for me, 10.11 El Capitan), listed below:

  1. As the top voted answer said, vi /etc/paths, which is recommended from my point of view.

  2. Also don't forget the /etc/paths.d directory, which contains files may affect the $PATH variable, set the git and mono-command path in my case. You can ls -l /etc/paths.d to list items and rm /etc/paths.d/path_you_dislike to remove items.

  3. If you're using a "bash" environment (the default Terminal.app, for example), you should check out ~/.bash_profile or ~/.bashrc. There may be not that file yet, but these two files have effects on the $PATH.

  4. If you're using a "zsh" environment (Oh-My-Zsh, for example), you should check out ~./zshrc instead of ~/.bash* thing.

And don't forget to restart all the terminal windows, then echo $PATH. The $PATH string will be PATH_SET_IN_3&4:PATH_SET_IN_1:PATH_SET_IN_2.

Noticed that the first two ways (/etc/paths and /etc/path.d) is in / directory which will affect all the accounts in your computer while the last two ways (~/.bash* or ~/.zsh*) is in ~/ directory (aka, /Users/yourusername/) which will only affect your account settings.

Read more: Mac OS X: Set / Change $PATH Variable - nixCraft

🌐
Mac Install Guide
mac.install.guide › terminal › path
Mac Path · Command Line Guide · 2026 - Mac Install Guide
November 30, 2023 - It is one of the first tools you'll need for a local development environment for programming on a Mac. It's an easy way to install many useful software programs for the terminal, or command line. On Apple silicon (M1, M2, and M2 CPUs), Homebrew installs files into the /opt/homebrew/ folder, which is not part of the default shell $PATH. After you Install Homebrew, you must set the shell $PATH to use any software programs installed by Homebrew. Set the $PATH environment variable in the ~/.zprofile (Zsh Profile) file.
🌐
Medium
schathurangaj.medium.com › lets-add-an-environment-variable-to-your-macos-under-30-seconds-79dd3b0c5b9b
Let’s add an Environment Variable to your MacOS under 30 seconds | by S Chathuranga Jayasinghe | Medium
October 5, 2022 - I tried: ... And I ended up with ... Just open your terminal and run either of these commands: launchctl setenv environmentvariablename environmentvariablevalue...
🌐
Reddit
reddit.com › r/mac › how to permanently set environment variables
r/mac on Reddit: How to permanently set environment variables
January 24, 2023 -

I installed nvm on my MacBook, however once I open a new terminal I cannot call it via any nvm/node/npm command.

I always need to enter this into the console first:

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

That's not the normal behavior is it? And is there any way to avoid it?

🌐
Quora
quora.com › How-do-you-add-an-environment-variable-permanently-on-a-Mac-OS-X-or-Linux-computer
How to add an environment variable permanently on a Mac OS X or Linux computer - Quora
For scripts and cron jobs, set variables inside the script or via the environment file those services use. Avoid putting sensitive secrets in plain dotfiles if other users can read them. ... Create ~/Library/LaunchAgents/environment.plist as above, then log out and back in. These patterns cover typical needs across macOS and Linux; pick the file and method appropriate to the shell and whether GUI apps must see the variable.