vagrantcomposer-phpvagrant-windows

Installing Composer with Vagrant


I have successfully installed Vagrant along with some boxes on my Windows PC. I got to say it works awesome, creating and destroying VM's with different configurations on the fly.

The only problem I'm facing now is that I want to install composer. But composer requires you to point to the php.exe to do so. I don't want to install PHP on my computer, otherwhise there is no point using Vagrant, right. How do I tackle this problem?

I've seen some articles about using Puppet, but I couldn't make much sense out of them.

Thanks in advance.


Solution

  • You just need to install PHP (and curl) in your vagrant box. For instance, execute vagrant ssh to get SSH access to your box and execute the following commands:

    $ sudo apt-get install -y php5-cli curl
    $ curl -Ss https://getcomposer.org/installer | php
    $ sudo mv composer.phar /usr/bin/composer
    

    Now you're ready to use the composer command in your vagrant box.

    You can improve this by making this part of provisioning, the step where a box is set up when running vagrant up. To do this, put the above commands in a shell file (for instance project/vagrant/provision.sh):

    sudo apt-get install -y php5-cli curl > /dev/null
    curl -Ss https://getcomposer.org/installer | php > /dev/null
    sudo mv composer.phar /usr/bin/composer
    

    Now, configure this shell file as a provision step in your VagrantFile:

    Vagrant.configure("2") do |config|
    
      config.vm.box = "ubuntu/trusty64"
    
      # configure the shell file as a provision step:
      config.vm.provision :shell, path: "vagrant/provision.sh"
    
    end
    

    Now, when running vagrant init, the shell file is executed and php & composer are installed.

    You can also choose to use a box with php and composer pre-installed, like laravel/homestead.