In Bash I usually install Composer this way:

curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer

I tried to read in the Ansible Composer module documentation just for a hint on what is the recommended way to install Composer with Ansible but I found none.

I just want to make sure that each time I install Composer it will be installed by the latest installation-command aside from being continuously upgraded by Ansible from my machine via SSH.

How could I do it in a Ansible "state: latest" way (as common with Ansible's apt module)?

  • What’s wrong with having Ansible execute the two commands you provided to install Composer?
    – Peschke
    Dec 16, 2018 at 3:31
  • Nothing enough wrong, I can do that with the command module but let's assume the unlikely happened and "tomorrow" the command will no longer be stable, how could I ensure I up2date with that command?
    – user149572
    Dec 16, 2018 at 3:47

4 Answers 4



ansible-galaxy install geerlingguy.composer

This will add geerlingguy.composer to your roles, which you can then add to your playbook.

  1. Use get_url to output https://getcomposer.org/installer to a file composer-setup.php, ideally in some directory, let's say /tmp

  2. Use command module to run php /tmp/composer-setup.php ... step

Or search Ansible Galaxy, and see if anyone else has done it.

  • AFAIK the command module is for custom code that never changes, very much like plain Bash with the default shell to be combined in Ansible (I actually wrote it on VSCODE offline). I should read about get_url as I'm new to Ansible, I never heard of it before, but should this be like a curl or wget altogether as well and the command would be transcribed in the future (if there'll be such need) by the Ansible community?
    – user149572
    Dec 18, 2018 at 0:50
  • Anyway, I'll check the Ansible Galaxy.
    – user149572
    Dec 18, 2018 at 0:50
  • 1
    I think the get_url calls a urllib library in Python, and might fallback to wget or curl Dec 18, 2018 at 1:52
  • Sorry man ! One last thing - I just started a very nice question here regarding this, I believe it might be interesting for you to look at anyway ! unix.stackexchange.com/questions/489630/…
    – user149572
    Dec 18, 2018 at 8:06

Here is an other way to do it, everything is explained here: https://www.devopsaurus.com/install-composer-with-ansible/


- name: Validate Composer checksum
    checksum: "sha384:795f976fe0ebd8b75f26a6dd68f78fd3453ce79f32ecb33e7fd087d39bfeb978342fb73ac986cd4f54edd0dc902601dc"
    dest: /usr/src/
    url: https://getcomposer.org/installer
  become: yes

- name: Download and install Composer
  shell: curl -sS https://getcomposer.org/installer | php
    chdir: /usr/src/
    creates: /usr/local/bin/composer
    warn: false
  become: yes

- name: Add Composer to global path
    dest: /usr/local/bin/composer
    group: root
    mode: '0755'
    owner: root
    src: /usr/src/composer.phar
    remote_src: yes
  become: yes

Read the checksum, store it in a variable, and download the PHAR file to the /usr/local/bin directory. The checksum task is optional. However, PHP is required to read the version.

- name: "Read Checksum"
  shell: curl https://getcomposer.org/download/latest-stable/composer.phar.sha256
  register: composer_checksum

- name: "Download PHAR file"
    url: https://getcomposer.org/download/latest-stable/composer.phar
    dest: /usr/local/bin/composer
    force: yes
    checksum: "sha256:{{ composer_checksum.stdout }}"

- name: "Read composer version"
  shell: composer -V
  register: composer_version

- debug:
    var: composer_version.stdout

The expected output

ok: [] => { "composer_version.stdout": "Composer version 2.1.9 2021-10-05 09:47:38" }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy