Installing Homebrew on Linux

I’m attempting to install Homebrew in a Linux build by doing the below: 

- name: Installing Homebrew
      run: |
        touch ~/.bash_profile ~/.profile
        /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
        test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
        test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
        test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >> ~/.bash_profile
        echo "eval \$($(brew --prefix)/bin/brew shellenv)" >> ~/.profile
        echo 'export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"' >> ~/.bash_profile
        source ~/.profile
        brew --version
    - name: Verify Homebrew's Installation
      run: brew --version

But I get the following error: 

line 1: brew: command not found

I did find out though if I run source ~/.profile && brew --version in the second step it  does work. My question is, is there a way to have source ~/.profile retained between each step in a job? Is there a better way to go about doing this? 

@destinyb-realityi ,

It is very strange, I tested the same installation steps on the on GitHub-hosted Ubuntu runner and my own  Ubuntu machine (a VM), on my own  Ubuntu  VM all the steps can work fine without errors, but on  GitHub-hosted Ubuntu runner  I also get the same error as your reported.

Below is the steps I use, I did a little changes based on yours:

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Installing Homebrew
        run: |
          /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
          test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
          test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
          echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile
          echo 'export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"' >>~/.bashrc
          echo 'export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"' >>~/.profile
          source ~/.bashrc
          source ~/.profile
          brew --version
      - name: Verify Homebrew's Installation
        run: |
          cat ~/.profile
          echo "======================="
          cat ~/.bashrc
          echo "======================="
          brew --version

You can see in the second step " Verify Homebrew’s Installation", I tried to print the contexts of the shell profile scripts, I can confirm the line " export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH" indeed has been added into shell profile scripts. But the command " brew --version" still always returns " brew: command not found".

I have reported this question to the appropriate engineering team for further investigation and evaluation. If they have any progress, I will notify you in time, and sometimes the appropriate engineers may directly reply you here.

@destinyb-realityi ,

According to the discussion with the appropriate engineers, the shell profile script ( .profile , .bashrc  or . bash_profile ) is only read for interactive login shells. more details, see here.

If you want to add  Homebrew to the PATH for all the subsequent steps in the same job, you can directly add Homebrew’s directory in  PATH  via the command " ::add-path::{path}", reference here:

echo "::add-path::/path/to/dir"

So the below workflow can work as expected:

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Installing Homebrew
        run: |
          /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
          test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
          test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
          echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile
          echo "::add-path::/home/linuxbrew/.linuxbrew/bin"
          brew --version
      - name: Verify Homebrew's Installation
        run: brew --version
2 Likes

Excellent! Thank you very much for your assistance :blush: