How to install latest yarn version

yarn

To install the latest version of yarn4.x we need nodejs to be installed first and yarn1.x version

Nodejs Installation Instructions

Ubuntu:

Install:

  • Import nodesource GPG key
sudo apt-get update  
sudo apt-get install -y ca-certificates curl gnupg  
sudo mkdir -p /etc/apt/keyrings  
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg - dearmor -o /etc/apt/keyrings/nodesource.gpg
  • Add nodejs deb URL to the sources list
NODE_MAJOR=21  
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
NODE_MAJOR can be changed depending on the version you need.
NODE_MAJOR=16  
NODE_MAJOR=18  
NODE_MAJOR=20  
NODE_MAJOR=21
  • Now run apt-get update and install nodejs
sudo apt-get update  
sudo apt-get install nodejs -y

Uninstall:

sudo apt-get purge nodejs  
rm -r /etc/apt/sources.list.d/nodesource.list  
rm -r /etc/apt/keyrings/nodesource.gpg

Mac:

Install:

brew install node

Uninstall:

brew uninstall node

Yarn1.x Installation Instructions

Ubuntu:

Install:

  • Import yarn GPG key
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
  • Add yarn deb URL to the sources list
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  • Now run apt-get update and install yarn
sudo apt-get update  
sudo apt-get install --no-install-recommends yarn -y
We don’t want apt-get to install nodejs again as we already installed the latest version of nodejs from the above method, so we are specifying --no-install-recommends in apt-get to not install nodejs along with yarn.

Uninstall:

sudo apt-get purge yarn  
rm -r /etc/apt/sources.list.d/yarn.list

Mac:

Install:

brew install yarn

Uninstall:

brew uninstall yarn

Yarn4.x Installation Instructions

After following the above methods we assume that yarn1.x binary is added to your path, if not run the following command to add it to your path

corepack enable
If corepack is not installed already use npm to install it globally
npm install -g corepack

Now we have to create a project directory and switch to that directory

mkdir yarn-project  
cd yarn-project

Initialize the project to use the latest version of yarn

yarn init -2

If you want to update yarn to the latest version you need to run:

yarn set version stable  
yarn install

Now yarn will be configured to use the most recent stable binary

Yarn will periodically gather anonymous telemetry to disable it run
yarn config set --home enableTelemetry 0