ChatGPT解决这个技术问题 Extra ChatGPT

Yarn: How to upgrade yarn version using terminal?

How should yarn be upgraded to the latest version?


J
Jimit Patel

For macOS users, if you installed yarn via brew, you can upgrade it using the below command:

brew upgrade yarn

On Linux, just run the below command at the terminal:

$ curl --compressed -o- -L https://yarnpkg.com/install.sh | bash

On Windows, upgrade with Chocolatey

choco upgrade yarn

Credits: Added answers with the help of the below answers

https://stackoverflow.com/a/54147594/842607

https://stackoverflow.com/a/53710422/842607


tagged for Ubuntu, not MacOs.
Google does not check the tag when routing users to this question.
You can do it faster with: (npm uninstall -g yarn && brew install yarn) || brew upgrade yarn
Note that installing yarn via brew also installs node. This may be undesired if, for instance, you are using nvm or n.
@LucianoBargmann I have brew on my Linux install so it's not a bad answer for these few people who upgrade in this very way.
P
Pang
npm install --global yarn
npm upgrade --global yarn 

This should work.


> Note: Installation of Yarn via npm is generally not recommended. When installing Yarn with Node-based package managers, the package is not signed, and the only integrity check performed is a basic SHA1 hash, which is a security risk when installing system-wide apps. yarnpkg.com/lang/en/docs/install/#alternatives-stable
I am pretty sure you can no longer install/upgrade yarn though npm, rather you need to do it throw brew on OSX at least.
Since this question was specifically about Ubuntu 16.04, comments about OSX and brew are not really relevant. I just did upgrade yarn through npm upgrade --global yarn on Ubuntu 16.04 where I had installed yarn through npm earlier. So pretty sure, it is still possible, even though that's not the recommended way. My original reason for installing yarn through npm was that I'm using nvm to keep multiple versions of node. Installing yarn through apt would have installed a specific version of node globally. But I read this can be avoided through the parameter --no-install-recommends.
@mnishiguchi the page you linked does not contain the quoted text. in fact, it contains the opposite statement: "It is recommended to install Yarn through the npm package manager"
Maybe things are different now in 2021 ¯_(ツ)_/¯
A
Agu V

Not remembering how i've installed yarn the command that worked for me was:

yarn policies set-version

This command updates the current yarn version to the latest stable.

From the documentation:

Note that this command also is the preferred way to upgrade Yarn - it will work no matter how you originally installed it, which might sometimes prove difficult to figure out otherwise.

Reference


Its also worth noting that this command creates .yarn/ and .yarnrc
It did not update Yarn globally, only in the current project.
This command didn't work for me, it return the error: ` error Couldn't find a package.json file in "/home/.."`
I guess it didn't work because I installed my yarn with apt-get. I think it is possible to know how you installed your package. For me, I do npm list -g >filename.txt, then I search the file for any package needed. For apt-get, I do apt list --installed, then I check the output for the package needed.
J
Janderson Silva

On Linux, just run below command at terminal:

$ curl --compressed -o- -L https://yarnpkg.com/install.sh | bash

After do this, close the current terminal and open it again. And then, run below command to check yarn current version:

$ yarn --version

Works on OSX as well
Works on git bash but shows the same older version when you run yarn --version on command prompt.
@NirajNiroula You will need to re-open the terminal in order to see any changes.
I tried this and it didn't work regardless of whether I opened a new terminal. For me npm install --global yarn worked in Ubuntu
A
April M. Clements

yarn policies set-version

will download the latest stable release

Referenced yarn docs https://yarnpkg.com/lang/en/docs/cli/policies/#toc-policies-set-version


Note that you can specify the version with this command too. e.g. yarn policies set-version 1.21.0
This command didn't work for me, it return the error error Couldn't find a package.json file in "/home/.."
This is typically used for setting a policy for a shared team project, not for updating/upgrading a global installation
P
Pang

For Windows users

I usually upgrade Yarn with Chocolatey.

choco upgrade yarn

Y
Yuriy Rypka

npm install -g yarn - solved the issue when nothing happened running npm update --global yarn.

Alternative method to update yarn: curl --compressed -o- -L https://yarnpkg.com/install.sh | bash.

Mac users with homebrew can run brew upgrade yarn.

More details here and here.


If npm update --global yarn didn't work but npm install -g yarn did it's possible it wasn't installed by npm in the first place.
thanks, it worked for me. upgraded to yarn 1.22.4 from yarn 1.19.1
I had to remove Yarn from Program Files (x86) first before installing with npm.
l
lfender6445

I had an outdated symlink that was preventing me from accessing the proper bin. I had also recently gone through a node upgrade which means a lot of my newer bins were available in a different folder with what i think was a lower priority

Here is what worked for me:

yarn -v 
> 1.15.2

which yarn
> /Users/lfender/.yarn/bin/yarn 

rm -rf /Users/lfender/.yarn/bin/yarn
npm uninstall --global yarn; npm install --global yarn

> + yarn@1.16.0
> added 1 package in 0.179s

which yarn
> /Users/lfender/.nvm/versions/node/v12.2.0/bin/yarn

yarn -v
> 1.16.0

If you are not using NVM, the location of your bin installs are likely to be unique to your system

From there, I've switched to doing yarn policies set-version as outlined here https://stackoverflow.com/a/55278430/1426788 to define my yarn version at the repo level


A
Ahmed

Works on all OS's

yarn set version latest
yarn set version from sources

Worked without the second line for me, but it is in the documentation for some reason.

Reference


Be careful. It works but broke global add for me.
P
Piyush Khera

According to https://yarnpkg.com/getting-started/install#updating-to-the-latest-versions

yarn set version <version>

For example to upgrade yarn v1.22.4 to v1.22.10:

yarn set version 1.22.10

Or latest: yarn set version stable
A
Afaq Ahmed Khan

Add Yarn Package Directory:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Install Yarn:

sudo apt-get update && sudo apt-get install yarn

Please note that the last command will upgrade yarn to latest version if package already installed.

For more info you can check the docs: yarn installation


This solution worked for me on Windows 10 ubuntu WSL terminal with zsh.
This solution works if you had installed yarn with apt-get. It worked for me on Ubuntu 16.04
M
Merabi Pkhaladze

I updated yarn on my Ubuntu by running the following command from my terminal

curl --compressed -o- -L https://yarnpkg.com/install.sh | bash

source:https://yarnpkg.com/lang/en/docs/cli/self-update


Thank you Merabi. The source is really useful.
N
NearHuscarl

If you already have yarn 1.x and you want to upgrade to yarn 2. You need to do something a bit different:

yarn set version berry

Where berry is the code name for yarn version 2. See this migration guide here for more info.


C
Chandresh Mishra

I tried all of the above solutions in Jenkins pipeline which needs the latest yarn. Finally, this worked for me.

Run yarn policies set-version in the git repo This will generate .yarn/releases/yarn-X.X.X.js file and .yarnrc file. Push both of these files in the Git repo. Now build and all the yarn commands will use the yarn-X.X.X version.

Note: This is helpful when you don't have root access to npm install -g yarn.


M
Mahdi Zarei
npm i -g yarn

This should update your yarn version. Check version with yarn -v or yarn --version.


yarn -v will be effective at new terminal tab
S
Sachin

yarn policies set-version

Use the above command in powershell to upgrade your current yarn version to Latest.It will download the latest yarn release


a
alfred carro

yarn policies set-version

this upgraded my yarn version from 1.22.5 to 1.22.10


R
Reettik Goswami

If You want to upgrade your yarn version from 1.22.5 to 1.22.10

yarn policies set-version


E
Ethan Cohen

This work for me to change yarn version 0.32 git to 1.22.5

https://www.codegrepper.com/code-examples/shell/yarn+0.32+git+ubuntu


Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
S
Stephen

Since you already have yarn installed and only want to upgrade/update. you can simply use

yarn self-update

Find ref here https://yarnpkg.com/en/docs/cli/self-update


This is not a solution. If you checked the page, it says "Note: self-update is not available. See policies for enforcing versions within a project"
why would they even have the page? that's confusing.
Why is this even a page. And it is a Google ranking page too. Ughh..