How to Install Drupal with Composer? Practical Guide
Nowadays, the recommended way to install Drupal is via Composer. It helps install Drupal core and the contributed modules or themes. Check out our short but profound step-by-step Drupal Composer installation guide.
Advantages of Installing Drupal with Composer
Advantages of Installing Drupal with Composer
If you want to install a new project, you must do it with Composer, not Drush. You will use the latter to install modules or themes, not download them.
The main advantages of using Composer are the ability to manage requirements systematically and transparently and the certainty that you have installed the correct and up-to-date version of a module or theme. The official project, which contains information and templates, shows how to install Drupal using Composer.
Installing Composer
Installing Composer
Firstly, you have to install Composer. Read the guide to learn more.
Downloading Drupal Core
Downloading Drupal Core
To create a new project based on the official template, you need to run the following command:
composer create-project drupal/recommended-project project_folder
It will download the drupal/recommended-project package to the project_folder. It will also automatically run the composer install command, which downloads the Drupal 10 core and all required components. Remember to rename project_folder.
What Does the Project Template Do?
What Does the Project Template Do?
If you install a project based on the composer.json file provided with the project template, you will see changes to the standard way of installing Drupal:
- Drupal will be installed in the web folder.
- Autoloader generated by Composer will be in vendor/autoload.php, while Drupal autoloader in web/vendor/autoload.php.
- Modules (drupal-module packages) will be placed in web/modules/contrib.
- Themes (drupal-themes packages) will be placed in web/themes/contrib.
- Profiles (drupal-profile packages) will be placed in web/profiles/contrib.
- Default, writable settings.php and services.yml versions will be created.
- A web/sites/default/files folder will be created.
- The latest Drush version will be installed locally, available at vendor/bin/drupal.
- Environment variables based on the .emv file will be created.
The project also includes a basic .gitignore file that excludes all folders containing packages installed using Composer. Therefore, once you have an up-to-date composer.json file, you can easily rebuild your Drupal installation.
How to Install a Drupal Site in a Different Folder?
How to Install a Drupal Site in a Different Folder?
Let’s assume you need to install your Drupal site in the docroot/ or public_html/ folder. To do this, you have to manually clone the project template to the folder:
git clone https://github.com/drupal/recommended-project.git project_folder
Then, go to the project folder and edit the composer.json file. In the extras/installer-paths section, change the paths from web/ to the directory you are interested in. Then, run the composer install command.
Now that you have downloaded the project, you can install it using the drush site:install command or go to the local site http://localhost/.
Downloading Drupal Module and Theme Packages
Downloading Drupal Module and Theme Packages
Downloading packages from drupal.org comes down to one simple command: composer require. For example, to install the Pathauto module, you must visit the extension’s subpage.
In the first line, there is a simple command you have to execute in the project. The principle of building the command comes down to remembering the syntax, which is composer require: repository_name/package_mechanical_name - repository_name in the case of drupal.org is drupal, and package_mechanical_name for Pathauto is simply pathauto.
After downloading the module, you have to install it. You can do it using the drush pm:install pathauto command.
Updating Dependencies
Updating Dependencies
You can update dependencies by running the composer update command, which you can attach to your git repository after testing. To mirror the environment on the remote version, you need to run composer install on the server. Then, all packages and the Drupal core will be downloaded to your project directory. This is because the current versions are saved in a special composer.lock file during the update.
Summary
Summary
Composer significantly speeds up the work related to Drupal and improves security. Updating Drupal and its dependencies comes down to one simple command.In the repository, you can easily see which module packages are utilized in the project. It is not difficult to install Drupa via Composer. However, if you have doubts or problems, do not hesitate to contact experts.