@Zeokat Horrible advice. Stop it!
@TorchPE If you HAVE to use SWAP, then use twice your RAM size, in your case, 1024MB, tho, SWAP is NOT recommended on DigitalOcean servers, they themselves highly recommend that you don't use SWAP as it deteriates the SSD drives.
512MB RAM is more then enough to run Flarum, something else is wrong on your system. I myself run 2 seperate NodeJS websites totalling 25,000 uniques per day on a single 512MB DigitalOcean instance without SWAP. You will be amazed by how much you can do with so "little".
First install htopapt-get install htop
then run it htop
And let us know what htop is returning, your server has to be using resources outside what you mention to get a RAM issue with composer. Also, don't explain what errors you get with composer, copy paste the errors entirely.
PS: When logged in to your terminal, then prepending commands with sudo
is not needed when you are logged in as the user root.