When opening a new terminal homebrew loses it's path

I’m new to linux and decided to add homebrew. I’m using manjaro (arch based). I installed homebrew two different ways, by following the linux installation guide. Cloning the repo and the second way by copying and pasting on the terminal. However I have two different issues. When I just copied and pasted everything worked fine but when I opened a new terminal brew doesn’t work. I get “command not found”. I tried “echo $PATH” and got this:
"

home/xavi/.linuxbrew/bin:/home/xavi/.linuxbrew/sbin:/home/xavi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin" (before opening a new terminal and brew works).

Now on a new terminal run the “echo $PATH”, I get:

“/home/xavi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin”.

Which is just missing the first two paths.

Now when I install by cloning the repo, when I enter “brew” on terminal I get this error:

mkdir: cannot create directory ‘/opt/brew/var’: Permission denied
Error: Can’t create vendor-install-ruby lock in /opt/brew/var/homebrew/locks!
Fix permissions by running:
sudo chown -R $(whoami) /opt/brew/var/homebrew
Error: Failed to install Homebrew Portable Ruby and cannot find another Ruby 2.6!
If there’s no Homebrew Portable Ruby available for your processor:
-install 2.6 with your system package manager (or rbenv/ruby-build)
-make it first in your PATH
-try again"

Also, my ruby is: “ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]”.

Oddly enough when I enter the below instructions on terminal both issues are temporarily fixed till a new terminal is open.

test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)

test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)

test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile

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

I can see that copying and pasting sets the path and solves the issue on which ever way I isntalled. In other words my problem is I’m losing the path. I have also tried completely uninstalling both ways and reinstalling that hasn’t worked. Thank you for any help.

Please report the output of

echo $SHELL
ls ~/.bash_profile ~/.profile ~/.zprofile

Try logging out and back in, or restarting your computer. I suspect that will resolve the issue.

Hi, Thanks for the reply. For echo $Shell output is: /usr/bin/zsh
For ls ~/.bash_profile ~/.profile ~/.zprofile output is: ls ~/.bash_profile ~/.profile ~/.zprofile.
I’ve had this issue for a few days. I didn’t realize till I restarted my pc and then I realized it wasn’t only when I restarted it but when I opened a new terminal.

Ah. Did you choose zsh or was that the default on your system, and if so, which OS?

Try running…

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