Skip to content

Instantly share code, notes, and snippets.

@cgons
Last active June 11, 2024 10:38
Show Gist options
  • Save cgons/d4e1d7c7f0f4e7e2de173852729fb2cc to your computer and use it in GitHub Desktop.
Save cgons/d4e1d7c7f0f4e7e2de173852729fb2cc to your computer and use it in GitHub Desktop.
Python Dev Setup for (M1) Macs

This document serves as a quick overview for setting up a new M1 Mac for Python based development.

  1. Install Docker Desktop https://www.docker.com/products/docker-desktop/

  2. Install xcode command line tools

xcode-select --install
  1. Install Homebrew (https://brew.sh)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. Install pyenv
# Install python build deps
brew install openssl readline sqlite3 xz zlib tcl-tk

brew install pyenv

Add this to your .bashrc or .zshrc (at the very end)

export PYENV_ROOT="$HOME/.pyenv"
eval "$(pyenv init -)"

For more details, see here: https://github.com/pyenv/pyenv#set-up-your-shell-environment-for-pyenv

  1. Install the latest python version (at the time of writing, 3.10.6) via pyenv
pyenv install 3.10.6
  1. Install Python Tools
pip3 install --upgrade pip

pip3 install --user poetry  # https://python-poetry.org

# Setup deps. for PG related tools (psycopg2)
brew install libpq --build-from-source
brew install openssl

# Add to your .bashrc / .zshrc
export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib -L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include -I/opt/homebrew/opt/libpq/include"

pip3 install --user pgcli  # https://github.com/dbcli/pgcli
  1. Install latest git
brew install git
  1. Instal NodeJS (LTS)
brew install node@16

# Allow npm packages to be installed globally without sudo
# See: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH

Optional Items

  1. Upgrade to the latest Bash and set it as your default shell.
brew install bash

sudo echo "/opt/homebrew/bin/bash" >> /etc/shells

chsh -s /opt/homebrew/bin/bash

Install + Setup bash-completion

# Since we're using Bash 5+, remove old completions and install the v2 version 
# which is recommended for new Bash versions
brew unlink bash-completion

brew install bash-completion@2
brew link bash-completion@2  # in case it wasn't linked already.

Add the folliowing to your .bashrc (after PATH is set):

[[ -r "$(brew --prefix)/etc/profile.d/bash_completion.sh" ]] && . "$(brew --prefix)/etc/profile.d/bash_completion.sh"

See below for sample .bashrc

  1. Nice to have tools and utils.
brew install wget
brew install htop
brew install tree
brew install coreutils

brew install jq     # https://github.com/stedolan/jq
brew install fzf    # https://github.com/junegunn/fzf
brew install exa    # https://github.com/ogham/exa

brew install --cask rectangle             # https://github.com/rxhanson/Rectangle
brew install --cask unnaturalscrollwheels # https://github.com/ther0n/UnnaturalScrollWheels
  1. Development tools to install manually

Sample bashrc

# Enable command history (up/down arrow keys)
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
bind 'set show-all-if-ambiguous on'
# bind 'set completion-ignore-case on'


# Setup Prompt
# ---
darkgrey='\[\033[01;38;5;244m\]'
grey='\[\033[01;38;5;248m\]'
yellow='\[\033[01;38;5;185m\]'
reset='\[\033[0m\]'

gitbranchname() {
  branchname=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
  if [ ! -z "$branchname" ]
  then
    echo "($branchname) "
  fi
}

export PS1="$darkgrey\h $grey[\w]$yellow \$(gitbranchname)\$$reset "
# ---


# Aliases
# ---------------
alias ls='exa -aF'
alias ll='exa -a --long --classify --group-directories-first --color-scale --header'


# Env. Vars.
# ---------------
export PIPENV_VENV_IN_PROJECT=true

# Work
export DKR_REGISTRY="<registry_id>.dkr.ecr.us-east-1.amazonaws.com"
export JFROG_USER=""
export JFROG_PASSWORD=""
export PIP_EXTRA_INDEX_URL=https://<username>:<password>@<company_name>.jfrog.io/<company_name>/api/pypi/pypi-local/simple


# Update PATH
# ---------------
PATH="/Users/<username>/Library/Python/3.8/bin:$PATH"
PATH="~/bin:/opt/homebrew/bin:$PATH"
PATH="~/.npm-global/bin:$PATH"
PATH="/opt/homebrew/opt/libpq/bin:$PATH"


# Add Coreutils to path
export PATH=$(brew --prefix coreutils)/libexec/gnubin:$PATH
export MANPATH=$(brew --prefix coreutils)/libexec/gnuman:$MANPATH

# ---

export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib -L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include -I/opt/homebrew/opt/libpq/include"

# Enable bash-completion (install bash-completion v2 first)
[[ -r "$(brew --prefix)/etc/profile.d/bash_completion.sh" ]] && . "$(brew --prefix)/etc/profile.d/bash_completion.sh"

# Enable pyenv
export PYENV_ROOT="$HOME/.pyenv"
eval "$(pyenv init -)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment