Setting up a local development environment is a critical step in any Drupal project. It allows you to develop, test, and debug your site without affecting the live environment. In this guide, we’ll walk through the process of creating a local Drupal environment using DDEV, a powerful tool that leverages Docker to simplify local development. For this example, we’ll use develahaye
as the project name in commands and URLs.
DDEV is an open-source tool designed to streamline the creation and management of local development environments for Drupal, WordPress, and other CMS platforms. Here’s why DDEV is a great choice:
Before we begin, ensure you have the following installed on your machine:
Create a Project Directory:
Open your terminal and create a new directory for your project. For this example, we’ll use develahaye
as the directory name. Navigate into the directory:
mkdir develahaye cd develahaye
Initialize DDEV:
Run the following command to initialize a new DDEV project:
ddev config
DDEV will prompt you for some basic information:
develahaye
. This will be used to generate the local URL (e.g., develahaye.ddev.site
).web
.drupal9
or drupal10
depending on your version.Once configured, DDEV will generate a .ddev
directory with configuration files.
Start the DDEV Environment:
Start the local environment by running:
ddev start
This command will spin up the necessary Docker containers for your project. Once the environment is running, DDEV will provide you with URLs to access your site locally (e.g., https://develahaye.ddev.site
).
With your local environment running, the next step is to install Drupal. Instead of using the browser-based installer, we’ll use Drush, a command-line tool for Drupal that simplifies many tasks, including site installation.
Download Drupal:
Use Composer to download Drupal into your project directory. Run the following command:
ddev composer create drupal/recommended-project
This will download the latest version of Drupal and its dependencies into your project.
Set Up the Database:
DDEV automatically creates a MySQL database for your project. You can find the database credentials in the .ddev/config.yaml
file or by running:
ddev describe
The database credentials will include:
db
db
db
.ddev/config.yaml
or run ddev debug migrate-database mariadb:VERSION
and restart ddev with ddev restart
Install Drupal Using Drush:
Then run the following command to start the installation:
ddev drush site:install --site-name="develahaye" --account-name=admin --account-pass=admin
Here’s what each option does:
--site-name="develahaye"
: Sets the name of your site.--account-name=admin
: Creates an admin user with the username admin
.--account-pass=admin
: Sets the admin user’s password to admin
(change this to something more secure in a real project).Drush will automatically use the database credentials from your DDEV configuration, so you don’t need to enter them manually.
Generate a One-Time Login Link:
After the installation is complete, generate a one-time login link for the admin user using the following command:
ddev drush uli
This will output a URL that looks something like this:
http://develahaye.ddev.site/user/reset/1/1698765432/abc123def456/login
Copy and paste this URL into your browser to log in as the admin user without needing to enter a password.
With Drupal installed, you’re ready to start developing. Here are some key tasks and tips:
Run Drush Commands:
DDEV makes it easy to run Drush commands. For example, to clear the cache, run:
ddev drush cr
Manage Dependencies with Composer:
Use Composer to install and update Drupal modules and themes. For example, to install the Pathauto module, run:
ddev composer require drupal/pathauto
Enable Xdebug for Debugging:
DDEV comes with Xdebug preconfigured. Enable it by running:
ddev xdebug on
Then, configure your IDE to listen for Xdebug connections. This allows you to step through your code and debug issues effectively.
Sync Files and Database:
If you need to sync your local environment with a remote site, you can use DDEV’s import/export commands. For example, to import a database dump:
ddev import-db --file=path/to/dump.sql.gz
When you’re done working, you can stop your local environment by running:
ddev stop
If you need to remove the environment entirely, use:
ddev delete
Setting up a local Drupal development environment with DDEV is a straightforward process that can significantly improve your workflow. By leveraging DDEV’s powerful features, you can ensure consistency across environments, streamline development, and focus on building great Drupal sites.
For more advanced configurations and tips, refer to the official DDEV documentation. Happy coding!