Adding Composer to Vagrant with Ansible

Published On: Thursday 10th of January 2019 17:48:09

Sometimes you might want to install Composer as part of your Vagrant setup. If you're using Ansible to provision your Vagrant box, this tutorial will show you how to quickly and easily install Composer as part of this process.

To begin with, we'll need to add the composer role so Ansible knows about it. Open up your ansible directory and add the following code to your playbok.yml, directly underneath the roles: section:

- composer

Next, open up the roles directory and inside of it, create a new directory called composer. Inside of composer add a directory called tasks and finally, add a file called main.yml inside of that. This should make the full path of: ansible/roles/composer/tasks/main.yml.

Open up the newly created main.yml and add the following code to it:

- name: Download Composer from
      dest: /tmp/installer.php
  - composer

- name: Perform the composer install
  command: "{{ item }}"
  - "php /tmp/installer.php"
  - mv composer.phar /usr/local/bin/composer
  - composer

- name: Delete the composer-installer.php after install
    path: /tmp/installer.php
    state: absent
  - composer

- name: Verify the version of Composer that was installed
  command: composer --version
  register: version
- debug: msg="{{ version.stdout_lines }}"

With that done, we can go ahead and provision the box. As Ansible works through its roles, you'll notice it hits our new Composer task and runs through our main.yml: Ansible installing composer

And that's it! Composer is now installed on your Vagrant machine via Ansible!