I was trying a few days back to deploy a node.js application on a virtual machine with only 512MB of RAM and no swap partition. And a neccessary step is to install the dependencies using npm, which, for some of them, needs to clone the package git repository and to build it.

As it turns out, this is a quite expensive operation in terms of memory space, and because on the machine there were some services and daemons already running, the operation failed due to insufficient memory. I tried one more time, but still no luck.

However, the machine had enough disk space available on an SSD drive so I thought: why not dynamicaly add some swap space? To do this just:

  1. Create a new file with the desired size (e.g. 1GB): $ dd if=/dev/zero of=/tmp/myswapfile bs=1M count=1024 or $ fallocate --length 1GiB /tmp/myswapfile
  2. Set up a swap area on that file: $ mkswap /tmp/myswapfile
  3. Enable swapping on the file: $ swapon /tmp/myswapfile

To check that the new swap space is available for the system:

$ free -m
             total       used       free     shared    buffers     cached
Mem:           490        476         13          3         44         95
-/+ buffers/cache:        335        154
Swap:         1023        357        666

or you can check with a tool like htop or top or to cat the contents of /proc/swaps.

After increasing the swap space, I was able to install all the dependencies and to successfully deploy the application.