Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GUIDE for mac OS X yarn nvm node install

GUIDE to install yarn, nvm (node) on macOS

last update: Dec 4, 2020

Assumptions:

  • macOS >= 10.14 (Mojave); tested with 10.15 (Catalina)
  • homebrew properly installed

Prepare before setup (cleanup)

brew uninstall --force yarn node npm  # remove previously installed node, npm, yarn
brew cleanup  # clean all broken symlinks and "waste" (not really required as of homebrew 2019)
brew update  # always good to have the latest

Cleanup previously installed node/npm config

If you used the instructions provided in this gist, then you need to do some more cleanup:

  1. in ~/.bashrc:
# remove all the lines below:
export NPM_PACKAGES....
export NODE_PATH....
# and remove all references to these variables later in the file
  1. delete ".npmrc": rm -f ~/.npmrc

  2. delete all existing installed global npm packages (! but make sure to write down if you're using any of them, to reinstall afterwards)

# !!! DESTRUCTIVE COMMAND, PAY ATTENTION !!!
sudo rm -rf /usr/local/npm_packages   #  !!! MAKE SURE YOU COPY THIS LINE WITH FULL ABSOLUTE PATH COMPLETELY !!!

Install yarn via homebrew

this section was updated Apr 2020

# install Yarn (will install node as a dependency, but that's OK)
brew install yarn

Install nvm

Always consult the latest README (Install section)

Install nvm version 0.37.2 (current: Dec 4 2020)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

Install node latest lts (fermium, v14.x, current on Oct 2020)

nvm install lts/fermium

Set nvm to use latest LTS as default for new bash sessions

echo "lts/fermium" > .nvmrc #  default to the latest LTS version
nvm alias default lts/fermium

RESTART all terminals => you're done ;-)

IMPORTANT NOTES

NEVER use sudo in any of the commands issued with node, yarn, or npm. If you need global packages installed, just follow nvm guidelines, and do yarn global add <package> or npm install -g <package>.

EXTRA: add fancy bash prompt to show ruby and node versions

...TODO... see .git-prompt-colors.sh

# This theme for gitprompt.sh is designed for dark color schemes
# it is clone of oh-my-zsh crunch theme style with exit status
override_git_prompt_colors() {
GIT_PROMPT_THEME_NAME="CrunchRC"
if [ -e ~/.rvm/bin/rvm-prompt ]; then
RUBY_PROMPT='{$(~/.rvm/bin/rvm-prompt v g)}'
else
if command -v rbenv > /dev/null; then
RUBY_PROMPT='{$(rbenv version | sed -e "s/ (set.*$//")}'
fi
fi
NODE_PROMPT='{$(nvm current)}'
Time12a="\$(date +%H:%M)"
GIT_PROMPT_STAGED="${Yellow}"
GIT_PROMPT_UNTRACKED="${Cyan}"
GIT_PROMPT_STASHED="${BoldMagenta}"
GIT_PROMPT_CLEAN="${Green}"
GIT_PROMPT_COMMAND_OK="${Green}"
GIT_PROMPT_COMMAND_FAIL="${Red}"
KERNEL_PROMPT='‹$(uname -r)›'
GIT_PROMPT_START_USER="${Magenta}rvm:${RUBY_PROMPT}; nvm:${NODE_PROMPT}\n_LAST_COMMAND_INDICATOR_${Cyan}${PathShort}${White}:"
GIT_PROMPT_START_ROOT="${GIT_PROMPT_START_USER}"
GIT_PROMPT_END_USER="${BoldBlue}${ResetColor}"
GIT_PROMPT_END_ROOT="${BoldRed} # ${ResetColor}"
GIT_PROMPT_LEADING_SPACE=0
GIT_PROMPT_PREFIX="${Green}["
GIT_PROMPT_SUFFIX="${Green}]"
GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING=""
}
reload_git_prompt_colors "CrunchCustom"
@fr-mccarty

This comment has been minimized.

Copy link

@fr-mccarty fr-mccarty commented Mar 12, 2019

Great! Thank you!

@armtuk

This comment has been minimized.

Copy link

@armtuk armtuk commented Apr 2, 2020

This no longer seems to work :/

Error: An exception occurred within a child process:
RuntimeError: /usr/local/opt/node not present or broken
Please reinstall node. Sorry :(

is what I get now.

@rcugut

This comment has been minimized.

Copy link
Owner Author

@rcugut rcugut commented Apr 2, 2020

@armtuk what command are you running to get this error?

@mhackersu

This comment has been minimized.

Copy link

@mhackersu mhackersu commented Apr 6, 2020

This no longer seems to work :/

Error: An exception occurred within a child process:
RuntimeError: /usr/local/opt/node not present or broken
Please reinstall node. Sorry :(

is what I get now.

Same here. Running brew install yarn --ignore-dependencies

@rcugut

This comment has been minimized.

Copy link
Owner Author

@rcugut rcugut commented Apr 6, 2020

gist updated today — April 6, 2020

@doanchungtu95

This comment has been minimized.

Copy link

@doanchungtu95 doanchungtu95 commented Apr 20, 2020

MacOS Catalina use zsh instead of bash
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh | zsh

@benlac

This comment has been minimized.

Copy link

@benlac benlac commented May 11, 2020

Helpful ! Thanks for this.

@hamzahkhan

This comment has been minimized.

Copy link

@hamzahkhan hamzahkhan commented Jan 14, 2021

MacOS Catalina use zsh instead of bash
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh | zsh

Is it ok, if I use bash? whats recommended?

@rcugut

This comment has been minimized.

Copy link
Owner Author

@rcugut rcugut commented Jan 14, 2021

@hamzahkhan I use bash, as personal and biased preference. I've been using bash for 16+ years, since my days of linux sysadmin work :-)

Here's an updated guide on how to install it on macOS, if you want to:
https://github.com/onpageideas/the-well-grounded-dev-guides/blob/master/GUIDE__macos-bash-terminal.md

@AlphaZach

This comment has been minimized.

Copy link

@AlphaZach AlphaZach commented Mar 18, 2021

Hi,
I followed the steps above, but it showed "nvm: command not found" after I reopened the terminal and run "nvm --version"
Do you know what might be going wrong?
Thanks!

@rcugut

This comment has been minimized.

Copy link
Owner Author

@rcugut rcugut commented Mar 18, 2021

I followed the steps above, but it showed "nvm: command not found" after I reopened the terminal and run "nvm --version"

@AlphaZach are you using bash or zsh as your default interpreter? See the comments above about bash and zsh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment