Skip to content

How to install modules (composer)

Don Richards edited this page Feb 21, 2023 · 3 revisions

Ways to run commands on the Drupal container

# As root within the container
docker-compose exec -T drupal with-contenv bash -lc "drush list"

# Or as nginx
docker-compose exec -T -u nginx drupal bash -lc "drush list"

Open a bash shell inside the Drupal contain

# As root within the container
docker-compose exec drupal bash

# Or as nginx within the container
docker-compose exec -u nginx drupal bash

Note: with-contenv is for s6 process supervisor inside of Docker container and isn't essential unless you need elevated permissions on a container with s6.

Installing modules

It's based on composer. Do not try to download and install with Drush commands. Your efforts will be undone when a composer command is run.

docker-compose exec -T drupal bash -lc "composer require drupal/console:~1.0"

# Or from within the container at the /var/www/drupal directory
composer require drupal/console:~1.0

If it’s a github repo, I found that this works best. Edit the composer.json file. Under the “repositories” section add the type as “package”

  • Name is the name you’d run composer require xxx/xxxx. For this example, it would be composer require citation-style-language/styles-distribution
  • Version is the version you’d like to a git branch. In this example it’s 1.0.0 composer require citation-style-language/styles-distribution:1.0.0
  • Package type tells composer where you intend to install this module. At the bottom of the composer.json file has the list. It has a pointer for themes, libraries, modules, etc.
  • Source type is git. Because it’s using git to fetch the repo
  • url needs a .git at the end
  • reference is the git branch it should fetch
 {
            "type": "package",
            "package": {
                "name": "citation-style-language/styles-distribution",
                "version":"1.0.0",
                "type": "drupal-module",
                "source": {
                    "type": "git",
                    "url": "https://github.com/citation-style-language/styles-distribution.git",
                    "reference": "master"
                }
            }
        },

## But to make it to where you can use composer to update it via a composer command, keep it simple (vcs).
        {
            "type": "vcs",
            "url": "[email protected]:citation-style-language/styles-distribution.git"
        },

After you add the value (make sure it isn’t already in there, then run the composer require command for that repo.

composer require citation-style-language/styles-distribution:1.0.0
# Or
composer require citation-style-language/styles-distribution:^1

# Or to specify the hash for a vcs require use this
composer require citation-style-language/styles-distribution:dev-main#123456

That will then update the compose.json file, then download the repo, then "should" enables it in Drupal. If not, then run drush en -y MODULE_NAME.

Updating can be a pain when referencing a git repo, and it's not completely supported by composer. Here's a couple of methods to get around it.

# Include the hash of the commit you want to update to.
# -- Note: for format for version is: dev- branch_name Hash_character hash_number
# -- dev- is required for it to use the hash
composer update jhu/demo_module:dev-master#2633721

# Or
# Require it as ^1@dev
# -- Note: for format for version is: version_number @dev
composer require jhu/demo_module:^1@dev
composer update --prefer-source jhu/demo_module

# -- Or using the vcs in the composer.json file yields the best results (example above)
composer update jhu/demo_module

# If composer is stuck try
composer clearcache
composer upgrade jhu/demo_module

Composer has many options for plugin for fetching repositories. Version control systems (vcs) can be really simple if all you need is to point to the repo. - composer docs on this https://getcomposer.org/doc/05-repositories.md

Don't forget

Run make config-export to export your configs to the config/sync directory. This is virtual to retain your work.