Adding home-brew to PATH

64,285

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

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

Now you should be able to run brew from anywhere.

Share:
64,285
Admin
Author by

Admin

Updated on July 15, 2022

Comments

  • Admin
    Admin almost 2 years

    I just installed Home-brew and now I'm trying to insert the home-brew 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?