How to Install nvm, node and yarn for your Ruby on Rails Application
This gave me trouble — 2 days of it! — so when digging through some temporal c++ error messages became too much, I decided to un/reinstall everything and start over. So long as I had my package.json and yarn.lock files in tact (they were in source control), I’d be fine. So here’s what I did.
But first, a note before we start. The most critical piece of knowledge required to understand this sophisticated chain of dependencies was as follows:
The official package manager for node is npm which comes pre-installed with node. You use npm to install yarn — so you do things in this order nvm -> node/npm -> yarn
As well as this:
Yarn is a dependency manager that works similar to npm. It has its own list of commands, but you still have access to the same npm registry. Yarn was created to solve issues npm couldn’t, but both tools have evolved to the point where either will suffice today.
So let’s do it!
Step 1: Install nvm
From the nvm installation instructions, we install nvm like so
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
When that completes, open a new terminal and run
nvm --version
If it returns a version number, you’ve succeeded, and can move on to installing node/npm.
However, if you instead see this:
zsh: command not found: nvm
then try these suggestions:
Open ~/.zshrc
and add this to the bottom:
[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh # This loads NVM
Open a new terminal and try running nvm --version
again. Hopefully it works. If it doesn’t check out the troubleshooting tips here.
Step 2: Use nvm to install node/npm
To install node, simply run
nvm install --lts
To confirm it worked, run node --version
— if you see a version number, you’re in business!
Note that npm --version
should also now succeed.
Step 3: Use npm to Install yarn
To install yarn, simply run
npm install --global yarn
To confirm it worked, run yarn --version
— if that works, you’re good to go!
Summary
Congratulations, you just installed nvm, node+npm, and yarn, and you’re now ready to use them in a ruby on rails application!