Tuesday, November 10, 2009

Setting environment variables in Mac OSX

Setting environment variables in Mac OSX is really simple (especially if you have any Unix/Linux knowledge).  Note that you can use method A or B, you shouldn't do both.  Also note that I don't go into detail about how or why you have to type certain things.  This post would be 5 times longer and more boring if I did.

A. Here is how to do it from the Terminal using echo statements (easiest)
  1. Click on the Spotlight icon in the far top right of your screen.
  2. Type Terminal and press Enter.
  3. Type echo KEY=VALUE >> .profile and press Enter. *
* If the VALUE part contains a space, you must enclose it in double quotes, but you must put a foward slash in front of each double quote (i.e. echo TEST=\"blah blah blah\" >> .profile).


B. Here is how to do it from the Terminal using the vi program (still easy)
  1. Click on the Spotlight icon in the far top right of your screen.
  2. Type Terminal and press Enter.
  3. Once inside Terminal, type vi .profile and press Enter.
  4. Press the i key once to go into INSERT mode.
  5. Type or copy/paste your export KEY=VALUE combinations. *
  6. Once you are finished, press Escape.
  7. Type :wq and press Enter (yes, that is a colon).
* Only enter one KEY=VALUE combination per line.  If the VALUE part contains a space, you must enclose it in double quotes (i.e. export TEST="blah blah blah").


To verify your new environment variable(s):
  1. Close Terminal and reopen it.
  2. Type echo $KEY and press Enter.

Here are the contents of my .profile file:
export CLICOLOR=1
export GOROOT=$HOME/go
export GOOS=darwin
export GOARCH=386
export GOBIN=$HOME/go/bin
export PATH=/opt/local/bin:/opt/local/sbin:$PATH:$GOBIN

The "export CLICOLOR=1" line makes directory listings (ls command) show directories and files in different colors like Linux does by default.


The following part is specific to setting Google Go environment variables.

The four lines that begin with "export GO..." are the four environment variables needed to install and use the new Google Go programming language.  Please note that you need to create the directories referred to in the $GOROOT and $GOBIN variables.  You also need to include the $GOBIN path in your $PATH variable. You do that by appending $GOBIN to the PATH=... line, but you must do this after you declare the $GOBIN environment variable.


The following command will create that directory.
mkdir $GOROOT

mkdir $GOBIN

To verify your Google Go variables, use this command:
env | grep GO


I hope this post has been a help to at least one person.  Comments, corrections, and suggestions are welcome.  Good luck!

3 comments:

  1. After you make the changes to your .profile you need to either open a new terminal or type the command "source .profile"

    ReplyDelete
  2. Yes, sorry, I was assuming that was understood in the "To verify your new environment variable(s)" section.

    ReplyDelete
  3. oops sorry, I think I forgot I read that...

    although I prefer just sourcing the .profile instead of closing and reopening terminal...

    ReplyDelete