27

I tried using

brew install yarn@1.7.0 --without-node

or

brew install yarn@1.7.x --without-node

But I get the following error

Error: No available formula with the name "yarn@1.7.0" 
==> Searching for a previously deleted formula (in the last month)...
Warning: homebrew/core is shallow clone. To get complete history run:
  git -C "$(brew --repo homebrew/core)" fetch --unshallow

Error: No previously deleted formula found.
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
==> Searching taps on GitHub...
Error: No formulae found in taps.

I presume its possible to install different versions of yarn using brew ?

tk421
  • 5,775
  • 6
  • 23
  • 34
me-me
  • 5,139
  • 13
  • 50
  • 91
  • 1
    Why don't you use npm? `npm install yarn@1.7.0` – MoFarid Aug 20 '18 at 23:31
  • 2
    @MuhFred surely you were meant to add `-g`? Regardless [it's not recommended](https://stackoverflow.com/a/43377805/80428) – Jay Wick Nov 19 '18 at 03:11
  • `npm` is not a good option if you use `nvm` and want yarn to be installed globally regardless of which version of node happens to be active at any given moment. – Mark Edington Dec 04 '19 at 18:36

6 Answers6

33

I had the same issue and wanted to install yarn via brew for various reasons. Honestly the only way is to use the actual link to the yarn.rb file in the Homebrew Repo for the version you want. The easiest way to find the .rb file through git is to check out the PRs in Git for yarn in the homebrew repo.

  • Before doing this though, run brew unlink yarn in order to allow an older version to be installed while keeping the newest version.

  • Then look up the PR of the version you want, here's a link to make your life easier.

  • Click the version you want and go to Files Changed tab. Click on View File button. Then click on Raw button and then copy the URL of this raw file

  • After you get that link, type in your terminal brew install [link] and you should be set

You can then use brew list --versions yarn to check your installed versions and brew switch to switch versions. You should have both the latest version you previously had installed and the version you just installed.

Mim Cobaj
  • 331
  • 3
  • 3
  • Thanks, this saved me a lot of time. – Turnip May 23 '19 at 04:21
  • 2
    Here's a full example of the last step installing `yarn@1.17.3`, in case it was unclear which filename to use: `brew install https://raw.githubusercontent.com/dotiful/homebrew-core/2815d3ce3ea17202979f062bbcae78c96ebc0723/Formula/yarn.rb` – Drew Oct 02 '19 at 00:43
  • 1
    Great answer. Bonus points for info about `brew switch`! On a side note, if you want just yarn without node you now need to use `--ignore-dependencies` instead of `--without-node`. See: https://github.com/yarnpkg/website/issues/913 – Mark Edington Dec 04 '19 at 18:45
  • 1
    Here is one for `yarn@1.22.4`: `https://raw.githubusercontent.com/DanBuild/homebrew-core/2f1c19df0ac2f89a9996367f5ff4e61fe0be6d08/Formula/yarn.rb` – mateuscb Jun 06 '20 at 00:13
  • 2
    I tried to do this, `brew install https://raw.githubusercontent.com/DanBuild/homebrew-core/552359058976923c627d00e3dc99b40746618c7f/Formula/yarn.rb` and got this error: `Invalid usage: Installation of yarn from a GitHub commit URL is unsupported! brew extract yarn to a stable tap on GitHub instead. (UsageError)` – cheesydoritosandkale Apr 08 '21 at 14:05
10

Some of the above answers don't seem to work anymore. Here is how I was able to install a specific version in April 2021:

brew unlink yarn@1.6.0 (If you already have a version installed)

brew extract --version 1.22.4 yarn homebrew/cask

brew install yarn@1.22.4

yarn -v

  • 1
    This worked for me - the highest rated answer threw the error `Invalid usage: Installation of yarn from a GitHub commit URL is unsupported! brew extract yarn to a stable tap on GitHub instead. (UsageError)` – maxsmoove Jul 28 '21 at 15:32
5

You can also use yvm, a yarn version manager instead of homebrew to install a specific or multiple versions of yarn

https://yvm.js.org/docs/overview

Enables easy switching between yarn versions, like nvm does for node

iamogbz
  • 99
  • 1
  • 13
Lukas
  • 9,752
  • 15
  • 76
  • 120
3

Hope this works for you guys.

To reinstall run below.

// Note:(updating homebrew) for Mac users.

brew install -g yarn

if yarn is still not found

brew reinstall yarn

heyfranksmile
  • 169
  • 1
  • 5
1

As per official github page https://github.com/yarnpkg/yarn/issues/599 you should use "brew install -g yarn" to install yarn using brew.

PS: I've installed Xcode and gcc before running above command as i ran into few issues when executed above command.

Sandeep
  • 29
  • 2
  • The linked github page mentions `npm install -g yarn` not `brew install -g yarn`, but as someone mentions above [this isn't a recommended way to install](https://stackoverflow.com/a/43377805/80428). – contrebis Mar 26 '19 at 17:13
  • 1
    https://yarnpkg.com/en/docs/install#mac-stable . brew install yarn is the way to go – amar Aug 07 '19 at 04:26
1

You could also use yarn policies set-version <version>, but it has a caveat; it will "check in your Yarn release within your repository. Once you run it, your configuration will be updated in such a way that anyone running a Yarn command inside the project will always use the version you set - and this transparently."

You might not want to have the Yarn release in your repository.

Official doc

mynk
  • 11
  • 1