How can I run two isolated installations of Homebrew? - homebrew

I want to install and run two versions of Homebrew simultaneously on an Apple Silicon Mac: an ARM64 version, and an Intel version running under Rosetta 2.
I know I can prepend any brew command with arch --x86_64 to emulate Intel for that command, but this can lead to conflicts for formulas whose dependencies you already have built for ARM64. For example:
Error: gnupg dependencies not built for the x86_64 CPU architecture:
pkg-config was built for arm64
gettext was built for arm64
readline was built for arm64
openssl#1.1 was built for arm64
How can I install and run two separate, isolated versions of Homebrew (one for native ARM64 and one for emulated Intel), keeping each of their installed formulae and dependencies separate?

Install Homebrew natively on Apple Silicon (will install to /opt/homebrew by default):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Intel-emulated Homebrew (will install to /usr/local by default):
arch --x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
If you haven't yet installed Rosetta 2, you'll need to run softwareupdate --install-rosetta first.
Create an alias for Intel homebrew. I'm calling mine brow because O for old. But hey you do your own thing.
In ~/.zshrc (or your shell's equivalent) add:
alias brow='arch --x86_64 /usr/local/Homebrew/bin/brew'
Add ARM Homebrew to your PATH.
In ~/.zshrc (or your shell's equivalent) add:
# Homebrew on Apple Silicon
path=('/opt/homebrew/bin' $path)
export PATH
If you're still on bash it'd be PATH=/opt/homebrew/bin:$PATH
Confirm
which brew should return /opt/homebrew/bin/brew
brew --prefix should return /opt/homebrew
which brow should return brow: aliased to arch --x86_64 /usr/local/Homebrew/bin/brew
brow --prefix should return /usr/local
If you have the same command installed in both Homebrews, it'll default to Apple Silicon (/opt/homebrew/) since we prepended that one in our PATH. To override, run the command with its full path (/usr/local/bin/youtube-dl), or override your PATH for one command (PATH=/usr/local/bin youtube-dl).
I also created another handy alias in .zshrc (alias ib='PATH=/usr/local/bin') so I can prepend any Homebrew-installed command with ib to force using the Intel version of that command:
~ ▶ which youtube-dl
/opt/homebrew/bin/youtube-dl
~ ▶ ib which youtube-dl
/usr/local/bin/youtube-dl
If you prefer Intel to be the default brew, add /opt/homebrew/bin to the end of your PATH instead of the beginning.

Install Native Homebrew
❯ arch --arm64 zsh -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
all Homebrew related stuffs are in /opt/homebrew.
Install Rosetta Homebrew
❯ arch --x86_64 zsh -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
all Homebrew related stuffs are in /usr/local.
Configuring ~/.zshrc to use Brew defaultly based on arch,
# Multiple Homebrews on Apple Silicon
if [ "$(arch)" = "arm64" ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
export PATH="/opt/homebrew/opt/python#3.8/bin:$PATH"
# export LDFLAGS="-L/opt/homebrew/opt/python#3.8/lib" # For compilers to find python#3.8
else
eval "$(/usr/local/bin/brew shellenv)"
export PATH="/usr/local/opt/python#3.7/bin:$PATH"
export PATH="/usr/local/opt/python#3.9/bin:$PATH"
# export LDFLAGS="-L/usr/local/opt/python#3.7/lib" # For compilers to find python#3.7
fi
Test
❯ arch
arm64
❯ which brew
/opt/homebrew/bin/brew
❯ arch -x86_64 zsh
❯ arch
i386
❯ which brew
/usr/local/bin/brew
# set alias as you like
❯ rzsh='arch -x86_64 zsh'

Related

Error: Cannot install under Rosetta 2 in ARM default prefix (/opt/homebrew)

I tried to upgrade with Brew
$ brew upgrade stripe/stripe-cli/stripe
I got:
Error: Cannot install under Rosetta 2 in ARM default prefix (/opt/homebrew)!
To rerun under ARM use:
arch -arm64 brew install ...
To install under x86_64, install Homebrew into /usr/local.
AS suggested, I tried
$ arch -arm64 brew install
But got:
Error: Invalid usage: This command requires at least 1 formula or cask argument.
I tried installing Rosetta 2 according to this tutorial
$ /usr/sbin/softwareupdate --install-rosetta --agree-to-license
But I got:
Installing Rosetta 2 on this system is not supported.
Seems homebrew has to be used like this from now on:
arch -arm64 brew upgrade
I added the original argument to arch -arm64 brew upgrade and it worked
$ arch -arm64 brew upgrade stripe/stripe-cli/stripe
Well, all other answers seemed like a workaround for me, I would recommend to do this
cd /opt
sudo mkdir homebrew
sudo chown $(whoami):admin homebrew
curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
Then just reload terminal and your brew install xxx should work just fine.

How to fix LoadError ("symbol not found in flat namespace mysql_affected_rows") when building Rails app using Ruby 2.6.4 and mysql2 0.5.3 (arm64)

I am working on a Ruby on Rails application in a Mac M1 machine. Previously I was able to build this same application while on Ruby 2.5.5 and mysql2 0.5.1 (both in arm64), but an upgrade happened and now when I run bundle exec rails server I get this error:
require': dlopen(/vendor/bundle/ruby/2.6.0/extensions/-darwin-21/2.6.0/mysql2-0.5.3/mysql2/mysql2.bundle, 0x0009): symbol not found in flat namespace (\_mysql_affected_rows) - /vendor/bundle/ruby/2.6.0/extensions/-darwin-21/2.6.0/mysql2-0.5.3/mysql2/mysql2.bundle (LoadError)\
So far I've:
Verified both the Ruby 2.6.4 and mysql installed are on arm64
(I used brew and rbenv, checked path is correct- brew --prefix mysql returns /opt/homebrew/opt/mysql)
Tried setting different bundle config (mysql#5.7 and mysql#8.0)
For example, in .bundle/config I added:
BUNDLE_BUILD__MYSQL2: "--with-mysql-lib=/opt/homebrew/opt/mysql#8.0/lib --with-mysql-dir=/opt/homebrew/opt/mysql#8.0 --with-mysql-config=/opt/homebrew/opt/mysql#8.0/bin/mysql_config --with-mysql-include=/opt/homebrew/opt/mysql#8.0/include --with-ldflags=-L/opt/homebrew/opt/mysql-client#8.0/lib --with-cppflags=-I/opt/homebrew/opt/mysql-client#8.0/include"
I also tried running:
gem install mysql2 -v '0.5.3' -- --with-mysql-lib=/usr/local/opt/mysql#8.0/lib --with-mysql-dir=/usr/local/opt/mysql#8.0 --with-mysql-config=/usr/local/opt/mysql#8.0/bin/mysql_config --with-mysql-include=/usr/local/opt/mysql#8.0/include --with-ldflags=-L/usr/local/opt/mysql-client#8.0/lib --with-cppflags=-I/usr/local/opt/mysql-client#8.0/include
Delete /vendor/bundle/ruby/2.6.0 folder (which has all dependencies) and redo bundle install
Re-install Ruby 2.6.4 arm64
Re-install mysql arm64
After all these attempts I still run into the same load error ><
I had a similar problem with my Mac M2 due to installing MySQL 5.7 with Homebrew for ARM. Here are the steps I followed to fix it in case it can help someone:
Access terminal with Rosetta:
close terminal completely
navigate to Applications > Utilities > Terminal
Ctrl-Click and select "Get Info"
tick on "Open using Rosetta"
Open terminal and install Homebrew for x86_64: arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Add aliases:
switch architecture between ARM and x86_64
echo 'alias arm="env /usr/bin/arch -arm64 /bin/zsh --login"' >> ~/.zshrc
echo 'alias intel="env /usr/bin/arch -x86_64 /bin/zsh --login"' >> ~/.zshrc
use brew for ARM and brew for x86_64
echo 'alias mbrew='arch -arm64e /opt/homebrew/bin/brew'' >> ~/.zshrc
echo 'alias ibrew='arch -x86_64 /usr/local/bin/brew'' >> ~/.zshrc
Close terminal completely, uncheck "Open using Rosetta" and reopen terminal again
mbrew remove mysql#5.7
intel
ibrew install mysql#5.7
gem install mysql2 -v x.x.x -- --with-mysql-config=/usr/local/opt/mysql#5.7/bin/mysql_config --with-ldflags=-L/usr/local/opt/openssl#1.1/lib --with-cppflags=-I/usr/local/opt/openssl#1.1/include
You can now return to the ARM console by running arm, close terminal completely and tick "Open using Rosetta" again...

`brew upgrade <package>`: "<package> cannot be built with any available compilers"

For example, I tried to run brew upgrade ruby-build.
That failed with error:
CompilerSelectionError: ruby-build cannot be built with any available compilers.
Install GNU's GCC
brew install gcc
When I ran brew install gcc , I got:
Warning: Building gcc from source:
The bottle needs the Xcode CLT to be installed.
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
Error: An exception occurred within a child process:
CompilerSelectionError: gcc cannot be built with any available compilers.
Install GNU's GCC
brew install gcc
How can I upgrade ruby-build via brew?
I ran this command and it now works:
xcode-select --install
I had to reset xcode to the default command line tools path with
sudo xcode-select -r
for this to work

How to tell if homebrew is installed on Mac OS X

I am doing some Rails programming and I consistently see Homebrew referenced in solutions around the web but have never used it.
I also notice Homebrew in the terminal version 2.9 as an option next to "Shell -> New" from the terminal drop down but when I select homebrew and issue commands, they fail.
Usually with the "command not found" error.
Strangely enough I have been unable to locate a simple command to determine whether brew is installed or not.
How do I check to see if Homebrew is already installed on my Mac?
brew help. If brew is there, you get output. If not, you get 'command not found'. If you need to check in a script, you can work out how to redirect output and check $?.
I use this to perform update or install:
which -s brew
if [[ $? != 0 ]] ; then
# Install Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
brew update
fi
The standard way of figuring out if something is installed is to use which.
If Brew is installed.
>>> which brew
/usr/local/bin/brew
If Brew is not installed.
>>> which brew
brew not found
Note: The "not installed" message depends on your shell. zsh is shown above. bash will just not print anything. csh will say brew: Command not found. In the "installed" case, all shells will print the path.)
It works with all command line programs. Try which grep or which python. Since it tells you the program that you're running, it's helpful when debugging as well.
While which is the most common way of checking if a program is installed, it will tell you a program is installed ONLY if it's in the $PATH. So if your program is installed, but the $PATH wasn't updated for whatever reason*, which will tell you the program isn't installed.
(*One example scenario is changing from Bash to Zshell and ~/.zshrc not having the old $PATH from ~/.bash_profile)
command -v foo is a better alternative to which foo. command -v brew will output nothing if Homebrew is not installed
command -v brew
Here's a sample script to check if Homebrew is installed, install it if it isn't, update if it is.
if [[ $(command -v brew) == "" ]]; then
echo "Installing Hombrew"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
echo "Updating Homebrew"
brew update
fi
brew -v or brew --version does the trick!
I just type brew -v in terminal if you have it it will respond with the version number installed.
Location of the brew where it installed
which brew
version of home brew install
brew --version
[ ! -f "`which brew`" ] && echo "not installed"
Explaination: If brew is not installed run command after &&
brew doctor checks if Homebrew is installed and working properly.
use either the which or type built-in tools.
i.e.: which brew or type brew
Maybe your mac don't received the path
enter image description here
Run command below
eval "$(/opt/homebrew/bin/brew shellenv)"
And run to check that work
brew help
Beginners normally do, the homebrew --version which is wrong.
Do instead, brew --version. brew help works also. If these two commands are not executed, you don't have homebrew installed.
Another one possible way:
# Check if Ninja is installed
if ! which ninja > /dev/null
then
echo 'Ninja installation...'
brew install ninja
fi
In my case Mac OS High Sierra 10.13.6
brew -v
OutPut-
Homebrew 2.2.2
Homebrew/homebrew-core (git revision 71aa; last commit 2020-01-07)
Homebrew/homebrew-cask (git revision 84f00; last commit 2020-01-07)
Yes you can run which brew, but you may have it installed and it says it is not found if you are using zsh. You will need to add it to your .zshrc file.
I find it simple to use brew help command to find it is installed or not. There was a user guide on the homebrew download page.
If it is not installed then it will show 'command not found'
If you need to install homebrew then paste this on terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
jest write brew -v in terminal and if you have it , you will see there version number and the install date .
like this :
Homebrew 3.3.12
Homebrew/homebrew-core (git revision c3cacc9cd1d; last commit 2022-01-31)
Homebrew/homebrew-cask (git revision fb6ec06d8b; last commit 2022-01-31)
Once you install Homebrew, type command brew doctor in terminal.
If you get the following message:
Your system is ready to brew
then you are good to go and you have successfully installed homebrew.
If you get any warnings, you can try fixing it.
Another way to do it is using the "command" builtin tool
if [ "$(command -v brew)" ]; then
echo "command \"brew\" exists on system"
fi
in your terminal, do which brew and itll tell you where it was installed at within your computer, but itll only work in zsh not in bash.
Running Catalina 10.15.4 I ran the permissions command below to get brew to install
sudo chown -R $(whoami):admin /usr/local/* && sudo chmod -R g+rwx /usr/local/*

Error: Cowardly refusing to `sudo brew install` for installing glue 0.3 for Mac OSX Mountain Lion

I'm trying to install glue 0.3 for OXS Mountain Lion and ran into this error after installing homebrew (Error: Cowardly refusing to sudo brew install You can use brew with sudo, but only if the brew executable is owned by root. However, this is both not recommended and completely unsupported so do so at your own risk.) I got this error after the first step for installing glue 0.3 ($ sudo brew install jpeg). No idea how to fix this...please help!
sudo chown root /usr/local/bin/brew
sudo chown -R $USER /usr/local
Use the above command instead of changing the owner of brew to root.
This is the suggested method by the Owner of homebrew in https://github.com/Homebrew/homebrew/issues/9953
Answered by the developers here:
https://github.com/Homebrew/homebrew/issues/9953
I solved it by NOT using sudo, and changing the permissions on whatever file it says cannot be accessed without sudo.
For example, I could not access /usr/local/Cellar, so I entered the following (as specified here: https://github.com/Homebrew/homebrew/issues/3930)
sudo chmod g+w /usr/local/Cellar
sudo chgrp staff /usr/local/Cellar
For High Sierra macOS 10.13.3 or newer.
None of the solutions given here worked for me on this version. The only (and the best) fix is to uninstall brew and install it again with the following terminal commands. Note you will lose your current kegs, so you should get a list of the installed kegs, for reinstalling them after reinstalling brew as shown below under step 1. Actually, this was a good opportunity for me to get rid of those kegs that I am no longer using:
List your current kegs for reinstalling, those you wan't to keep:
brew list
Uninstall brew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
Reinstall brew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Optionally to prevent your usage being sent to Google Analytics (Update: Fortunately, now this has been stopped, so you may skip this step):
brew analytics off
Finally reinstall your previous kegs from step 1 (or those you still want to use). Replace the text with the square brackets with the list, (i.e. brew install sqlite heroku ):
brew install [list of kegs from step 1]
I have updated my macos to sierra and npm stopped working. Below are the steps followed to fix them.
Uninstall Node and install it from brew and follow the below steps
sudo chmod g+w /usr/local/Cellar
sudo chgrp staff /usr/local/Cellar
sudo chown root /usr/local/bin/brew
sudo brew postinstall node
This was helpful for me
http://digitizor.com/fix-cowardly-refusing-sudo-error-brew/
As quoted in the text, you essentially have to change the user and group of brew to root and wheel respectively.

Resources