Drupal 9 Composer

Posted : admin On 1/26/2022

Upgrading to Drupal 8.4.0 and Drush 9 with Composer - a blog post by Benji Fisher (@benjifisher) at Isovera covering the problematic 8.3 to 8.4 upgrade. Update to Drupal core 8.4, a step by step guide - a blog post by PreviousNext covering the problematic 8.3 to 8.4 upgrade.

Goal: Use Composer and/or Git to download files and dependencies in the core software, or in add-on modules and themes.See the written version of this tutori. Download Drupal core using Composer. As of the release of Drupal 8.8.0 - the recommend composer template changed from drupal-composer/drupal-project to the officially supported: drupal/recommended-project. For Drupal 8 and Drupal 9, use the composer template at drupal/recommended-project. This template ensures Drupal Core dependencies are the exact same version as the official drupal release. Use Drupal 8 on Lando for local development; powered by Docker and Docker Compose, config php version, swap db backends or web server, use composer, drush, drupal console, xdebug and custom config files, oh and also import and export databases.

I've recently come across this problem a few times while talking to friends and collegues. It seems people are still using Homebrew or other strange methods to try and get Drush installed locally. This is especially frustrating for people looking to get started with Drupal 8 or 9 development as it requires the latest version of Drush. Using Composer is far and away the easiest method to install Drush.

The best way to use Drush 9 is to include it in your project.

First install Composer if you don't have it already. Follow the latest instructions from the official source, getcomposer.org and then return here.

Now that you have Composer, you can include drush in your project.

Okay now what? How does one use this drush? There are two ways....

Call it directly via the vendor directory

Drupal recommended project

Or (even better) install the Drush Launcher so that it will auto-discover whatever drush is in your working project folder.

Drupal

Follow instructions here: Install Drush Launcher and then you can call drush whenever you have it inside the project.

Still, I like the feel of having a 'global' drush, so the secret there is to set a system variable in your .bashrc or (my favorite) .zshrc

Once that is set you can reload your configuration and you should once again be able to call drush from any directory. In the example, I'm using the Drush provided by Dev Desktop as my fallback drush.

This is kind of nice, because I have have Drush 9 on the project level, but also use older Drush 8 aliases with my 'global' drush without actually installing it globally.

One should also be able to install Drush globally on their machine.

composer global require drush/drush:dev-master

...but YMMV there.

Drupal 9 Composer Installation

Again, I recommend the drush launcher.

Some other things you can do with drush and composer...

Keep Drush (and anything else you have installed with Composer) up to date.

Roll back to a release version of Drush 8 if you need to. There are some changes from Drush 8 to Drush 9 and you may find gaps depending on the modules you're using.

That's it!

No really, we're done now.

You can now go back to doing whatever it is you were doing before you realized you needed to install Drush. :)

Composer

  1. Install unzip
    • sudo yum install unzip
  2. Install git
    • sudo yum install git
  3. Install composer
    • sudo yum install composer
  4. Increase memory size on VM to 4GB or add Swap space
    • sudo fallocate -l 2G /swapfile
      sudo chmod 600 /swapfile
      sudo mkswap /swapfile
      sudo swapon /swapfile
  5. Create a directory under htdocs or /var/html to hold the install
    • Note a composer project will be setup in the directory you create, and drupal will be setup in a sub directory call web, the web directory is where your virtual machine should point.
  6. Create a database and user in MySQL
  7. Install Drupal and Drush with composer, if needed change the /var/www/drupal8 to the directory you created in step #5, this is a dev version of the composer config, but will install the latest stable version of Drupal
    • composer create-project drupal-composer/drupal-project:8.x-dev /opt/www/drupal8 --stability dev --no-interaction
  8. Install Drush Globally (so you can run it from any project)

    First, we’ll install Drush with Composer.

    alias drush='/home/username/.config/composer/vendor/drush/drush/drush'

  9. How to Maintain a Drupal 8 Site Built with Composer

Preparing to Update Drupal Core

You’ll need to follow the steps below to update your core files if you installed Drupal using the Composer build.

  • Run composer update drupal/core --with-dependencies to update Drupal Core and its dependencies.
  • Run git diff to determine if any of the scaffolding files have changed. Review the files for any changes and restore any customizations to .htaccess or robots.txt.
  • Commit everything all together in a single commit, so web will remain in sync with the core when checking out branches or running git bisect.
  • In the event that there are non-trivial conflicts in step 2, you may wish to perform these steps on a branch, and use git merge to combine the updated core files with your customized files. This facilitates the use of a three-way merge tool such as kdiff3. This setup is not necessary if your changes are simple—keeping all of your modifications at the beginning or end of the file is a good strategy to keep merges easy.
  1. Copy drupal8/web/sites/default/default.settings.php settings.php
  2. Enable writting to settings.php disable SElinux on Centos7
    • # setsebool allow_httpd_anon_write=1
      # chcon -t public_content_rw_t sites/default sites/default/settings.php sites/default/files
  3. Make directory drupal8/web/sites/default/files
  4. Change permissions on settings.php and files to be writable by apache
  5. Change permissions on the directory to allow apache to create files in your directory
    • sudo chgrp apache drupal8
  6. Create a database and user for drupal
  7. Add apache virutal site config for recipes site to /etc/httpd/conf.d
    • sudo vim /etc/httpd/conf.d/recipes.conf
    • Add the following:
    • <VirtualHost *:80>
      ServerName recipes.web3us.com
      ServerAlias recipes.web3us.com
      ServerAdmin [email protected]
      DocumentRoot '/opt/www/recipes/web/'

      <Directory '/opt/www/recipes/web/'>
      Options +Indexes +FollowSymLinks
      AllowOverride All
      </Directory>

      ErrorLog /var/log/httpd/recipes-error_log
      CustomLog /var/log/httpd/recipes-access_log combined
      # Possible values include: debug, info, notice, warn, error, crit
      LogLevel warn
      </VirtualHost>

      <VirtualHost *:443>
      SSLEngine on
      SSLCertificateFile /etc/letsencrypt/live/domain.com/cert.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem

      ServerName recipes.web3us.com
      ServerAlias recipes.web3us.com
      ServerAdmin [email protected]
      DocumentRoot '/opt/www/recipes/web/'

      <Directory '/opt/www/recipes/web/'>
      Options +Indexes +FollowSymLinks
      AllowOverride All
      </Directory>

      ErrorLog /var/log/httpd/recipes-error_log
      CustomLog /var/log/httpd/recipes-access_log combined
      # Possible values include: debug, info, notice, warn, error, crit
      LogLevel warn
      </VirtualHost>

      # ServerName: The domain that should match for this virtual host configuration. This should be your domain name.
      # ServerAlias: All other domains that should matchfor this virtual host as well, such as the www subdomain.
      # DocumentRoot: The directory from which Apache will serve the domain files.
      # Options: This directive controls which server features are available in a specific directory.
      # -Indexes: Prevents directory listings.

  8. Restart apache
    • sudo systemctl restart httpd
  9. localhost/drupal8
  10. Check for errors
    • sudo tail /var/log/httpd/error_log
    • if running SSL check sudo tail /var/log/httpd/ssl_error_log
  11. Select language
    • English
  12. Choose profile
    • Demo: Umami Food Magazine (Experimental)
  13. Enter database, user, and password
  14. Install
  15. Configure site
    • Set site name
    • Create admin user
    • Select country and timezone
    • Save
  16. Login at: http://localhost/drupal8/web/user/login
  17. To add modules to drupal change to the project directory, the one above the /web
    • composer require drupal/<modual_name>
    • drush en <module_name>

Drupal 9 Composer.json

To Update Drupal Core

Drupal 9 Composer Patch

  1. Check to see if outdated
  2. Update Drupaldirectory/composer.json if needed (moving from drupal 8.6 to 8.7) Note: If you are upgrading major version i.e. from 8.5 to 8.6, you need to edit composer.json and replace '~8.5.x' with '^8.6.0' for drupal/core and webflo/drupal-core-require-dev. I have also been told you can replace 8.6.0 with 8.6, but I have not tested that yet.
    • replace '~8.5.x' with '^8.6.0' for drupal/core
  3. Update drupal/core with dependencies
  4. Update drupal and webflo
  5. To allow upgrade to Drupal 8.7.6 had to use this command as webflo is still behind
  6. Update the database
  7. Cache Reset

Troubleshooting

To display the dependency tree, browsing that might help you determine what requires the packages that need to be updated and inform a decision. You also might find github.com/webflo/drupal-core-strict useful, it fixes core dependencies to known good versions which might help you update with more confidence.

  • composer show --tree

To figure out which dependency was blocking my Drupal core upgrade using the Composer command:

  • composer prohibits drupal/core:8.5.0

Since symfony/config (a dependency of drush and drupal console), I needed to run

Drupal 8 Composer

I still have no idea how I could've figured out I just needed to add symfony/config in the update command (vs adding all the other symfony components)... but if I do, I'll try to update this answer with that information so others can benefit in the future!