Setting up Drupal 10 with Composerfor Drupal 8 , 9 , 10 , and 11

Last updated :  

Introduction

In the previous lesson, you explored the crucial aspects of managing configuration versus content storage in Drupal. Now, it’s time to dive into the practical setup of a Drupal 10 environment using Composer, a powerful PHP dependency manager. Setting up Drupal with Composer offers advantages such as streamlined dependency management, easier version control, and robust project management, ensuring your site is future-proof and maintainable.

What is Composer?

Composer is a tool for managing dependencies in PHP projects. It allows you to declare the libraries your project depends on and installs them in your project for you. By using Composer with Drupal, you can easily install, update, and manage your Drupal modules and themes.

Prerequisites

Before we begin, ensure you have the following prerequisites:

  • A web server with Apache or Nginx.
  • PHP version 8.1 or higher.
  • MySQL version 5.7.8+ or MariaDB version 10.3.7+.
  • Access to the command line or terminal on your computer.
  • Composer installed on your system. You can install Composer by following the official Composer download instructions.

Installing Composer

If you haven’t installed Composer yet, follow these steps:

  1. Open your command line or terminal.
  2. Download the installer using PHP:
  3. php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  4. Validate the installer:
  5. php -r "if (hash_file('sha384', 'composer-setup.php') === '') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  6. Run the installer:
  7. php composer-setup.php
  8. Remove the installer:
  9. php -r "unlink('composer-setup.php');"
  10. Move Composer into your PATH:
  11. mv composer.phar /usr/local/bin/composer

Setting Up Your Drupal Project with Composer

With Composer ready, you can now set up your Drupal 10 site:

  1. Create a project directory, navigate to it in your terminal, and run:
  2. composer create-project drupal/recommended-project my_site_name_dir
  3. This command creates a new directory named my_site_name_dir containing a fully-functional Drupal 10 installation. Composer automatically handles dependency management, downloading the necessary files.
  4. Navigate into your new project directory:
  5. cd my_site_name_dir

Database Configuration

With your project created, configure your database:

  1. Create a new database for your Drupal site using your preferred database management tool.
  2. Open the sites/default directory, and rename default.settings.php to settings.php.
  3. Edit settings.php to add your database connection details.
  4. Set file permissions to ensure settings.php is writeable during setup:
  5. chmod 664 sites/default/settings.php

Running Your Drupal Site

Finally, it’s time to run your Drupal site:

  1. In the command line, start the built-in PHP server to run your site locally:
  2. php -S localhost:8000 -t web/
  3. Open your browser and navigate to http://localhost:8000. Follow the on-screen instructions to complete the installation.

Conclusion

You've now set up a Drupal 10 site using Composer! This method lays the groundwork for a maintainable and scalable project, simplifying dependency and version management. You can now explore the powerful features of Drupal 10 with ease.

Next Lesson Preview: Downloading and Configuring Drupal via Tarball

In our next lesson, we'll explore an alternative method to get Drupal up and running: using a tarball. This approach provides more manual control over the Drupal installation, perfect for those who love diving deeper into code management. Stay tuned!