Adding a swap file to an Ubuntu 12.04 system

I’ve started to move myself over to Amazon EC2 servers. I still have some at Rackspace and like a lot of what they have to offer, but I think the seemingly lower cost and flexibility offered with EC2 is becoming better suited to my business. But that’s just me right now. The problem is, I am trying out their free tier usage right now, and have a micro server. That server has 600MB or RAM and by default comes with no swap.  A production server, in a perfect world, is never swapping, but a server with only 600MB of RAM could swap with one person looking at a drupal site.  It does slow the server down a lot, but’s acceptable for a dev server in my opinion and better than spending twice as much on a dev server that doesn’t swap.  After all, I usually develop locally so it’s more of a client review server than an actual dev server.  No one wants a server running out of RAM while a client is reviewing a site and mysql just randomly stopping on you.

It only comes with an 8GB hard drive, which I alreadly have 1.3GB used with a single wordpress install, Ubuntu, Apache, PHP, and MariaDB, so using 1GB of that for swap might be risky, but I don’t imagine having much more than that on a dev server, so I’m gonna create a 1GB swap file on the hard drive.

First I decided to create a swap directory in the root. Most people put swap on it’s own partitian at something like /dev/sda2, but I’m just gonna put mine at /swp.

[code]
sudo mkdir /swp
[/code]

Then I create the swap file and set the right permissions.

[code]
sudo fallocate -l 1024m /swp/1024MiB.swap
sudo chmod 600 /swp/1024MiB.swap
[/code]

Make it a swapping device.

[code]
sudo mkswap /swp/1024MiB.swap
[/code]

Adding it to the system

[code]
sudo swapon /swp/1024MiB.swap
[/code]

Add it to fstab to make it permanent

[code]
sudo vim /etc/fstab
[/code]

Add this to the end of that file.

[code]
/swp/1024MiB.swap none swap sw 0 0
[/code]

The information for this posting is the contents of this page https://help.ubuntu.com/community/SwapFaq that applied to my situation.

Leave a Reply

Your email address will not be published. Required fields are marked *