Mac Os X Set Environment Variable For Application



Here we will talk about some things you’ll need for setting environment variables in Mac OS X.

Set Environment Variable For Windows. When we click to the Newin the system variables part we will see the following screen where we can set Variable name or Key and Variable value. Set Environment Variable For Windows. We can see that the system variable WEBSITE is added to the system variables. Setting Environmental Variables in Mac OS X Command Line Because the Mac defaults to using bash shell, you can set environmental variables in the user directories.bashprofile, for an active user account the path to that file is locate at.

Terminal

You will find Terminal in the Utilities folder in the Applications folder.

Print environment variables

Print all environment variables:

Mac Os X Set Environment Variable For Application

Print a specific environment variable:

Text Editors

Depending on which method you pick, you’ll need to edit some text files

GUI

TextWrangler

If you pick TextWrangler, we’ll be opening files with File > Open File by Name..

  • TextWrangler - Download

Xcode

If you pick Xcode, we’ll be opening files with File > Open Quickly...

If you use the ~/.MacOSX/environment.plist approach, you’ll need Xcode for editing plist files.

Mac
  • for Mac OS X 10.7 - Xcode 4 on the Mac App Store
  • for Mac OS X 10.6 - Xcode 3.2.6 and iOS SDK 4.3

In Terminal

The following editors are already installed.

  • vi/vim
  • pico/nano
  • emacs

You will need to use sudo for open some files, notably in /etc.

Example:

Parts of this series

  • HowTo: Set an Environment Variable in Mac OS X - Prerequisites

Question or issue on macOS:

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 also why there seem to be so many different ones (why? Do they do different things?)

So could someone please explain very patiently to a previous Windows user (wanting desperately to become more familiar with the pleasant if initially somewhat confusing OS X world) how to do this step by step?

I need the variables to be set both for GUI applications and command line applications, and at the moment it’s for an ant script that needs the variables, but there will most likely be other needs as well.

Please note that I have Lion too, since many of the answers you get Googling seem to be outdated for Lion…

Also note that I have practically zero experience using the Terminal. I’m willing to learn, but please explain for a novice…

How to solve this problem?

Solution no. 1:

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:

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:

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.

Solution no. 2:

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:

etc.

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

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.

Solution no. 3:

Here’s a bit more information specifically regarding the PATH variable in Lion OS 10.7.x:

If you need to set the PATH globally, the PATH is built by the system in the following order:

  1. Parsing the contents of the file /private/etc/paths, one path per line
  2. Parsing the contents of the folder /private/etc/paths.d. Each file in that folder can contain multiple paths, one path per line. Load order is determined by the file name first, and then the order of the lines in the file.
  3. A setenv PATH statement in /private/etc/launchd.conf, which will append that path to the path already built in #1 and #2 (you must not use $PATH to reference the PATH variable that has been built so far). But, setting the PATH here is completely unnecessary given the other two options, although this is the place where other global environment variables can be set for all users.

These paths and variables are inherited by all users and applications, so they are truly global — logging out and in will not reset these paths — they’re built for the system and are created before any user is given the opportunity to login, so changes to these require a system restart to take effect.

BTW, a clean install of OS 10.7.x Lion doesn’t have an environment.plist that I can find, so it may work but may also be deprecated.

Solution no. 4:

it prints current path value

Bundled Mac Os X Application

Then do vim ~/.bash_profile and write

here you are appending to the old path, so preserves the old path and adds your new path to it

then do

this will execute it and add the path

then again check with

Solution no. 5:

Unfortunately none of these answers solved the specific problem I had.

Here’s a simple solution without having to mess with bash. In my case, it was getting gradle to work (for Android Studio).

Btw, These steps relate to OSX (Mountain Lion 10.8.5)

  • Open up Terminal.
  • Run the following command:

    sudo nano /etc/paths (or sudo vim /etc/paths for vim)

  • 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.
  • Open a new terminal window then type:

    echo $PATH

You should see the new path appended to the end of the PATH

I got these details from this post: How to transfer office for mac to new computer.

I hope that can help someone else

Solution no. 6:

Simplified Explanation

This post/question is kind of old, so I will answer a simplified version for OS X Lion users.
By default, OSX Lion does not have any of the following files:

  • ~/.bashrc
  • ~/.bash_profile
  • ~/.profile

At most, if you’ve done anything in the terminal you might see ~/.bash_history

What It Means

You must create the file to set your default bash commands (commonly in ~/.bashrc). To do this, use any sort of editor, though it’s more simple to do it within the terminal:

  1. %> emacs .profile
  2. [from w/in emacs type:] source ~/.bashrc
  3. [from w/in emacs type:] Ctrl + x Ctrl + s (to save the file)
  4. [from w/in emacs type:] Ctrl + x Ctrl + c (to close emacs)
  5. %> emacs .bashrc
  6. [from w/in emacs type/paste all your bash commands, save, and exit]

The next time you quit and reload the terminal, it should load all your bash preferences. For good measure, it’s usually a good idea to separate your commands into useful file names. For instance, from within ~/.bashrc, you should have a source ~/.bash_aliases and put all your alias commands in ~/.bash_aliases.

Mac Os X Set Environment Variable For Application

Solution no. 7:

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

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

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.)

Solution no. 8:

Variable
Setup your PATH environment variable on Mac OS

Open the Terminal program (this is in your Applications/Utilites folder by default).
Run the following command

This will open the file in the your default text editor.

Mac Os X Set Environment Variable For Applications

For ANDROID SDK as example :

You need to add the path to your Android SDK platform-tools and tools directory. In my example I will use “/Development/android-sdk-macosx” as the directory the SDK is installed in. Add the following line:

Save the file and quit the text editor.
Execute your .bash_profile to update your PATH.

Now everytime you open the Terminal program you PATH will included the Android SDK.

Solution no. 9:

Adding Path Variables to OS X Lion

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

Mac Set Environment Variable Terminal

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

Solution no. 10:

Open Terminal:

Apply changing to system (no need restart computer):

(Also work with macOS Sierra 10.12.1)

Mac

Uninstall Mac Os X Applications

Hope this helps!