How To Update Yarn

Last Updated on 2023-01-20

[#upgrading-yarn-globally]Upgrading Yarn globally[#upgrading-yarn-globally]

Use one of these commands:

 ## Installation Script
 $ curl -o- -L | bash -s -- --version [version]
 ## NPM $ npm install --global yarn@

You can see available versions in the official yarn GitHub repository.

[#Upgrading-yarn-for-specific-project]Upgrading Yarn for a specific project[#Upgrading-yarn-for-specific-project]

Yarn policies were created to upgrade Yarn to a specific version within a project.

 $ yarn policies set-version 

When running this command, the specified yarn version will be automatically downloaded from the official GitHub repository, and the version number present in the project's configuration file will be updated.

As long as this change has been added and merged into your project, your teammates working on the repository will be able to use a consistent version of Yarn.

[#yarn-self-update]But what about [.inline-code]yarn self-update[.inline-code]?[#yarn-self-update]

You may have seen references to the [.inline-code]yarn self-update[.inline-code] command across the web. In 2019, the yarn team decided to replace the [.inline-code]yarn self-update[.inline-code] command with [.inline-code]yarn policies set-version[.inline-code]. Per the documentation, [.inline-code]yarn policies[.inline-code] is now the preferred way to upgrade.

[#reinstalling-yarn-npm]Reinstalling Yarn with [.inline-code]npm[.inline-code][#reinstalling-yarn-npm]

Certain situations where you have multiple versions of yarn spread across projects may prevent you from upgrading to the intended yarn package. Here is one path to resolving this issue:

 ## Check the version of yarn 
 $ yarn -v
 ## Check for the location
 $ which yarn 
 ## Remove this reference
 $ rm -rf 
 ## Uninstall yarn globally and install the specific version
 $ npm uninstall --global yarn; npm install --global yarn@

[#troubleshooting]Troubleshooting upgrading Yarn[#troubleshooting]

Although the [.inline-code]yarn policies set-version[.inline-code] command is relatively stable, you may still encounter issues after upgrading yarn. Here are a few things you can try to do to fix them:

  1. Run the [.inline-code]yarn install[.inline-code] command again to reinstall the project's dependencies; which may help with compatibility problems.
  2. Verify the [.inline-code]yarn.lock[.inline-code] file has the contents you expect to see.
  3. Rely on the published release notes of Yarn and the new version for breaking changes and known issues.

If the above steps do not help, you might need to perform a rollback and downgrade the yarn version.

[#upgrading-yarn-1-to-2]Upgrading From Yarn 1 to Yarn 2[#upgrading-yarn-1-to-2]

There are inevitable breaking changes and differences between Yarn 1 (Classic) and Yarn 2. For example, [.inline-code]yarn upgrade[.inline-code] has been renamed to [.inline-code]yarn up[.inline-code] and [.inline-code]yarn install -- production[.inline-code] is now [.inline-code]yarn workspaces focus --all --production[.inline-code], which requires a separate plugin. 
Here is a quick summary on how to migrate to Yarn 2:

  1. Install Yarn globally to the latest Yarn 1
  2. Navigate to your project directory in the terminal
  3. Within that directory, set the Yarn version to V2 with [.inline-code]yarn set version berry[.inline-code]- this will download the specified version from the yarn repository, store it within your project, and update the settings.
  4. Run [.inline-code]yarn install[.inline-code]

Read Yarn's blog post on migrating for more detail, and to understand Yarn 2 limitations and troubleshooting steps.