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:
- Open your command line or terminal.
- Download the installer using PHP:
- Validate the installer:
- Run the installer:
- Remove the installer:
- Move Composer into your PATH:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
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:
- Create a project directory, navigate to it in your terminal, and run:
- 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. - Navigate into your new project directory:
composer create-project drupal/recommended-project my_site_name_dir
cd my_site_name_dir
Database Configuration
With your project created, configure your database:
- Create a new database for your Drupal site using your preferred database management tool.
- Open the
sites/default
directory, and renamedefault.settings.php
tosettings.php
. - Edit
settings.php
to add your database connection details. - Set file permissions to ensure
settings.php
is writeable during setup:
chmod 664 sites/default/settings.php
Running Your Drupal Site
Finally, it’s time to run your Drupal site:
- In the command line, start the built-in PHP server to run your site locally:
- Open your browser and navigate to http://localhost:8000. Follow the on-screen instructions to complete the installation.
php -S localhost:8000 -t web/
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!