Understanding Composer for Drupal

understanding Composer

Composer is an application/tool that manages dependencies in PHP.  In this article, Composer is used to manage dependencies that are needed by a Drupal installation. We will show you what Composer needs to run,  how Composer can be used to install a Drupal site, and how Composer can load a module/theme and any corresponding dependencies.

Warning:  Composer is not a Drupal program or extension.  It is a PHP tool used to manage dependencies.  Drupal themes and extensions can use many libraries that require separate management in order to keep versions up-to-date and operating properly.  Using Composer is an advanced tool that is not recommended for users who are not familiar with using terminal or dependency libraries. 


Need a new home for your new Drupal site?  Check out InMotion’s Managed Drupal Hosting on VPS servers for secure, performance-oriented hosting solutions.

What is Composer?

Composer is not a package manager like YUM or APT utilities for Linux OS/Linux distributions.  Instead, it provides a way for you to list the libraries that your installation requires and can manage them for you.  Composer allows you to list what’s needed and then install or update these dependencies for your site so that it can function properly and securely. 

What Does Composer require to run?

In order to run Composer, you will need PHP 5.3.2+.  Composer can be installed on Windows, Mac OS, and Linux.  Composer can be installed locally (in the project directory), or it can be installed for use on the server (globally).  We recommend installing it locally for security purposes unless you intend to use it across multiple projects. 

To learn more about installing Composer, please see Installing Composer in Windows or Linux.

NOTE: If you are an InMotion Hosting subscriber, then you will already have the required version of PHP on your hosting subscription server.

If you want to see all of the available command options for Composer, go to the command line and then type in composer and hit return.  You will see a list of the options:

list of all the composer commands using a terminal

Installing Drupal with Composer

Composer can be used to install a Drupal site and all its dependencies.  Composer is also commonly used to install modules and themes for a Drupal site.  Follow the steps below to see the typical commands for different versions of Drupal installation using composer.

  1. Log in to cPanel.
  2. Log into your account using your favorite SSH terminal application.  If you didn’t know, cPanel also provides terminal access.  To learn more about accessing it, please see: How to Launch the cPanel Terminal.
  3. Once you’re in the terminal, use the following command line to install a Drupal site with all of its dependencies:
[Command line for Drupal 9]
composer create-project drupal/recommended-project website_folder_name
[Command line for Drupal 8.80 and above] – Installing version 8.9.20

composer create-project drupal/recommended-project:8.9.20 website_folder_name
[Command line for Drupal 8]
composer create-project drupal/recommended-project:8.x-dev -n website_folder_name
[Command line for Drupal 7]
composer create-project drupal/recommended-project:7.x-dev -n website_folder_name


Once the command runs, a Drupal site will be created in the “website_folder_name”. The composer install command is automatically executed to download the latest stable version of Drupal and all of its dependencies.

It is important to understand how the files and folders are being installed for your Drupal installation.

  • The “website_folder_name” will contain files that need to be outside of the web root and not be able to be accessed via the web server.  
  • The default web root path using this install method would be “website_folder_name/web”.

The composer command used above downloads the current official release of Drupal.  If you intend to use a specific version, then you should add the version number to the command after a colon, like this:

composer create-project drupal/recommended-project:9.2.1 website_folder_name

How to Download Modules, Themes, and their Dependencies

As more Drupal modules are contributed it has been very common to have dependencies coming from third-party libraries.  If you used Composer to install Drupal, then you will typically want to use it to download all the modules and themes used on your site.

In order to add a module or theme to the list managed by Composer, you will need to use the required command for Composer.

The format for the composer command to add a module to the list of dependencies is:\

composer require drupal/<modulename>

An example of the command would be:

composer require drupal/token

In order to download a specific version, then you would add a colon at the end of the command followed by the version number.

composer require drupal/token:<version>

This command should not be executed at the same level as the core directory.  For example, if you’re in a terminal, you would switch to the actual core directory and then run the command.  

When the command is executed then the module and all of its dependencies will be downloaded. 

Once the module is loaded, then you will need to enable the module using one of two different ways:

You can also specify a version of the module.  The recommendation is to use quotes around the requirement that you state.  Here’s an example:


composer require ‘drupal/token:^1.5’

This concludes our tutorial on understanding Composer for Drupal. In terms of all of the commands, we have only provided an introduction to the command. Remember that using Composer will require you to be familiar with the Drupal code and its files. If you are unsure about using Composer, then make sure to speak with an experienced programmer/developer to learn more about using Drupal. To learn more about using Composer make sure to check out their website:  getcomposer.org.  


Make sure your business, agency, or reseller clients are always connected and powered on with our optimized Managed VPS Hosting.
AC
Arnel Custodio Content Writer I

More Articles by Arnel

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+