TransWikia.com

How do I set environment variables on OS X?

Ask Different Asked by akcasoy on February 9, 2021

I have always difficulties setting the Environment variables in OS X like JAVA_HOME, M2_HOME or PATH. How to set these on OS X Mavericks?

I did some research but the answers I’ve found didn’t really help me yet:

  1. export JAVA_HOME=/... (But it seems that the changes are temporary, just for one terminal session.)
  2. setenv JAVA_HOME=/... (But command not found.)
  3. Open .profile and write the variables inside to make the changes permanent- (.profile does not exist).
  4. Open .bash_profile and write the variables inside to make the changes permanent- (.bash_profile does not exist).
  5. vi ~/.bash_profile (Quite a challenge for somebody who doesn’t know vi.)
  6. Creating your own enrivonment.plist file.

Can somebody please walk me through the steps to get that to work on OS X Mavericks, assuming no Unix knowhow?

9 Answers

Open terminal

COMMAND-1

input : echo $SHELL output : /bin/zsh

if your machine uses zsh then use below commands|

Command - 2

vi ~/.zshrc

it will open new file or edits existing file.

now add below line and press esc and enter :wq to save and close the file. export JAVA_HOME=$(/usr/libexec/java_home)

Command 3:: to apply the changes

source ~/.zshr

Command 4::

echo $JAVA_HOME /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

you will see output as per above line.

Answered by Sai Ram on February 9, 2021

Create file on Home directory : .bash_profile (if not have)

Open file with text editor : vim / nano (This is a hidden file, may not visible on Finder)

Add env variables (add these lines) : export VARIABLE_NAME=VARIABLE_VALUE (Don't add spaces between variable name and value)

Save and Exit.

Run : source .bash_profile at home directory on terminal.

Check : Open new terminal and run echo $VARIABLE_NAME. (should display the VARIABLE_VALUE)

PS : These variables may not load for some IDEs, need to load with .env file.

Answered by Thushara Buddhika on February 9, 2021

In case you're using zsh like me, you need to modify ~/.zshrc.

Answered by shellbye on February 9, 2021

From http://hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac:

  • Open Terminal
  • Run touch ~/.bash_profile; open ~/.bash_profile
  • In TextEdit, add

    export PATH="$HOME/.rbenv/bin:$PATH"
    
  • Save the .bash_profile file and Quit (Command + Q) Text Edit.

  • Run source ~/.bash_profile

Answered by Sivathanu on February 9, 2021

In Yosemite, you should put export VARIABLE='something' inside .bash_profile.

I've tried writing the export VARIABLE='something' inside .profile without success.

Answered by Rafael Eyng on February 9, 2021

For adding a directory to a path, there is a better option in OS X: All entries in the file /etc/paths are added to the path, before any shell is started.

Edit this file with:

sudo pico /etc/paths

For more info, see: https://gist.github.com/Overbryd/1669348

Answered by Bjinse on February 9, 2021

For those who don't like to have the hassle with text files and editors, there is a GUI tool as well on GitHub.

Answered by mkraemerx on February 9, 2021

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.

Answered by Bhas on February 9, 2021

OSX has had only one change re environment variables and that was in Lion where ~/.MacOSX/environment.plist was removed. Although there also has been a change in the default shell in 10.2 or 10.3 from tsch to bash.

You need to define what you want the enviroment variable set for and what environment you have.

You also need to understand shells and Unix which by your comments you have not fully done. There are several nbash tutorials around.

As to your points

  1. You understand correctly assuming you are in a sh type shell (e.g. bash or zsh)
  2. This is for C shells e.g. tcsh so won't work on recent defaults.
  3. You need to create .profile then it works
  4. It is .bash_profile or better .bashrc and you need to create the file first
  5. Totally correct :) Use TextEdit or nano (or emacs)
  6. Correct up to OSX 10.7 and the wy to do this for programs called from the Workspace e.g. from Dock or Finder (or open)

So 3 or 4 work if you are calling the program from the command line (or from a program started in the command line but not by open)

As for programs from the GUI see this question

As for internet is full of rubbish - you need to have enough background to understand an article because as you have discovered many assume things or are incorrect. StackExhnage sites should be better as you can see if an answer has been agreed to from the number of votes.

Answered by mmmmmm on February 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP