1. Home
  2. Helpful Tips
  3. How to Add a Swap File

How to Add a Swap File

Swap files provide some protective buffer to prevent application crashes when your server (or cloud VPS) runs out of memory.

Here’s how to create a swap file in Ubuntu:

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 2.48383 s, 432 MB/s
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=2e64-4e75-b04e-bbbd915d81f7-8ddef4fa
$ sudo swapon /swapfile

In addition to WordPress sites on small servers, I’ve run into this running composer update:

$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)                       
Package operations: 3 installs, 17 updates, 0 removals                   
  - Updating swiftmailer/swiftmailer (v5.4.6 => v5.4.7): The following exception is caused by a lack of memory or swap, or not having swap configured
Check https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors for details

PHP Warning:  proc_open(): fork failed - Cannot allocate memory in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php on line 973

Warning: proc_open(): fork failed - Cannot allocate memory in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php on line 973
                                                     
  [ErrorException]                                   
  proc_open(): fork failed - Cannot allocate memory  
Updated on May 5, 2017

Was this article helpful?

Related Articles

Leave a Comment