|
1 | 1 | #!/bin/bash |
| 2 | +set -uo pipefail |
| 3 | +echo "### $0 from pforret/macos_m1_cli" |
| 4 | + |
| 5 | +die(){ |
| 6 | + echo "$1" >&2 |
| 7 | + tput bel |
| 8 | + echo "Quitting script!" >&2 |
| 9 | + exit 1 |
| 10 | +} |
2 | 11 |
|
3 | 12 | if [[ -x "/opt/homebrew/bin/brew" ]] ; then |
4 | | - echo "Homebrew is already installed at [/opt/homebrew/bin], quitting script!" >&2 |
5 | | - /opt/homebrew/bin/brew update |
| 13 | + /opt/homebrew/bin/brew update > /dev/null |
6 | 14 | /opt/homebrew/bin/brew config | grep VERSION |
7 | | - exit 1 |
| 15 | + die "Homebrew is already installed at [/opt/homebrew/bin]" |
8 | 16 | fi |
9 | 17 |
|
| 18 | +# Sanity check, only run on MacOs in native mode |
| 19 | +[[ $(uname) == "Darwin" ]] || die "This script should only be run on a MacOS machine" |
| 20 | +[[ $(uname -m) == "arm64" ]] || die "This script should only be run in native mode, not with Rosetta 2" |
| 21 | +[[ $UID -eq 0 ]] && die "You should not be root to run this script" |
| 22 | + |
10 | 23 | # first run git to initiate download of developer tools |
11 | | -echo "* Check if git is installed ..." |
12 | | -git version || echo "If you get a popup asking to install 'developer tools', please say yes" >&2 |
| 24 | +echo "* If you get a MacOS popup asking to install 'developer tools', please confirm" |
| 25 | +git version |
13 | 26 |
|
14 | 27 | echo "* Create a folder for Homebrew ..." |
15 | 28 | cd /opt && sudo mkdir homebrew |
16 | | -if [[ ! -f "/opt/homebrew" ]] ; then |
17 | | - echo "Folder [/opt/homebrew] could not be created, quitting script!" >&2 |
18 | | - exit 1 |
19 | | -fi |
| 29 | +[[ -d "/opt/homebrew" ]] || die "Folder [/opt/homebrew] could not be created" |
| 30 | + |
20 | 31 | sudo chown -R "$(whoami)" /opt/homebrew |
21 | 32 |
|
22 | | -echo "* Download and install Homebrew ..." |
| 33 | +echo "* Download and install Homebrew from Github ..." |
23 | 34 | # cf https://docs.brew.sh/Installation. |
24 | 35 | curl -L "https://github.com/Homebrew/brew/tarball/master" | tar xz --strip 1 -C homebrew |
25 | 36 |
|
26 | | -if [[ ! -x "/opt/homebrew/bin/brew" ]] ; then |
27 | | - echo "Executable [/opt/homebrew/bin/brew] not found, quitting script!" >&2 |
28 | | - exit 1 |
29 | | -fi |
| 37 | +[[ -x "/opt/homebrew/bin/brew" ]] || die "Executable [/opt/homebrew/bin/brew] not found" |
| 38 | + |
| 39 | +startup_config(){ |
| 40 | + comment="#M1HBnat" |
| 41 | + echo "## /opt/homebrew is homebrew for native Apple ARM M1 mode $comment" |
| 42 | + echo "export PATH=/opt/homebrew/bin:\$PATH $comment" |
| 43 | + echo "alias brew1 \"brew --build-from-source\" $comment" |
| 44 | +} |
30 | 45 |
|
31 | 46 | case $(basename "$SHELL") in |
32 | 47 | zsh) |
33 | 48 | echo "* Adding [/opt/homebrew/bin] to your zsh startup config path" |
34 | | - echo "export PATH=/opt/homebrew/bin:$PATH" >> ~/.zshrc |
| 49 | + startup_config >> ~/.zshrc |
35 | 50 | ;; |
| 51 | + |
36 | 52 | bash) |
37 | 53 | echo "* Adding [/opt/homebrew/bin] to your bash startup config path" |
38 | | - echo "export PATH=/opt/homebrew/bin:$PATH" >> ~/.bashrc |
| 54 | + startup_config >> ~/.bashrc |
39 | 55 | ;; |
| 56 | + |
40 | 57 | *) |
41 | | - echo "Please add [/opt/homebrew/bin] to your path, could not be done automatically" >&2 |
| 58 | + echo "Add the following to your shell startup script, could not be done automatically" >&2 |
| 59 | + echo "#####" |
| 60 | + startup_config |
| 61 | + echo "#####" |
42 | 62 | esac |
43 | 63 |
|
44 | 64 | echo "* Running brew a first time to trigger compilation" |
45 | | -/opt/homebrew/bin/brewbrew install --build-from-source -q awk 2> /dev/null |
| 65 | +/opt/homebrew/bin/brew install --build-from-source -q awk 2> /dev/null |
46 | 66 |
|
47 | 67 | echo "* Homebrew was installed as native binary. Version will be > 2.6" |
48 | 68 | /opt/homebrew/bin/brew config | grep VERSION |
| 69 | +echo "* Installation using 'brew install' will give a warning as long as Homebrew is not yet officially released for M1" |
| 70 | +echo "* You can use 'brew1 install' to always build from source and skip that warning" |
| 71 | +echo "* You might get build errors, but remember: you are now doing bleeding edge CLI Terminal!" |
49 | 72 |
|
50 | | -echo "* Rosetta 2 should be 'false'" |
51 | | -/opt/homebrew/bin/brew config | grep Rosetta |
| 73 | +echo "> Close this Terminal and start a new one to make sure brew is in the path" |
0 commit comments