Buy

Installing Composer Deps

Ok, let's install our Composer dependencies already! Go back to the Ansible composer module for reference. Then, find your playbook and add a new task with a poetic and flowery name: "Install Composer's dependencies":

113 lines ansible/playbook.yml
---
- hosts: vb
... lines 3 - 6
tasks:
... lines 8 - 109
- name: Install Composer's dependencies
... lines 111 - 113

Ok, boring name, but clear! Use the composer module, and set the one required option - working_dir - to {{ symfony_root_dir }}:

113 lines ansible/playbook.yml
---
- hosts: vb
... lines 3 - 6
tasks:
... lines 8 - 109
- name: Install Composer's dependencies
composer:
working_dir: "{{ symfony_root_dir }}"

Hey, that variable is coming in handy!

Run that playbook!

ansible-playbook ansible/playbook.yml -i ansible/hosts.ini

It's running... running, installing Composer's dependencies and... explosion! Ah! So much red! Run!

Then... come back. Let's see what's going on. It looks like it was downloading stuff... if we move to /var/www/project on the VM and ls vendor/, yep, it was populated.

The problem was later - when one of Symfony's post-install tasks ran:

Fatal error: Uncaught exception, SensioGeneratorBundle does not exist.

Oh yea. By default, the composer module runs composer like this:

composer install --no-dev

This means that your require-dev dependencies from composer.json are not installed:

78 lines composer.json
{
... lines 2 - 35
"require-dev": {
"sensio/generator-bundle": "^3.0",
"symfony/phpunit-bridge": "^3.0",
"doctrine/data-fixtures": "^1.1",
"hautelook/alice-bundle": "^1.3"
},
... lines 43 - 76
}

If you're deploying to production, you may want that: it gives you a slight performance boost. But in a Symfony 3 application, it makes things blow up! You can fix this by setting an environment variable... and we will do that later.

But, since this is a development machine, we probably do want the dev dependencies. To fix that, in the playbook, set no_dev to no:

114 lines ansible/playbook.yml
---
- hosts: vb
... lines 3 - 6
tasks:
... lines 8 - 109
- name: Install Composer's dependencies
composer:
working_dir: "{{ symfony_root_dir }}"
no_dev: no

Try the playbook now.

ansible-playbook ansible/playbook.yml -i ansible/hosts.ini

This time, I personally guarantee it'll work. In fact, I'm so confident, that if it doesn't work this time, I'll buy you a beer or your drink of choice if we meet in person. Yep, it's definitely going to work - I've never been so sure of anything in my entire life.

Ah! No! It blew up again! Find the culprit!

Attempted to load class "DOMDocument" from the global namespace.

Uh oh. I skipped past something I shouldn't have. When you download a new Symfony project, you can make sure your system is setup by running:

php bin/symfony_requirements

Your system is not ready to run Symfony projects.

Duh! The message - about the SimpleXML extension - means that we're missing an extension! In our playbook, find the task where we install PHP. Add another extension: php7.1-xml:

115 lines ansible/playbook.yml
---
- hosts: vb
... lines 3 - 6
tasks:
... lines 8 - 55
- name: Install PHP packages
... lines 57 - 60
with_items:
... lines 62 - 66
- php7.1-xml
... lines 68 - 115

Run that playbook - hopefully - one last time:

ansible-playbook ansible/playbook.yml -i ansible/hosts.ini

Ya know, this is the great thing about Ansible. Sure, we might have forgotten to install an extension. But instead of installing it manually and forgetting all about it next time, it now lives permanently in our playbook. We'll never forget it again.

Phew! It worked! Go back to the VM and check out requirements again:

php bin/symfony_requirements

We're good! And most importantly, we can boot up our Symfony app via the console:

php bin/console

Our app is working! And there's just one last big step to get things running: configure NGINX with PHP-FPM and point it at our project. Let's go!

Leave a comment!

  • 2017-05-18 Diego Aguiar

    Yeah, give it a try and let us know if you encounter to any problem :)

  • 2017-05-18 jian su

    Ah I see. So I just need download previous version then I should be fine

  • 2017-05-18 jian su

    Awesome. Thank you!

  • 2017-05-17 weaverryan

    Hey jian su!

    Ah, ha! This is related to Symfony! Are you trying to use the new Symfony 3.3 dependency injection features? The exclude key is pretty new (only a few days old), so you likely need to update to the absolute latest commit of Symfony on the master branch. I hope you enjoy those features - I've been personally working very hard on them (along with other people).

    Cheers!

  • 2017-05-17 weaverryan

    Yo jian su!

    Yes and no :). This depends on your task, and it's something we talk about later, in fact in the *next* chapter to be released: https://knpuniversity.com/s...

    When you use the apt module, it's basically running apt-get install php7.1. And apt is smart enough to not re-download and re-install something if it's already there (however, if you use the "latest" state, then it *will* upgrade the package if there is a new version). So for apt the answer is mostly no: the packages are not redownloaded and reinstalled.

    But for Composer, the answer (until the later chapter), is YES! This is because we're simply telling Ansible to run some commands (which download Composer), so it simply runs those commands *every* time. That's not a big deal, but it is wasteful. So, we fix it later.

    Cheers!

  • 2017-05-17 jian su

    Hi Guys:

    After running this task and have problem

    - name: Install Composer's dependencies
    composer:
    working_dir: "{{ symfony_root_dir }}"
    no_dev: no

    Need help, I have the following error

    [Symfony\Component\Config\Exception\FileLoaderLoadException]
    The configuration key "exclude" is unsupported for definition "AppBundle\" in "/var/www/project/app/config/services.yml". Allowed configuration key
    s are "resource", "parent", "shared", "lazy", "public", "abstract", "deprecated", "factory", "arguments", "properties", "configurator", "calls", "t
    ags", "autowire", "autoconfigure" in /var/www/project/app/config/services.yml (which is being imported from "/var/www/project/app/config/config.yml
    ").

  • 2017-05-17 jian su

    If I made mistakes like you, and I rerun the playbook again. Does that mean I re-download php7.1 mysql and composer......over again??