【Laravel】composer require symfony/dom-crawlerのエラー対処法:PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 83886080 bytes)

laravel-prograshi(プロぐらし) Laravel
記事内に広告が含まれていることがあります。

Laravelでスクレイピングを行うために、Symfonyのdomcrawlerを導入しようとした時に次のようなエラーが発生した時の対処法について。

Updating dependencies (including require-dev)
PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted (tried to allocate 83886080 bytes) in phar:///usr/bin/composer/src/Composer/DependencyResolver/RuleSet.php on line 90

Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 83886080 bytes) in phar:///usr/bin/composer/src/Composer/DependencyResolver/RuleSet.php on line 90

Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.

症状

composer require symfony/dom-crawlerを実行した時に、インストールに成功したがUpdating dependenciesでエラーが発生。

sympfony dom-crawler自体は導入できたが、依存関係のパッケージのインストールに失敗している。

エラー内容はメモリ容量不足

なお、依存関係のインストールができないとuse Symfony\Component\DomCrawler\Crawler;new Crawlerした時にClass “Symfony\Component\DomCrawler\Crawler” not found のエラーが発生する。

このため、解決は必須。


対応策

一時的にメモリ上限を無制限にしてインストールします。

composer requireコマンドの前に環境変数COMPOSER_MEMORY_LIMITを指定します。

# COMPOSER_MEMORY_LIMIT=-1 composer require symfony/dom-crawler


実例

# COMPOSER_MEMORY_LIMIT=-1 composer require symfony/dom-crawler
Cannot load Zend OPcache - it was already loaded
Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
    1/4:	http://repo.packagist.org/p/provider-latest$2c076ffb933a1bc12d7375c60f47b8c194fc123ed445cb970efffa8b05bae7b2.json
    2/4:	http://repo.packagist.org/p/provider-2021-07$5949629f07c7b69ca73c75b80a6078a47af1ba725c58b3f9b74acb254a234cdd.json
    3/4:	http://repo.packagist.org/p/provider-2021-01$4da0ffe5bffe29dea75e647c2d3ad3b8370879936b1b785dec1a7a8a2e0d9f82.json
    4/4:	http://repo.packagist.org/p/provider-2021-04$41142924c49cfa661f9097b28d13a3224a3fd21a1bc76b31f13e81317343d4f3.json
    Finished: success: 4, skipped: 0, failure: 0, total: 4
Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
Using version ^5.3 for symfony/dom-crawler
./composer.json has been updated
Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
Loading composer repositories with package information
Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
Updating dependencies (including require-dev)
    2/15:	https://codeload.github.com/symfony/cache-contracts/legacy.zip/c0446463729b89dd4fa62e9aeecc80287323615d
    2/15:	https://codeload.github.com/symfony/filesystem/legacy.zip/343f4fe324383ca46792cae728a3b6e2f708fb32
    3/15:	https://codeload.github.com/symfony/dom-crawler/legacy.zip/c7eef3a60ccfdd8eafe07f81652e769ac9c7146c
    4/15:	https://codeload.github.com/symfony/asset/legacy.zip/9bd222a8fdd13ecca91384e403247103198f80a1
    5/15:	https://codeload.github.com/symfony/cache/legacy.zip/864867b13bd67347497ce956f4b253f8fe18b80c
    6/15:	https://codeload.github.com/symfony/framework-bundle/legacy.zip/5d4fcef02a42ea86280afcbacedf8de7a039032c
    7/15:	https://codeload.github.com/symfony/dependency-injection/legacy.zip/a665946279f566d94ed5eb98999cfa65c6fa5a78
    8/15:	https://codeload.github.com/1up-lab/OneupUploaderBundle/legacy.zip/9a207fa8b2f273fec8086341eb9b4ca51d672f8c
    10/15:	https://codeload.github.com/symfony/templating/legacy.zip/768d2fc5a9c9c93aad8c10af834a89f9d9d31011
    10/15:	https://codeload.github.com/php-fig/cache/legacy.zip/213f9dbc5b9bfbc4f8db86d2838dc968752ce13b
    11/15:	https://codeload.github.com/symfony/polyfill-php81/legacy.zip/e66119f3de95efc359483f810c4c3e6436279436
    12/15:	https://codeload.github.com/symfony/yaml/legacy.zip/4500fe63dc9c6ffc32d3b1cb0448c329f9c814b7
    13/15:	https://codeload.github.com/twigphp/Twig/legacy.zip/21578f00e83d4a82ecfa3d50752b609f13de6790
    14/15:	https://codeload.github.com/symfony/var-exporter/legacy.zip/2ded877ab0574d8b646f4eb3f716f8ed7ee7f392
    15/15:	https://codeload.github.com/symfony/config/legacy.zip/4268f3059c904c61636275182707f81645517a37
    Finished: success: 15, skipped: 0, failure: 0, total: 15
Package operations: 15 installs, 0 updates, 0 removals
  - Installing twig/twig (v3.3.2): Loading from cache
  - Installing symfony/yaml (v5.3.6): Loading from cache
  - Installing symfony/polyfill-php81 (v1.23.0): Loading from cache
  - Installing psr/cache (2.0.0): Loading from cache
  - Installing symfony/templating (v5.3.7): Loading from cache
  - Installing symfony/filesystem (v5.3.4): Loading from cache
  - Installing symfony/dependency-injection (v5.3.7): Loading from cache
  - Installing symfony/config (v5.3.4): Loading from cache
  - Installing symfony/var-exporter (v5.3.7): Loading from cache
  - Installing symfony/cache-contracts (v2.4.0): Loading from cache
  - Installing symfony/cache (v5.3.7): Loading from cache
  - Installing symfony/framework-bundle (v5.3.7): Loading from cache
  - Installing symfony/asset (v5.3.4): Loading from cache
  - Installing oneup/uploader-bundle (3.1.3): Loading from cache
  - Installing symfony/dom-crawler (v5.3.7): Loading from cache
symfony/dependency-injection suggests installing symfony/expression-language (For using expressions in service container configuration)
symfony/dependency-injection suggests installing symfony/proxy-manager-bridge (Generate service proxies to lazy load them)
symfony/framework-bundle suggests installing ext-apcu (For best performance of the system caches)
symfony/framework-bundle suggests installing symfony/form (For using forms)
symfony/framework-bundle suggests installing symfony/serializer (For using the serializer service)
symfony/framework-bundle suggests installing symfony/validator (For using validation)
symfony/framework-bundle suggests installing symfony/property-info (For using the property_info service)
symfony/framework-bundle suggests installing symfony/web-link (For using web links, features such as preloading, prefetching or prerendering)
oneup/uploader-bundle suggests installing knplabs/knp-gaufrette-bundle (0.1.*)
oneup/uploader-bundle suggests installing oneup/flysystem-bundle (^3.0)
oneup/uploader-bundle suggests installing twistor/flysystem-stream-wrapper (^1.0 (Required when using Flysystem))
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Cannot load Zend OPcache - it was already loaded
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/tinker
Discovered Package: laravelium/sitemap
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: olssonm/l5-very-basic-auth
Package manifest generated successfully.
87 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

エラーが出ることなく無事に依存関係のパッケージをダウンロードすることができました。


補足

上記を試す以外に、dockerコンテナ内のphp.iniの環境変数を追記する方法を次の2つを試しましたが、エラーが発生してうまくいきませんでした。

  1. Dockerファイルで変更
  2. docker-composeで変更

タイトルとURLをコピーしました