Thursday 7 May 2020

Composer: How to fix error "Fork failed errors" in Linux server


In many cases we encounter lack of memory errors when running "composer install" or "composer update" command to update packages in our PHP project.
Some of the error messages you may receive:

  • - proc_open(): fork failed errors - Cannot allocate memory
  • - exception is caused by a lack of memory
  • - Killed

The cause of all these errors is because our server does not have enough RAM to allocate to Composer. Increasing packages in the project, the larger the size, so this error is also becoming more and more common. To fix them you can increase the server's physical RAM but the cost will be quite expensive.

There is another solution simpler and completely free to fix Composer memory error. That is we can enable swap space for the server. This is quite easy to implement and will thoroughly fix the error we encounter. For each operating system, there will be different ways to initialize swap partitions. You can refer to one of the instructions below:

Enable Swap to fix Composer memory error for Centos, Ubuntu ...:

Step 1: Create a new Swap file (1GB to 3Gb)
sudo fallocate -l 3G /swapfile
Step 2: Add Write permission for Swap file
sudo chmod 600 /swapfile
* If your server is running under SELinux, after chmod command you must run the following command
sudo chcon -t swapfile_t /swapfile 
Step 3: set up a Linux swap area
sudo mkswap /swapfile
Step 4: Active Swap for server
sudo swapon /swapfile
opening the "/etc/fstab" file
vi /etc/fstab
then add the following line at end of file.
/swapfile swap swap defaults 0 0

Done ! If there are no errors, you have successfully enabled the swap for your server. You can try checking the swap partition with one of the following commands:
sudo swapon --show
or
top
or
sudo free -h

Now it's time to try running the composer commands again. Hopefully the bug has been fixed. Good luck.

*Note:
- /swapfile  - is file name for swap file, you can use another name or put it in another folder.
- In addition to using swap files, you can also try to use swap partitions.
- Some problems with PHP modules can also cause errors because Composer runs on PHP

*Refer: https://getcomposer.org/doc/articles/troubleshooting.md

0 nhận xét:

Post a Comment