Search code examples
bashshellvagrantvagrantfilevagrant-provision

Vagrant provision shell, auto install Linuxbrew with command


I'm using this vagrantfile:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "ubuntu/trusty64"

  ...bla bla bla bla bla...

    config.vm.provision "shell", path: "provision/setup.sh"

end

Since I want to install Linuxbrew I have in my provision/setup.sh this code:

sudo apt-get update

sudo apt-get install --yes git-all libreadline-dev build-essential curl git m4 python-setuptools ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"

# or maybe also this: (but nothing anyway):
# sudo git clone https://github.com/Linuxbrew/linuxbrew.git /home/vagrant/.linuxbrew

export PATH=$HOME/.linuxbrew/bin:$PATH

brew doctor

But I retrieve errors:

==> default: /tmp/vagrant-shell: line 35: brew: command not found

How to fix this?


Solution

  • There's an issue how you run your script - as you run with config.vm.provision "shell", path: "provision/setup.sh" vagrant will run it as root user and so you do not need sudo

    however you should really run it as your user so do config.vm.provision "shell", path: "provision/setup.sh", privileged: false

    also the export will not be saved for your future session so add it to .bashrc file something like echo PATH=$HOME/.linuxbrew/bin:$PATH >> .bashrc so the final script would look like

    sudo apt-get update
    sudo apt-get install --yes git-all libreadline-dev build-essential curl git m4 python-setuptools ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
    
    yes | ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
    
    echo PATH=$HOME/.linuxbrew/bin:$PATH >> ~/.bashrc    
    export PATH=$HOME/.linuxbrew/bin:$PATH
    brew doctor
    

    The export is needed if you run brew from the script but note that brew doctor will likely ends up with warning and do not return so you might end up seeing vagrant message as

    The SSH command responded with a non-zero exit status. Vagrant
    assumes that this means the command failed. The output for this command
    should be in the log above. Please read the output to determine what
    went wrong.
    

    and finally for the original error, @BMW gets all credit adding yes | to the command will default the enter key on the question