A minimal Drupal 9 local development environment
Tags: drupal
Published: 2021-02-28 09:20:19

_Originally posted on [colorfield.be](https://colorfield.be/blog/minimal-drupal-9-local-development-environment)_

We will see how to get a minimal and fast Drupal 9 setup in 3 commands.  
It just relies on a PHP built-in server, so no Docker here, the only requirements are [Composer](https://getcomposer.org/download/) and PHP.  
Then, we will do a recap of the available tools for developers.

The setup is not suitable for production, but is fine to evaluate a new version of Drupal locally, start a proof of concept, do some quick debugging on a vanilla setup, ...

## Get Drupal and install it

This command will get the last stable version (9.1.x at the time of writing).

```shell
composer create-project drupal/recommended-project my-project-directory
```

Install then the standard profile with the core install script, using SQLite.

```shell
cd my-project-directory
php web/core/scripts/drupal install standard
```

Run it with the PHP built-in server.

```shell
cd web
php -S 127.0.0.1:8888
```

And done :) your fresh Drupal site is now available: [http://127.0.0.1:8888](http://127.0.0.1:8888)

### Notes

To require a specific version, e.g. 8.9 or 9.2.x-dev

```shell
composer create-project drupal/recommended-project:^9.2.x-dev my-project-directory
```

To update the Drupal core, it is slightly different than the [Composer template for Drupal projects](https://github.com/drupal-composer/drupal-project) way to go, if you are used to it.

```shell
composer update drupal/core 'drupal/core-*' --with-all-dependencies
```

## Get common development tools

### CLI utility

Install Drush as a development dependency.

```shell
composer require --dev drush/drush
```

Check the site status and list Drush commands.

```shell
cd web
../vendor/bin/drush status
../vendor/bin/drush list
```
With Drush installed, it can be used to (re-)install your site, e.g. from the configuration, instead of the core script installation.

```shell
../vendor/bin/drush si -y standard --sites-subdir default --account-name admin --account-pass admin --existing-config
```

Or to serve it, as a replacement of php -S

```shell
../vendor/bin/drush serve
```

### Code scaffolding

For Drupal 9, you might be looking for Drupal Console, but [there is no current support for it](https://www.drupal.org/forum/general/general-discussion/2020-09-30/drupal-console-195-for-drupal-9).  
Console was taking care of several other helpers (like _site:mode dev_), but to scaffold code, you can find a replacement with the [Module Builder](https://www.drupal.org/project/module_builder) project. Require then enable it (drush en module_builder) and visit [http://127.0.0.1:8888/admin/config/development/module_builder](http://127.0.0.1:8888/admin/config/development/module_builder)

```shell
composer require --dev drupal/module_builder
```

### Developer module

Install [Devel](https://gitlab.com/drupalspoons/devel#introduction), it provides extra developer helpers like admin UI tools and Drush commands, var dumper, web profiler, content generation, ...

```shell
composer require --dev drupal/devel
```

The var dumper can be a lightweight replacement of the xdebug one. Out of the box, the Symfony var dumper is available, but I like to use Kint in this case, which is more readable and now allows to search for expressions.

```shell
composer require --dev kint-php/kint
```

Then configure Devel to use Kint, and use _kint()_, _ksm()_ or _dpm()_ in your code.

![Devel Kint variables dumper configuration](https://colorfield.be/sites/default/files/inline-images/devel-kint-variables-dumper.png)

### Examples

For more than 10 years, the [Examples for Developers](https://www.drupal.org/project/examples) module provides up-to-date and best practices code for most of the custom development tasks, and it is available for Drupal 9.

### Update code from Drupal 8

Reports and fixes deprecated code.

*   [Upgrade Rector](https://www.drupal.org/project/upgrade_rector)
*   [Upgrade Status](https://www.drupal.org/project/upgrade_status)

### Unit tests

*   [Add Drupal core-dev](https://www.drupal.org/docs/develop/using-composer/starting-a-site-using-drupal-composer-project-templates#s-adding-drupalcore-dev)
*   [Run PHPUnit tests](https://www.drupal.org/docs/automated-testing/phpunit-in-drupal/running-phpunit-tests)
*   [Cypress E2E tests](https://www.drupal.org/project/cypress)

### Code quality

Most of them are not specific to Drupal, but can have Drupal support.

*   [PHPStan for Drupal](https://github.com/mglaman/phpstan-drupal)
*   [PHP Code Sniffer](https://www.drupal.org/docs/contributed-modules/code-review-module/php-codesniffer-command-line-usage)
*   [Phan](https://github.com/phan/phan)
*   [PHPMD](https://phpmd.org/)
*   [GrumPHP](https://github.com/phpro/grumphp)

### Resources

*   [Using Composer to Install Drupal and Manage Dependencies](https://www.drupal.org/docs/develop/using-composer/using-composer-to-install-drupal-and-manage-dependencies)
*   [Homebrew PHP (Shivam Mathur)](https://github.com/shivammathur/homebrew-php) to switch local PHP version on Mac OS