setup-node: Yarn is not installed

We are using a self-hosted runner and using this action to setup-node and install yarn.

I know on github-hosted runners, yarn is already pre-installed but it would be good if yarn was installed by this action for those of us using self-hosted.

Running setup-node with the following workflow and it works

 with:
    node-version: 12.13.0
    always-auth: true
    registry-url: https://registry.npmjs.org

/usr/bin/tar xz --warning=no-unknown-keyword -C /actions-runner/_work/_temp/febce1c0-314e-4a14-9a65-5bde8a990b71 -f /actions-runner/_work/_temp/d4bf33ab-571e-458e-9495-bd6c5b994814
/actions-runner/_work/_tool/node/12.13.0/x64/bin/node --version
v12.13.0
/actions-runner/_work/_tool/node/12.13.0/x64/bin/npm --version
6.12.0

Note that node and npm are installed.

A later step in the workflow then attempts to run yarn:

Run yarn install --ignore-scripts --frozen-lockfile
/actions-runner/_work/_temp/58e53b82-f9ae-4c68-9cec-75f75831208b.sh: line 1: yarn: command not found
##[error]Process completed with exit code 127.

This of course works on github hosted runners just fine, but since I just migrated to an EC2 instance that doesn’t have yarn installed, I am now having issues.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 143
  • Comments: 17

Commits related to this issue

Most upvoted comments

Workaround is to install yarn yourself using npm (after running this action)

- name: Install Yarn
   run: npm install -g yarn

and later can uninstall it -

- name: Uninstall Yarn
   if: always()
   run: npm uninstall -g yarn

While this works, I find it rather hacky. It would be good for this action to not need to rely on github’s instances to have yarn package installed.

EDIT: I wanted to add the documentation acts as if yarn is a given, since it has sections on usage with yarn Publish to npmjs and GPR with yarn:. Perhaps some clarification in that section that if you’re on self-hosted runners you will need to install yarn yourself via npm install -g yarn and uninstall it at the end of your job with npm uninstall -g yarn

Note that if you use with: cache: yarn this action will fail if you don’t have yarn pre-installed… But since you need node to install yarn… you can’t get out of this without running this action twice or using other actions.

While Node 17 isn’t LTS, moving forward with Node 18 LTS (or >=16.10 LTS) it would be great if corepack enable was automatically run if the cache is yarn or pnpm.

Corepack is part of the node install itself and contains both yarn and pnpm but is currently opt-in. It’s also the recommended way to use/install yarn 2+.

You can enable it manually in a single step which fixed the issue for me:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-node@v3
      with:
        node-version: '17'
        cache: 'yarn'
    - run: corepack enable
    - run: yarn install
    - run: yarn lint

GitHub Runners creates a file called .path in the same directory where you have config.sh. It contains the content of $PATH variable, which is further used during the execution of your workflows.

You have to add a directory with the yarn binary. In my case:

/home/github/.yarn/bin`

While Node 17 isn’t LTS, moving forward with Node 18 LTS (or >=16.10 LTS) it would be great if corepack enable was automatically run if the cache is yarn or pnpm.

Corepack is part of the node install itself and contains both yarn and pnpm but is currently opt-in. It’s also the recommended way to use/install yarn 2+.

You can enable it manually in a single step which fixed the issue for me:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-node@v3
      with:
        node-version: '17'
        cache: 'yarn'
    - run: corepack enable
    - run: yarn install
    - run: yarn lint

This doesn’t solve this issue. It’s only working for you because you’re using runs-on: ubuntu-latest which has yarn installed by default.

Hi everyone. I faced the same problem and this may sound really stupid, but I solved it this way:
(Thanks for the tip, @davide-bertola-deltatre)

jobs:
  node-ci:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v2

      - name: Setup Nodejs and npm
        uses: actions/setup-node@v2
        with:
          node-version: "14"

      - name: Setup yarn
        run: npm install -g yarn

      - name: Setup Nodejs with yarn caching
        uses: actions/setup-node@v2
        with:
          node-version: "14"
          cache: yarn

      - name: Install dependencies
        run: yarn
ss

NOTE: This way, if you run the yarn uninstall in the step, the Post process will not be able to find yarn and the job will fail.

Actions log shows that the cache was successful, but I am not sure if it is really cached in this case.

Cache Size: ~118 MB (124177777 B)
Cache saved successfully
Cache saved with the key: foo-bar-baz

When I connected to the runner instance that ran the job and ran the yarn command, I got not found, so everything seems to be working. However, this is not an essential solution and I also hope that yarn will be installed with it.

FYI: There isn’t a chicken-and-egg problem, you don’t need Node to install yarn:

curl -fsSL -o ~/bin/yarn \
  https://github.com/yarnpkg/yarn/releases/download/v1.22.17/yarn-1.22.17.js
chmod +x ~/bin/yarn

Hi, a lot of great answers here! Based on @jasonk’s solution, to run setup-node on a self-hosted runner we’re doing this (2023 edition):

...
- name: Install yarn
        run: |-
          curl -fsSL --create-dirs -o $HOME/bin/yarn \
          https://github.com/yarnpkg/yarn/releases/download/v1.22.19/yarn-1.22.19.js
          chmod +x $HOME/bin/yarn
          echo "$HOME/bin" >> $GITHUB_PATH

- name: Setup node
   uses: actions/setup-node@v3
   with:
      node-version: 16
      cache: 'yarn'
...

Curl needs the --create-dirs option, otherwise it cannot write to a not-yet-existing directory and complains with (23) Failed writing body. Also, you need to add whatever folder you’re copying the yarn executable into to $GITHUB_PATH in order for setup-nodeto pick it up.

GitHub Runners creates a file called .path in the same directory where you have config.sh. It contains the content of $PATH variable, which is further used during the execution of your workflows.

You have to add a directory with the yarn binary. In my case:

/home/github/.yarn/bin`

Thanks @pperzyna

Let me share my approach, maybe it will useful.

Find out where is the yarn

First on your server run: which yarn

then you got the yarn location, like this

/home/admin/.nvm/versions/node/v16.14.0/bin/yarn

copy the path (not including the yarn)

/home/admin/.nvm/versions/node/v16.14.0/bin

Edit path

run

nano .path

you got something like

aaaaa:bbbb:cccc

separated with :

then paste the /home/admin/.nvm/versions/node/v16.14.0/bin

so the .path become

aaaaa:bbbb:cccc:/home/admin/.nvm/versions/node/v16.14.0/bin

Don’t add the enter or something. Just give the : and paste next to it

Restart runner

You need restart it. so it will load the new path

Mostly you are running with svc.sh then stop it

sudo ./svc.sh stop

Run it again

sudo ./svc.sh start

I think the point of contention here is that we need to return the runner back clean. It’s easy enough to install the binaries, but given the hassle of making sure they’re purged every time, I’d prefer to see them included under Node’s control

@konsalex Since self-hosted runners are shared and are not a clean instance per job execution, like a github hosted runner would be, the job should do its best to clean itself up after a run so that each future workflow run is running on what amounts to a fresh instance.

If you install software during the run, such as yarn, redis, or terraform without uninstalling it, could potentially cause conflicts with other jobs running as the state of the instance moves further away from the original state.

Say then you try to upgrade versions of something in your workflow but since the old version is still installed from a previous run it may use that and create a difficult to debug situation.

As others have mentioned you can pre-install any software on a self-hosted runner so the workflow itself does not have to be responsible for installing anything, which would side-step this altogether.

Up to you, of course, how you best want to manage that 😃

https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners#differences-between-github-hosted-and-self-hosted-runners

Let me get this straight, the solution is for the consumers to:

  • download an orphaned yarn bin
  • append the bin location to $GITHUB_PATH
    • which will prove an agony if you hoped to at least keep that shit out of the workflow
  • run actions/setup-node
  • so that we can then exec $ yarn That’s quite an amusing procedure, I admit. So, what about those who are not using or have migrated from the legacy obsolete, unmaintained, unsupported, ancient yarn 1.* (that luckily doesn’t provide proper binaries for the last release, but rather the aforementioned orphaned bin we use as a workthefaroundabout) and have pretty much no choice but to use corepack - the official tool for managing node package managers - to install yarn? Are we gonna have to exploit quantum state to both install it and not install it for this shit to work, or we adding support for something that node took ownership of? It is not a separate concern anymore.

tldr. Anyways, done with the rant, debugging self-hosted runners combined with this, well, definitely doesn’t qualify as fun. I’ll put up a PR proposal for fixing this later/tomorrow, depending on how much of yarn installation control I end up having to expose.

I have the error yarn: command not found (just forked your repo on V3)

What is the best way to fix it ?

    # Install node.js and yarn
    - uses: Clovis-team/action-setup-node@v3
      with:
        node-version: '14'

    # Get yarn cache directory path in order to cache it
    - name: Get yarn cache directory path
      id: yarn-cache-dir-path
      run: |
        echo "::set-output name=dir::$(yarn cache dir)"
        echo "::set-output name=version::$(yarn -v)"
      shell: bash

Log in github:

Run Clovis-team/action-setup-node@v3
Attempting to download 14...
Acquiring 14.20.0 - x64 from https://github.com/actions/node-versions/releases/download/14.20.0-2633808318/node-14.20.0-linux-x64.tar.gz
Extracting ...
/usr/bin/tar xz --strip 1 --warning=no-unknown-keyword -C /home/runner/_work/_temp/7bbe90d2-dc57-4b39-b61c-14fb1db60e4f -f /home/runner/_work/_temp/b4832165-c982-406d-bb25-e1f89e2a8268
Adding to the cache ...
Done
Run echo "::set-output name=dir::$(yarn cache dir)"
/home/runner/_work/_temp/a152258c-d989-400f-a9b8-abc6252b12b7.sh: line 1: yarn: command not found
/home/runner/_work/_temp/a152258c-d989-400f-a9b8-abc6252b12b7.sh: line 2: yarn: command not found

@pperzyna thanks! I spent 30mins guessing why it was not working even after updating the path. Seems run.sh has to be restarted.