"Cannot allocate memory" when upgrading

It looks like this is your problem:

“Cannot allocate memory”

The solution here is to stop apache, stop mysql, stop all the services you can, temporarily, such that you have free RAM and can build the gems. Then post-build you can restart them.

Failing that if you’re on a virtual machine pay for more RAM.