Question

I am using Vagrant to deploy a virtual machine with several installed packages using shell provisioning. One of the packages needs an update of path to be used properly which I wasn't able to do.

These are the contents of my Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"

#config.vm.network "forwarded_port", guest: 8888, host: 8888

config.ssh.forward_agent = true
config.vm.provision "shell", path: "provision.sh" 

end

These are the following things I tried:

  1. Create a separate .bashrc and .profile files with the following commands (appended at the end of file) and copy them into home directory:

    export PATH="/usr/local/x86_64/bin:$PATH"
    
  2. Try to write into .profile file:

    echo 'export PATH="/usr/local/x86_64/bin:$PATH"' >> .profile
    
  3. Just try exporting PATH during the provisioning (i.e. as a line of code in the provision.sh):

    export PATH="/usr/local/x86_64/bin:$PATH"
    

After vagrant up command finishes, this command does enable the change of path following vagrant ssh.

Was it helpful?

Solution

The problem was resolved with the following added to the provision.sh file based on this post:

echo PATH $PATH
[ -f ~/.profile ] || touch ~/.profile
[ -f ~/.bash_profile ] || touch ~/.bash_profile
grep 'PATH=/usr/local/x86_64/bin' ~/.profile || echo 'export PATH=/usr/local/x86_64/bin:$PATH' | tee -a ~/.profile
grep 'PATH=/usr/local/x86_64/bin' ~/.bash_profile || echo 'export PATH=/usr/local/x86_64/bin:$PATH' | tee -a ~/.bash_profile
. ~/.profile
. ~/.bash_profile
echo PATH $PATH

This works for the precise 64 box all the commands should be one line.

OTHER TIPS

Example using Ex/vi editor:

ex +'$s@$@\rexport PATH=/var/lib/vendor/bin:$PATH@' -cwq /etc/bash.bashrc

which appends:

export PATH=/var/lib/vendor/bin:$PATH

into global /etc/bash.bashrc file (so it's available for all users using bash shell, or use /etc/profile to use for all Bourne shells).

Alternatively use simply cat, e.g.:

cat >> ~/.bashrc <<EOF
export PATH=~/.composer/vendor/bin:\$PATH
EOF

If you need to have access to newly tool straight away, then you need to source the file.

If you're using composer, you may consider installing the binaries by specifying the requirements in composer.json, see some examples here, so in this case, you don't have to worry about configuring PATH variable.


If you're using Ansible playbooks, you can try using Files Modules with the following rule in yml file:

- name: Update bashrc to add new PATH entry:
    dest=/home/vagrant/.bashrc
    line="export PATH='/usr/local/x86_64/bin:$PATH'"
    regexp="^export PATH"
    owner=vagrant
    state=present
    insertafter=EOF
    create=True
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top