DEV Community

Muneer Syed
Muneer Syed

Posted on

How to install latest yarn version

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

yarn

Nodejs Installation Instructions

Ubuntu:

  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

NODE_MAJOR can be changed depending on the version you need.

NODE_MAJOR=16  
NODE_MAJOR=18  
NODE_MAJOR=20  
NODE_MAJOR=21
Enter fullscreen mode Exit fullscreen mode
  • Now run apt-get update and install nodejs
sudo apt-get update  
sudo apt-get install nodejs -y
Enter fullscreen mode Exit fullscreen mode

Uninstall:

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

Mac:

Install:

brew install node
Enter fullscreen mode Exit fullscreen mode

Uninstall:

brew uninstall node
Enter fullscreen mode Exit fullscreen mode

Yarn1.x Installation Instructions

Ubuntu:

  • Import yarn GPG key
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Now run apt-get update and install yarn
sudo apt-get update  
sudo apt-get install --no-install-recommends yarn -y
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Mac:

Install:

brew install yarn
Enter fullscreen mode Exit fullscreen mode

Uninstall:

brew uninstall yarn
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

npm install -g corepack
Enter fullscreen mode Exit fullscreen mode

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

mkdir yarn-project  
cd yarn-project
Enter fullscreen mode Exit fullscreen mode

Initialize the project to use the latest version of yarn

yarn init -2
Enter fullscreen mode Exit fullscreen mode

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

yarn set version stable  
yarn install
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)