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.
I managed to install yarn 4.x globally using Mise, a tool for configuring development environments.
mise use node@20.0.0
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.
which yarn
)