37

I just installed Homebrew and now I'm trying to insert the homebrew directory at the top of my path environment variable by typing in two commands inside my terminal. My questions are these:

What is a path environment variable?

Are the two codes provided me correct?

echo "export Path=/usr/local/bin:$PATH" >> ~/.bash_profile && source  ~/.bash_profile

After this I am to type in brew doctor. Nothing is happening as far as I can see. Can anyone offer me some advice or direction?

Greg Dubicki
  • 5,983
  • 3
  • 55
  • 68
artison66
  • 371
  • 1
  • 3
  • 5

5 Answers5

98

I installed brew in my new Mac M1 and ask me to put /opt/homebrew/bin in the path, so the right command for this case is:

echo "export PATH=/opt/homebrew/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
Lucas Tettamanti
  • 1,360
  • 8
  • 10
34

TL;DR

echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile

is what you want.

To answer your first question; in order to run (execute) a program (executable) the shell must know exactly where it is in your filesystem in order to run it. The PATH environment variable is a list of directories that the shell uses to search for executables. When you use a command that is not built into the shell you are using the shell will search through these directories in order and will execute the first matching executable it finds.

For example when you type: mv foo bar the shell is almost certainly actually using an executable located in the /bin directory. Thus fully the command is

/bin/mv foo bar

The PATH environment variable therefore saves you some extra typing. You can see what is in your PATH currently (as you can with all environment variables) by entering:

echo $<NAME OF VARIABLE>

So in this instance:

echo $PATH

As I mentioned earlier, ordering is important. Adding /usr/local/bin to the beginning of PATH means that the shell will search there first and so if you have an executable foo in that folder it will be used in preference to any other foo executables you may have in the folders in your path. This means that any executables you install with brew will be used in preference to the system defaults.

On to your second question. What the command you have provided is trying to do is add a line to your .bash_profile and then source it. The .bash_profile is a text file stored in your home directory that is sourced (read) every time bash (your shell) starts. The mistake in the line you've provided is that only the first letter of PATH is capitalised. To your shell Path and PATH are very different things.

To fix it you want:

echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile

To explain

echo "export PATH=/usr/local/bin:$PATH"

simply prints or echoes what follows to stdout, which in the above instance is the terminal. (stdout, stderr and stdin are very important concepts on UNIX systems but rather off topic) Running this command produces the result:

export PATH=/usr/local/bin:/opt/local/sbin:/opt/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

on my system because using $PATH within double quotes means bash will substitute it with its value. >> is then used to redirect stdout to the end of the ~/.bash_profile file. ~ is shorthand for your home directory. (NB be very careful as > will redirect to the file and overwrite it rather than appending.) && means run the next command is the previous is successful and

source ~/.bash_profile

simply carries out the actions contained in that file.

Peter Morris
  • 513
  • 3
  • 6
9

As per the latest documentation, you need to do this:

echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> $HOME/.bashrc
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

Now you should be able to run brew from anywhere.

Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
Dhruv Awasthi
  • 149
  • 1
  • 4
  • This is correct as same is displayed once you end up installing brew on Mac. Instructions are there 2 run the 2 commands with user name in the path. I am on M1 macbook pro, Monterey. – sud007 Jun 23 '22 at 03:50
  • It's important to point out to novice users (which might be target audience of this question) that your code snippet points to a file under your username (dhruv), which won't exist on someone else's computer. Perhaps you should consider replacing it by a generic string and pointing it out in your answer's description. – Luca Bezerra Jul 14 '23 at 23:43
4

When you type in a program somewhere and click enter, it checks certain locations to see if that program exists there. Linux brew uses locations different from the normal linux programs, so we are adding these locations to the ~/.profile file which sets the paths.

Run this in your terminal, and it will place the correct code in the .profile file, automatically.

echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile

Don't use .bash_profile because when you use something different from bash, like zsh, it may not work. .profile is the correct location.

music
  • 191
  • 5
0

Other answers did not work for me. The below commands were shown at the end of brew installation.

- Add Homebrew to your PATH in ~/.zprofile:
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

Executing these two commands, added brew to the PATH and is persisted across terminal sessions.

kaushalpranav
  • 1,725
  • 24
  • 39