Skip to content

Instantly share code, notes, and snippets.

@liviaerxin
Last active June 8, 2023 15:08
Show Gist options
  • Save liviaerxin/1b6f93481114345da14c82618cc642df to your computer and use it in GitHub Desktop.
Save liviaerxin/1b6f93481114345da14c82618cc642df to your computer and use it in GitHub Desktop.
Development Tools Setup in Apple Silicon #apple-silicon
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="~/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
# ZSH_THEME="robbyrussell"
ZSH_THEME="powerlevel10k/powerlevel10k"
# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"
# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"
# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"
# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"
# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13
# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"
# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"
# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"
# Uncomment the following line to display red dots whilst waiting for completion.
# Caution: this setting can cause issues with multiline prompts (zsh 5.7.1 and newer seem to work)
# See https://github.com/ohmyzsh/ohmyzsh/issues/5765
# COMPLETION_WAITING_DOTS="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"
# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
export NVM_LAZY=1
plugins=(
git
ruby
macos
dotenv
nvm
)
# Update oh-my-zsh by just offering a reminder every few days, if there are updates available
zstyle ':omz:update' mode reminder
source $ZSH/oh-my-zsh.sh
# User configuration
# export MANPATH="/usr/local/man:$MANPATH"
# You may need to manually set your language environment
# export LANG=en_US.UTF-8
# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='mvim'
# fi
# Compilation flags
# export ARCHFLAGS="-arch x86_64"
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
alias zshconfig="code ~/.zshrc"
alias ohmyzsh="code ~/.oh-my-zsh"
alias leg="arch -x86_64 zsh" # alias enter Rosetta terminal
alias pj='python -m json.tool' # pretty json
export KERNEL_NAME=$( uname | tr '[:upper:]' '[:lower:]' )
case $KERNEL_NAME in
'linux')
;;
'darwin')
;;
*) ;;
esac
# Multiple Homebrews on Apple Silicon
if [ "$(arch)" = "arm64" ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"
# export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib" # For compilers to find [email protected]
export DOTNET_ROOT="/usr/local/share/dotnet"
export PATH="$DOTNET_ROOT:~/.dotnet/tools:$PATH"
else
eval "$(/usr/local/bin/brew shellenv)"
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
# export LDFLAGS="-L/usr/local/opt/[email protected]/lib" # For compilers to find [email protected]
export DOTNET_ROOT="/usr/local/share/dotnet/x64"
export PATH="$DOTNET_ROOT:~/.dotnet/tools:$PATH"
fi
# Setting PATH for Python 3.7
export PATH="${PATH}:/Library/Frameworks/Python.framework/Versions/3.7/bin"
# Setting PATH for Python 3.9
export PATH="${PATH}:/Library/Frameworks/Python.framework/Versions/3.9/bin"
# Add Visual Studio Code (code)
export PATH="${PATH}:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
# remove duplicat entries from $PATH
# zsh uses $path array along with $PATH
typeset -U PATH path
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

Development Setup in Apple Silicon

Command Line Tools for Xcode and Rosetta2

# Rosetta2
❯ softwareupdate --install-rosetta  --agree-to-license
# Command Line Tools(including python3, clang and other useful libraries)
❯ xcode-select --install

Zsh and Oh-My-Zsh

> sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Notable Themes:

powerlevel10k is an awesome-looking theme for Zsh which can actually make your shell faster!

Configuring ~/.zshrc:

alias leg="arch -x86_64 zsh" # alias enter Rosetta terminal

Or if using many Alias and PATH setting, it's better to put them into a ~/.profile file, and add it to ~/.zshrc:

# before `~/.p10k.zsh`
[[ ! -f ~/.profile ]] || source ~/.profile

[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

add cpu_arch into POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS in ~/.pk10k.zsh to show whether x86 or arm cpu arch in mac m1.

typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(
    ...
    cpu_arch              # CPU architecture
    ...
)

Homebrew(Both Arm64 and X86_64)

  1. Native Homebrew

    ❯ arch --arm64 bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

    all Homebrew related stuffs are in /opt/homebrew.

  2. Rosetta Homebrew

    ❯ arch --x86_64 bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

    all Homebrew related stuffs are in /opt/local.

To switch between native and intel-emulated Homebrews based on different working architecture, you can add following into ~/.zshrc,

Configuring ~/.zshrc:

if [ "$(arch)" = "arm64" ]; then
    eval "$(/opt/homebrew/bin/brew shellenv)"
else
    eval "$(/usr/local/bin/brew shellenv)"
fi

So, in Rosetta terminal, we will have brew linked to /usr/local/bin/brew,

❯ leg
❯ arch
i386
❯ which brew
/usr/local/bin/brew

Mirrors:

Python3.7, Python3.8, Python3.9

Python3.8 and Python3.9 are natively supported in Applie Silcon,

❯ arch
arm64
❯ brew install [email protected] [email protected]
❯ file /opt/homebrew/opt/[email protected]/bin/python3
/opt/homebrew/opt/[email protected]/bin/python3: Mach-O 64-bit executable arm64

Python3.7, however and other belows will never be supported on Apple Silicon:

NOTE: There are no plans to backport support to 3.7 and 3.6 which are in the security-fix-only phase of their release cycles.

❯ leg
❯ arch
i386
❯ brew install [email protected]
❯ file /usr/local/opt/[email protected]/bin/python3
/usr/local/opt/[email protected]/bin/python3.7: Mach-O 64-bit executable x86_64

Configure ~/.zshrc:

if [ "$(arch)" = "arm64" ]; then
    eval "$(/opt/homebrew/bin/brew shellenv)"
    export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"
    export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"
else
    eval "$(/usr/local/bin/brew shellenv)"
    export PATH="/usr/local/opt/[email protected]/bin:$PATH"
fi

Python packages requiring compilation

# useful Python C-library compliation flags
export LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix zlib)/lib"
export CPPFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix zlib)/include"

NVM

Native nvm,

❯ arch
arm64
❯ brew install nvm

Configure ~/.zshrc:

export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && . "/opt/homebrew/opt/nvm/nvm.sh"  # This loads nvm
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && . "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

.NET

Here is the aim to install both arm64 and x64 respectively.

  1. Download .NET from website respectively for arm64 and x64 version, the former will be installed into "/usr/local/share/dotnet" and the later will be in "/usr/local/share/dotnet/x64".

  2. Delete the dotnet environment setting PATH, otherwise it will always prepend the arm dotnet path in first.

    ❯ cat /etc/paths.d/dotnet
    /usr/local/share/dotnet
    ❯ rm -rf /etc/paths.d/dotnet

    NOTE: this is because /usr/libexec/path_helper makes the trouble in macOS.

  3. Configure ~/.zshrc to use arm64 and x64 dotnet:

    # Multiple Homebrews on Apple Silicon
    if [ "$(arch)" = "arm64" ]; then
        eval "$(/opt/homebrew/bin/brew shellenv)"
        export DOTNET_ROOT="/usr/local/share/dotnet"
        export PATH="$DOTNET_ROOT:~/.dotnet/tools:$PATH"
    else
        eval "$(/usr/local/bin/brew shellenv)"
        export DOTNET_ROOT="/usr/local/share/dotnet/x64"
        export PATH="$DOTNET_ROOT:~/.dotnet/tools:$PATH"
    fi

References

Homebrew Documentation

How to migrate to native Homebrew on an M1 Mac
Python, Django, and React Development on Apple Silicon

My Ultimate M1 Mac Developer Setup

Properly setting $PATH for zsh on macOS (fighting with path_helper) · GitHub

Customize zsh prompt icon

Customize powerlevel10k OS_Icon to Prompt Whether in arm64 or x86_64 Termial in Apple Silicon #Apple Silicon.

Configure ~/.p10k.zsh:

  # Custom icon.
  if [[ "$(uname)" == "Darwin" ]]; then 
    typeset -g POWERLEVEL9K_OS_ICON_CONTENT_EXPANSION="$(uname -m) $(print_icon 'APPLE_ICON')" #'⭐'
  fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment