【Drupal9】Docker × DDEVでDrupalローカル開発環境構築

# 0. はじめに
『Drupal 9 Web開発ことはじめ』記載の環境構築がうまくいかなかったのでメモ。

https://nextpublishing.jp/book/12999.html

## 0-1. 全体の流れ
> [1.DDEV](#1-ddev)
> [2.Drupal](#2-drupal)
> [3.git](#3-git)
> [4.appendix](#4-appendix)
> [Reference](#reference)

## 0-2. 前提条件
- `Docker`インストール済み
- `Homebrew`インストール済み

https://www.docker.com/products/docker-desktop/

https://brew.sh/index_ja

## 0-3.開発環境
|key|value|
|:--:|:--:|
|Drupal|9|
|PHP|8.1|
|Docker|20.10.21|
|Homebrew|3.4.10|
|OS|macOS Ventura 13.1|

# 1. DDEV
https://ddev.readthedocs.io/en/latest/users/install/ddev-installation/#macos

## 1-1. installation
```terminal:local
$ brew install drud/ddev/ddev
$ brew upgrade ddev
```

## 1-2. error
https://qiita.com/Saw-000/items/d1d9af1bf55c32ca5fe8

```terminal:local
$ brew install drug/ddev/ddev
Error: An exception occurred within a child process:
  CompilerSelectionError: drud/ddev/ddev cannot be built with any available compilers.
Install GNU's GCC:
  brew install gcc
```

- `gcc`をインストールしろと言われる。

```terminal:local
$ brew install gcc
Error: gcc: the bottle needs the Apple Command Line Tools to be installed.
  You can install them, if desired, with:
    xcode-select --install

If you're feeling brave, you can try to install from source with:
  brew install --build-from-source gcc
```

- `xcode-select`が必要らしい。

```terminal:local
$ xcode-select --install
xcode-select: note: install requested for command line developer tools
$ brew install drud/ddev/ddev
$ ddev
Create and maintain a local web development environment.
Docs: https://ddev.readthedocs.io
Support: https://ddev.readthedocs.io/en/stable/users/support

Usage:
  ddev [command]

Available Commands:
=== 略 ===
```


# 2. Drupal
## 2-1. installation
```terminal:local
$ mkdir my-drupal9-site
$ cd my-drupal9-site
$ ddev config --project-type=drupal10 --docroot=web --create-docroot
$ ddev start
$ ddev composer create drupal/recommended-project
$ ddev composer require drush/drush
$ ddev drush site:install --account-name=admin --account-pass=admin -y
$ ddev drush uli
$ ddev launch
```

![53e100a6-1fc8-46b1-a924-ff3cdd75b750.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/f49348aa-736b-d225-9a67-fe42d616cf5e.png)

![17db5572-eda0-42ec-a268-be077883208e.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/1b26d1a6-0ddf-a19b-3020-11252a27278f.png)

## 2-2. lang
![1888e0de-3bb7-4c04-b928-07e19a611a6e.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/603100c8-cd2c-608f-550d-89fe72959223.png)

![cfd5438c-58e8-43bf-8e89-aef15c8dfa9c.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/b643ac73-f093-ecf6-afed-1c3d0c9c761a.png)

![db28f571-4338-434d-bff3-03784029c3dd.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/b883e39e-f837-73fd-38f4-74c3df60e9d6.png)

![491b9c5e-7615-4956-bd3c-b37e4e6d524c.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/32d336d5-3638-21de-b0ba-13652d692141.png)

![de1c308d-329f-4634-a03f-d0a384dabcd1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/0d70d32d-7407-b956-5e96-b6ecc500853c.png)

![378f6e2f-b97a-49a0-a644-3c87953f79f8.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/59afd276-997f-c735-6dd3-3be9779eaeb1.png)

![4e252e7b-2925-48c7-ac57-089f2d10da86.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/2c172129-4f87-7448-70b2-922e92bc32e9.png)

![a8e34eb3-6c33-4cdc-b883-481543be8f08.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/3148604a-5653-c154-9de3-e7d3ded58a0f.png)


## 2-3. locale
![95557428-d6b3-4eb1-929e-b5ee774dce36.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/d28237bf-566e-df60-780c-8d62c32d119f.png)

![7e5033ea-159c-49be-951a-cc343804734d.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/9e47c428-990e-af8b-f53e-b52c8a07469a.png)


# 3. git
## 3-1. .gitignore
- サンプルは`drupal8`ですが、9系のサンプルを見つけたら更新します。
```terminal:local
$ cd my-drupal9-site
$ touch .gitignore
```
```text:.gitignore
# Ignore directories generated by Composer
/drush/contrib/
/vendor/
/web/core/
/web/modules/contrib/
/web/themes/contrib/
/web/profiles/contrib/
/web/libraries/

# Ignore all site specific data
/web/sites/*
!/web/sites/default/
!/web/sites/example.sites.php

# Ignore IDE files
/.idea/
/.vscode/
*.iml
*.code-workspace

# Ignore all log files
*.log

# Ignore node modules
node_modules

# Ignore some css related stuff
*.css.map
*.css.map.map

# Ignore default text files
/CHANGELOG.txt
/COPYRIGHT.txt
/INSTALL*.txt
/LICENSE.txt
/MAINTAINERS.txt
/UPGRADE.txt
/README.txt
sites/README.txt
sites/all/modules/README.txt
sites/all/themes/README.txt
```

https://stefvanlooveren.me/blog/default-gitignore-file-drupal-8

https://github.com/github/gitignore/blob/main/Drupal.gitignore

# 4. appendix
- Drupal9は、PHP8.1以上がインストールされていないとエラーが出る。

https://github.com/drud/ddev/issues/3953

```terminal:local
$ ddev composer require drush/drush
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - drupal/core 10.0.0 requires php >=8.1.0 -> your php version (8.0.26) does not satisfy that requirement.
    - drupal/core-recommended 10.0.0 requires drupal/core 10.0.0 -> satisfiable by drupal/core[10.0.0].
    - drupal/core-recommended is locked to version 10.0.0 and an update of this package was not requested.

You can also try re-running composer require with an explicit version constraint, e.g. "composer require drush/drush:*" to figure out if any version is installable, or "composer require drush/drush:^2.1" if you know which you need.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.
composer [require drush/drush] failed, composer command failed: exit status 2. stderr=
```

```diff:.ddev/config.yml
- php_version: "8.0"
+ php_version: "8.1"
```

# Reference
https://nextpublishing.jp/book/12999.html

https://www.drupal.org/documentation

https://ddev.readthedocs.io/en/stable/

https://ddev.readthedocs.io/en/latest/users/install/ddev-installation/#macos

https://ddev.readthedocs.io/en/stable/users/quickstart/#drupal

https://unlimitedchaos.net/how-to-localize-japanese-drupal8/

https://qiita.com/Saw-000/items/d1d9af1bf55c32ca5fe8

https://github.com/github/gitignore/blob/main/Drupal.gitignore

https://github.com/drud/ddev/issues/3953

https://qiita.com/hanlio/items/6bcb3667147f2d5633de