Search code examples
yarnpkg-v4

How do I upgrade legacy yarn(1.22.x) to yarn 4.x globally?


As far as I know, npm install -g yarn or sudo apt install yarn installs the legacy yarn version which is 1.22.x. If you want to migrate to yarn 4.x for a specific project, it is possible using yarn set version stable (or replace stable with the specific version to upgrade to). However, my use case required installing yarn 4.x globally as opposed to per project(which I know is discouraged in general) and I could not find any information on how to do that.


Solution

  • I managed to install yarn 4.x globally using Mise, a tool for configuring development environments.

    1. Install mise
    2. mise use node@20.0.0
    3. mise use yarn@4.2.1

    After this, using yarn -v should show the correct version. Because I was using phpstorm, it only executes yarn from /bin/yarn, so I had to use a symlink to make it work.

    1. sudo ln -s /home/naomi/.local/share/mise/installs/yarn/4.2.1/bin/yarn /bin/yarn (replace the first path with where your yarn is installed. You can check the path using which yarn)
    2. ls -l /bin/yarn (ensure symlink is working)
    3. yarn -v (ensure yarn is working after symlink is established)