欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

PHP Composer update “cannot allocate memory” error (using Laravel 4) 有大用

I just can't solve this one.

I'm on Linode 1G RAM basic plan. Trying to install a package via Composer and it's not letting me. My memory limit is set to "-1" on PHP.ini

Is there anything else I can do to get this installed?

Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing thujohn/rss (dev-master df80a7d)
    Downloading: 100%         
PHP Fatal error:  Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory' in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php:975
Stack trace:
#0 [internal function]: Composer\Util\ErrorHandler::handle(2, 'proc_open(): fo...', 'phar:///usr/loc...', 975, Array)
#1 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(975): proc_open('stty -a | grep ...', Array, NULL, NULL, NULL, Array)
#2 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(853): Symfony\Component\Console\Application->getSttyColumns()
#3 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(818): Symfony\Component\Console\Application->getTerminalDimensions()
#4 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(752): Symfony\Component\Console\Application->getTerminalWidth()
#5 phar:///usr/local/bin/com in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php on line 975

Fatal error: Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory' in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php:975
Stack trace:
#0 [internal function]: Composer\Util\ErrorHandler::handle(2, 'proc_open(): fo...', 'phar:///usr/loc...', 975, Array)
#1 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(975): proc_open('stty -a | grep ...', Array, NULL, NULL, NULL, Array)
#2 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(853): Symfony\Component\Console\Application->getSttyColumns()
#3 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(818): Symfony\Component\Console\Application->getTerminalDimensions()
#4 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(752): Symfony\Component\Console\Application->getTerminalWidth()
#5 phar:///usr/local/bin/com in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php on line 975
 

 
 
 
 
 
 
 

我做了三件事 解决了问题 
1)php.ini 中增加内存
 
I increased the PHP memory_limit from the default 128M to 512M and restart the server
2)

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1
3)重启 apache 服务器
 

A bit old but just in case someone new is looking for a solution, updating your PHP version can fix the issue.

Also you should be committing your composer.lock file and doing a composer install on a production environment which is less resource intensive.

More details here: https://github.com/composer/composer/issues/1898#issuecomment-23453850

 
5 
BRILLIANT! That worked. I didn't upgrade my PHP, but committing composer.lock file and updating installed all things properly. Thank you. – ericbae Oct 5 '13 at 6:02
   
Late to the party, but I simply switched off Apache and MySQL. There is a reason I am using a 512MB RAM VPS, don't want to spend monies. – Kumar Mar 20 at 14:13

Looks like you runs out of swap memory, try this

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

as mentioned by @BlackBurn027 on comments below, this solution was described in here

 
11 
You sir, are a genius - its not a php issue and removing files will no help. I have to follow your instructions (with sudo) on my VPS. This is the only one useful answer here for VPS owners. – Croll Dec 29 '15 at 10:27
   
Its a perfect solution which always helps me :) – Bastin Robin Dec 30 '15 at 3:53
   
This worked for me too and I didn't have to update my PHP. Thanks! – Jed Aug 27 '16 at 3:34
   
This worked for me. With this solution I increased memory_limit from the default 128M to 512M – Piusha Sep 16 '16 at 3:52
2 

I have faced the same issue. I am on a AWS Free Microinstance which has less memory. I always try one of the below options and it always works (Before all this please check if you have the latest version of composer installed)

sudo php -dmemory_limit=750M composer.phar update

or remove the contents of the vendor folder and try composer update.

sudo rm -rf vendor
sudo php -dmemory_limit=750M composer.phar update --no-scripts --prefer-dist
sudo php artisan --dump-autoload

The second option tries to update all the components, if there is no update, it picks up the package from the cache else picks up from the dist

Note: Please change the memory limit as per your choice.

or

Create a swap partition and try. Swap partition is the portion of the hard drive that linux uses as virtual memory when it runs out of physical memory. It's similar to the windows swap file only instead of using an actual file, linux uses a partition on the hard drive instead.

Hope this helps

 
1 
I dropped the memory limit down to 500M and composer installed what I needed. – devNoise Dec 9 '13 at 19:47
   
you can also use an actual swap file instead of a partition. see cyberciti.biz/faq/linux-add-a-swap-file-howto – rwilson04 Dec 17 '13 at 18:56
2 
Had same problem on my Digital Ocean account even at 250M I also had to stop Apache and MySQL first before I could run it – tristanbailey Jun 14 '14 at 19:42
   
tristanbailey your solution worked, thanks! Before I stopped mysql and apache I couldn't update, even by setting the php memory limit. – Elias Kouskoumvekakis Dec 6 '15 at 1:03

I had the same issue I solved the problem by using the following

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

Source: Memory limit errors#

answered Jan 30 '15 at 21:46
Koray Güclü
1,99912226
 
   
This worked for me but can you explain these commands in more detail. This would make your answer better than the currently most upvoted answer which says the same thing. – Joseph Astrahan Feb 27 at 15:25

I had a same issue on vagrant. I fixed it by allcate more memory.

 config.vm.provider :virtualbox do |vb|
      vb.customize ["modifyvm", :id, "--memory", "1024"]
 end
answered Oct 1 '14 at 3:53
 
   
Solved this issue for me too. Note that in vagrant 2.x instead of vb.customize ... you can do vb.memory = 1024. – acobster Oct 21 '16 at 17:55

Easy, type this commands:

rm -rf vendor/

rm -rf composer.lock

php composer install --prefer-dist

Should work for low memory machines

answered Aug 30 '14 at 2:26
insign
7601013
 
   
Worked for me. I have written a comprehensive answer over here - stackoverflow.com/questions/26850332/…– halkujabra Nov 10 '14 at 18:48
   
It's not working. And this deleted all my vendor folder. It's wrong! – Vladimir Kovalchuk Nov 15 '16 at 13:03
   
What is the problem delete the vendor? just run install/update again... – insign Nov 16 '16 at 18:06

This seems to be a recurring issue with 1GB and smaller server instances. Apart from trying to shutdown processes and tweak swap settings, you could install on a local machine and upload.

answered Aug 8 '13 at 1:46
Makita
1,615812
 
1 
yep. Funny thing is if I delete the whole project, do a fresh git pull and then do composer install, it seems to work. Strange. – ericbae Aug 8 '13 at 1:57
   
Frustrating, I am deploying a few L4 projects to 1GB Linode instances and am now worried about hitting the memory ceiling. – Makita Aug 8 '13 at 2:01
   
An old link but may be of some use: github.com/composer/composer/issues/1104 – Makita Aug 8 '13 at 2:07
1 
The accepted answer is still the best way to go. You should commit composer.lock and then run an install instead of update on the production server. – Makita Jan 28 '14 at 3:08

Here is the workaround that I found that works for me every time:

df -h 
dd if=/dev/zero of=/swapfile bs=1M count=1024
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
mkswap /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo 'echo "/swapfile  none  swap  defaults  0  0" >> /etc/fstab' | sudo sh

free -m

confirm u see your swap there:
total used free shared buffers cached
Mem: 494 335 158 0 19 62
-/+ buffers/cache: 254 240
Swap: 1023 3 1020

watch free -m
answered Oct 1 '14 at 5:28
Goran
463310
 
1 
Wowwow, posting the commands without explaining what they do is probably dangerous! Although I can definitively say it would work on most systems. Basically you are creating allocating Swap space so that the HD can be used as RAM. Here is a comprehensive guide on how to do that explaining each command: digitalocean.com/community/tutorials/… – adelriosantiago Jul 7 '15 at 16:50
   
Thank you for the clarification of the commands, it is nice to know what the script actually do before you execute it. But in the same way it is not nice how you are promoting digital ocean and your tutorial/blog post.– Goran Oct 17 '15 at 15:11

I get into this situation most of the times so normally i used to follow the step of setting the swap memory.

But now i found a simple alternate trick which worked for me.

Run composer update --no-dev Other than composer update

answered Dec 30 '15 at 3:57
 

I solved the same problem in Vagrant. I increased the value of memory_limit and delete composer cache: sudo rm -R ~/.composer and finally vagrant reload.

answered May 15 '16 at 14:47
oussaka
34837
 

Try this:

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

This work for me on Centos 6

answered Jun 11 '16 at 17:33
Adonias
44269
 
   
this worked for me on AWS Opsworks EC2 Instance Ubuntu 14.04! Thanks! – Paul Preibisch Nov 9 '16 at 23:35

have same problem with php composer.phar update on my 512mb hosting.

solved with php composer.phar install

 

Sometimes by self-updating composer it solves the problem

php composer.phar self-update

Cheers

answered Sep 17 '15 at 18:47
 
   
This should be the first thing to try to fix the issue. I had problem with 1.3.3, self-update to 1.4.0 fixed the issue. Tip: you could update your answer to cover also cover the use-case of composer self-update, if someone not-so-familiar with composer is to read this reply. – Niklaus Mar 10 at 10:05

I had a similar issue on the cheapest server (512MB RAM) hosted with DigitalOcean, and I was also running Jenkins CI on the same server. After I stopped the Jenkins instance the composer install command worked (well, to a point, it failed with the mcrypt extension missing besides already being installed!).

Maybe if you have another app running on the server, maybe its worth trying to stop it and re-running the command.

answered Sep 28 '14 at 21:28
Chris
153216
 

I increased the PHP memory_limit from the default 128M to 512M and restart the server. That solved the problem.

answered Sep 17 '15 at 4:38
Aris
2,33911724
 
   
reboot shouldn't be needed as PHP doesn't runs as a service. – HelpNeeder Apr 27 at 23:22

I tried by just deleting the vendor folder and composer.lock file and then i run the command "composer clear-cache", and then composer install. So it working without any error.

answered Feb 8 '16 at 7:36
 

Make a swap and run composer command again. I hope it'll work for you.

 $ dd if=/dev/zero of=/swapfile bs=1024 count=512k
 $ mkswap /swapfile
 $ swapon /swapfile
 $ echo "/swapfile       none    swap    sw      0       0 " >> /etc/fstab
 $ echo 0 > /proc/sys/vm/swappiness
 $ chown root:root /swapfile
 $ chmod 0600 /swapfile
answered Apr 23 at 8:49
nixon1333
1821718
 

来自  https://stackoverflow.com/questions/18116261/php-composer-update-cannot-allocate-memory-error-using-...



HomeGetting StartedDownloadDocumentationBrowse Packages

Troubleshooting#

This is a list of common pitfalls on using Composer, and how to avoid them.

General#

  1. Before asking anyone, run composer diagnose to check for common problems. If it all checks out, proceed to the next steps.

  2. When facing any kind of problems using Composer, be sure to work with the latest version. See self-update for details.

  3. Make sure you have no problems with your setup by running the installer's checks via curl -sS https://getcomposer.org/installer | php -- --check.

  4. Ensure you're installing vendors straight from your composer.json via rm -rf vendor && composer update -v when troubleshooting, excluding any possible interferences with existing vendor installations or composer.lock entries.

  5. Try clearing Composer's cache by running composer clear-cache.

Package not found#

  1. Double-check you don't have typos in your composer.json or repository branches and tag names.

  2. Be sure to set the right minimum-stability. To get started or be sure this is no issue, set minimum-stability to "dev".

  3. Packages not coming from Packagist should always be defined in the root package (the package depending on all vendors).

  4. Use the same vendor and package name throughout all branches and tags of your repository, especially when maintaining a third party fork and using replace.

  5. If you are updating to a recently published version of a package, be aware that Packagist has a delay of up to 1 minute before new packages are visible to Composer.

  6. If you are updating a single package, it may depend on newer versions itself. In this case add the --with-dependencies argument or add all dependencies which need an update to the command.

Package not found on travis-ci.org#

  1. Check the "Package not found" item above.

  2. If the package tested is a dependency of one of its dependencies (cyclic dependency), the problem might be that Composer is not able to detect the version of the package properly. If it is a git clone it is generally alright and Composer will detect the version of the current branch, but travis does shallow clones so that process can fail when testing pull requests and feature branches in general. The best solution is to define the version you are on via an environment variable called COMPOSER_ROOT_VERSION. You set it to dev-master for example to define the root package's version as dev-master. Use: before_script: COMPOSER_ROOT_VERSION=dev-master composer installto export the variable for the call to composer.

Package not found in a Jenkins-build#

  1. Check the "Package not found" item above.
  2. Reason for failing is similar to the problem which can occur on travis-ci.org: The git-clone / checkout within Jenkins leaves the branch in a "detached HEAD"-state. As a result, Composer is not able to identify the version of the current checked out branch and may not be able to resolve a cyclic dependency. To solve this problem, you can use the "Additional Behaviours" -> "Check out to specific local branch" in your Git-settings for your Jenkins-job, where your "local branch" shall be the same branch as you are checking out. Using this, the checkout will not be in detached state any more and cyclic dependency is recognized correctly.

I have a dependency which contains a "repositories" definition in its composer.json, but it seems to be ignored.#

The repositories configuration property is defined as [root-only] (../04-schema.md#root-package). It is not inherited. You can read more about the reasons behind this in the "why can't composer load repositories recursively?" article. The simplest work-around to this limitation, is moving or duplicating the repositories definition into your root composer.json.

I have locked a dependency to a specific commit but get unexpected results.#

While Composer supports locking dependencies to a specific commit using the #commit-ref syntax, there are certain caveats that one should take into account. The most important one is documented, but frequently overlooked:

Note: While this is convenient at times, it should not be how you use packages in the long term because it comes with a technical limitation. The composer.json metadata will still be read from the branch name you specify before the hash. Because of that in some cases it will not be a practical workaround, and you should always try to switch to tagged releases as soon as you can.

There is no simple work-around to this limitation. It is therefore strongly recommended that you do not use it.

Need to override a package version#

Let's say your project depends on package A, which in turn depends on a specific version of package B (say 0.1). But you need a different version of said package B (say 0.11).

You can fix this by aliasing version 0.11 to 0.1:

composer.json:

{
    "require": {
        "A": "0.2",
        "B": "0.11 as 0.1"
    }
}

See aliases for more information.

Memory limit errors#

Composer may sometimes fail on some commands with this message:

PHP Fatal error: Allowed memory size of XXXXXX bytes exhausted <...>

In this case, the PHP memory_limit should be increased.

Note: Composer internally increases the memory_limit to 1.5G.

To get the current memory_limit value, run:

php -r "echo ini_get('memory_limit').PHP_EOL;"

Try increasing the limit in your php.ini file (ex. /etc/php5/cli/php.ini for Debian-like systems):

; Use -1 for unlimited or define an explicit value like 2G
memory_limit = -1

Or, you can increase the limit with a command-line argument:

php -d memory_limit=-1 composer.phar <...>

This issue can also happen on cPanel instances, when the shell fork bomb protection is activated. For more information, see the documentation of the fork bomb feature on the cPanel site.

Xdebug impact on Composer#

To improve performance when the xdebug extension is enabled, Composer automatically restarts PHP without it. You can override this behavior by using an environment variable: COMPOSER_ALLOW_XDEBUG=1.

Composer will always show a warning if xdebug is being used, but you can override this with an environment variable: COMPOSER_DISABLE_XDEBUG_WARN=1. If you see this warning unexpectedly, then the restart process has failed: please report this issue.

"The system cannot find the path specified" (Windows)#

  1. Open regedit.
  2. Search for an AutoRun key inside HKEY_LOCAL_MACHINE\Software\Microsoft\Command ProcessorHKEY_CURRENT_USER\Software\Microsoft\Command Processor or HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Command Processor.
  3. Check if it contains any path to non-existent file, if it's the case, just remove them.

API rate limit and OAuth tokens#

Because of GitHub's rate limits on their API it can happen that Composer prompts for authentication asking your username and password so it can go ahead with its work.

If you would prefer not to provide your GitHub credentials to Composer you can manually create a token using the following procedure:

  1. Create an OAuth token on GitHub. Read more on this.

  2. Add it to the configuration running composer config -g github-oauth.github.com <oauthtoken>

Now Composer should install/update without asking for authentication.

proc_open(): fork failed errors#

If composer shows proc_open() fork failed on some commands:

PHP Fatal error: Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory' in phar

This could be happening because the VPS runs out of memory and has no Swap space enabled.

free -m

total used free shared buffers cached
Mem: 2048 357 1690 0 0 237
-/+ buffers/cache: 119 1928
Swap: 0 0 0

To enable the swap you can use for example:

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

You can make a permanent swap file following this tutorial.

Degraded Mode#

Due to some intermittent issues on Travis and other systems, we introduced a degraded network mode which helps Composer finish successfully but disables a few optimizations. This is enabled automatically when an issue is first detected. If you see this issue sporadically you probably don't have to worry (a slow or overloaded network can also cause those time outs), but if it appears repeatedly you might want to look at the options below to identify and resolve it.

If you have been pointed to this page, you want to check a few things:

  • If you are using ESET antivirus, go in "Advanced Settings" and disable "HTTP-scanner" under "web access protection"
  • If you are using IPv6, try disabling it. If that solves your issues, get in touch with your ISP or server host, the problem is not at the Packagist level but in the routing rules between you and Packagist (i.e. the internet at large). The best way to get these fixed is raise awareness to the network engineers that have the power to fix it. Take a look at the next section for IPv6 workarounds.

  • If none of the above helped, please report the error.

Operation timed out (IPv6 issues)#

You may run into errors if IPv6 is not configured correctly. A common error is:

The "https://getcomposer.org/version" file could not be downloaded: failed to
open stream: Operation timed out

We recommend you fix your IPv6 setup. If that is not possible, you can try the following workarounds:

Workaround Linux:

On linux, it seems that running this command helps to make ipv4 traffic have a higher prio than ipv6, which is a better alternative than disabling ipv6 entirely:

sudo sh -c "echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf"

Workaround Windows:

On windows the only way is to disable ipv6 entirely I am afraid (either in windows or in your home router).

Workaround Mac OS X:

Get name of your network device:

networksetup -listallnetworkservices

Disable IPv6 on that device (in this case "Wi-Fi"):

networksetup -setv6off Wi-Fi

Run composer ...

You can enable IPv6 again with:

networksetup -setv6automatic Wi-Fi

That said, if this fixes your problem, please talk to your ISP about it to try and resolve the routing errors. That's the best way to get things resolved for everyone.

Composer hangs with SSH ControlMaster#

When you try to install packages from a Git repository and you use the ControlMaster setting for your SSH connection, Composer might just hang endlessly and you see a sh process in the defunct state in your process list.

The reason for this is a SSH Bug: https://bugzilla.mindrot.org/show_bug.cgi?id=1988

As a workaround, open a SSH connection to your Git host before running Composer:

ssh -t git@mygitserver.tld
composer update

See also https://github.com/composer/composer/issues/4180 for more information.

Zip archives are not unpacked correctly.#

Composer can unpack zipballs using either a system-provided unzip utility or PHP's native ZipArchive class. The ZipArchive class is preferred on Windows. On other OSes where ZIP files can contain permissions and symlinks, the unzip utility is preferred. You're advised to install it if you need these features.

来自  https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors


普通分类: