UI upgrades require more memory I believe because you are trying to run the site and rebuilding simultaneously.
Make sure you have enough swap to cope - at least as much as your RAM.
So on 4GB server make sure you have 4GB swap.
Btw the git pull here is redundant. The build script does this for you.