Main Tutorials

How to set environment variables on Mac OS X

In Mac OS X, you can set the environment variables in one of the following files :

  1. ~/.bashrc
  2. ~/.bash_profile
  3. ~/.profile

By default, Mac OS X does not has above files, you need to create it manually.

$PATH example

This example shows you how to set “mongodb/bin” folder to the existing $PATH environment variable.


$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

$ mongo -version
-bash: mongo: command not found

$cd ~
$pwd
/Users/mkyong
$touch .bash_profile
$vim .bash_profile

export MONGO_PATH=~/mongodb
export PATH=$PATH:$MONGO_PATH/bin

##restart your terminal
$ mongo -version
MongoDB shell version: 2.0.7

$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/mkyong/mongodb/bin

Done.

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Altaf
10 years ago

Could you also show detail steps of how to configure JAVA_HOME in Mac OS?

CrizZ
10 years ago
Reply to  Altaf

That’s almost the same. Just name your environment variable JAVA_HOME and get the path to your java directory:

export JAVA_HOME=/path/to/java
export PATH=$PATH:$JAVA_HOME

Kirk True
10 years ago
Reply to  CrizZ

Probably should be:

export PATH=$PATH:$JAVA_HOME/bin

Kirk True
10 years ago
Reply to  Kirk True

Or:

export JAVA_HOME=`/usr/libexec/java_home`