How to upgrade yarn in CircleCi 2.0?

I’m trying to upgrade to CircleCI 2.0 for a Rails app. I used the automated config updater tool, and the auto-generated tool resulted in failures when the command bin/rake assets:precompile ran, with the error Webpacker requires Yarn >= 0.25.2 and you are using 0.21.3.

To fix it I tried manually updating yarn as one of my build steps, meaning my config file now starts like this:

version: 2
jobs:
  build:
    working_directory: ~/XX/YYY
    parallelism: 1
    shell: /bin/bash --login
    environment:
      CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
      CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
    docker:
    - image: circleci/build-image:ubuntu-14.04-XXL-upstart-1189-5614f37
      command: /sbin/init
    steps:
    - checkout
    - run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS
    - run:
        working_directory: ~/XX/YYY
        command: rm -f XX/YYY/.rvmrc; echo 2.3.1 > XX/YYY/.ruby-version; rvm use 2.3.1 --default
    - run:
        working_directory: ~/XX/YYY
        command: nvm install 8.9.4 && nvm alias default 8.9.4
    - run: curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
    - run: echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
    - run: sudo apt-get update && sudo apt-get install yarn && yarn -v

Given that sudo apt-get install yarn installs v1.5.1 (the last line of the output is Setting up yarn (1.5.1-1)), I would have assumed that the output of yarn -v would be v1.5.1. But actually the build fails on this command, with the error:

yarn install v0.21.3
[1/4] Resolving packages...
[2/4] Fetching packages...
error @rails/webpacker@3.0.2: The engine "yarn" is incompatible with this module. 
Expected version ">=0.25.2".
error Found incompatible module

I can’t for the life of me understand why yarn -v just after I installed v1.5.1 is prompting v0.21.3 to be installed, and why webpacker is getting involved at this stage. It looks like the first time I try to invoke yarn, something somewhere says to go fetch 0.21.3 - so how do I bump that number?

Thanks!

This build image comes preinstalled with yarn 0.21.3 and node 4.2.6. Installing via apt won’t remove them from your system or your PATH. In order to run a newer version of either, you need to (a) modify your PATH (b) fully-qualify the path to the executable, or (c) use another base image e.g. https://circleci.com/docs/2.0/circleci-images/#ruby