Setting Environment Variables in Mac OS X

How do you set environment variables in Mac OS X? Why would you need to set environment variables in Mac OS X anyway? You might not even need to do this at all, and in these tutorials we won’t be doing much with environment variables. We might need to set some variables when we use Maven. So read on, since it is good to know how to set environment variables in Mac OS X anyway, or if you are not interested, then you can skip to the next section now.

In Mac OSX there are a few ways to set environment variables:

2. in .bash_profile
3. in environment.plist

1. The TERMINAL way to set environment variables

Environment variables can be set in Mac OS X by simply opening a terminal window (Applications -> Utilities -> Terminal) and entering the following (for example)

$ export M2_HOME=/usr/local/apache-maven/apache-maven-3.0.3
$ env

If you need to make a variable available to an application, then you can launch the application from the terminal window.

However, those variables will only be available to that session of the terminal. When you close the terminal window, the variable will be gone. To make environment variables available to future sessions, then you can either set them in .bash_profile or environment.plist

2. The .bash_profile way to set environment variables

This is probably the easiest way to set environment variables so that they are available to all future TERMINAL sessions.

Open a new TERMINAL window and create a new .bash_profile file if you don’t have one already:

$ cd ~
$ touch .bash_profile

Now open the .bash_profile file with your favorite editor. You can find the file in this location:
Macintosh HD/Users/<your username>/.bash_profile

Now you can enter your environment variables in the new file:


export M2_HOME=/Users/username/somefolder/apache-maven-2.0.8
export M2=$M2_HOME/bin
export JAVA_HOME=/Library/Java/Home/
export PATH=$PATH:$M2

If you don’t see this file then you need to make your hidden files visible! Open a new TERMINAL window and enter the following:

$ defaults write AppleShowAllFiles TRUE
$ killall Finder

This will show you all of the hidden files and folders on your operating system. If you want to reverse the command replace TRUE with FALSE.

3. The environment.plist way to set environment variables

If you are looking for a visual tool to add, delete, and edit environment variables then you can install the XCode tools. It seems like a lot more trouble than it is worth in my opinion, but you can decide that yourself!

XCode installation

The Apple development environment toolset is called Xcode.To install XCode tools, do the following….

Put your MacOSX install disk into your drive. XCode should be included with all releases of Mac OS X but I can’t say that for sure.

When you open up the install DVD, you’ll see that in addition to the cool “Install Mac OS X” icon, there’s also a folder labeled “Optional Installs”. That’s what you want.

There’s an icon there labeled “Optional Installs.mpkg”. That’s not what you want, so don’t click on it. Instead, go into the folder “Xcode Tools”, where you’ll see three installers: “Dashcode.pkg”, “WebObjects.mpkg” and “XcodeTools.mpkg”. Double-click on “XcodeTools.mpkg” to get started.

environment.plist file

Now that you installed XCode, you can create your environment.plist file! Here’s how:

$ cd? ~
$ mkdir .MacOSX?
$ echo “” > .MacOSX/environment.plist
$ open .MacOSX/environment.plist

This should open your new, empty environment.plist file in the Property List Editor. Select Root and Add a Child: the Key should be the name of the variable you want to set; Type should be String; Value should be the value you want to set for this variable. Next time you open a new TERMINAL session, the variables should be set.

Environment variables set in environment.plist also appear in the standard bash environment. You can see all your environment variables using the shell command env from a TERMINAL window. (but you must restart the computer… that is a pain!)
Creating the TEST variable and restarting the computer and typing “env” into the terminal should result in the TEST variable being there.

yetanothermacuser wrote on 2012-09-18 at 21:41:09:
Mac OS X installed mvn under /usr/bin/mvn (I get that when I do a whereis mvn). In this case, what do I set the value of M2_HOME to?

Thomps wrote on 2012-02-10 at 17:19:28:
Hey thanks for this great article, now I understand this stuff a lot better! :)

Post a Comment