Adding swap space to any Linux VPS instance
In this post I want to share a little (and very simple) tutorial that has helped me quite a lot when using Linux VPS (Virtual Private Server) instances.
The problem: cheap Linux VPS instances – such as the ones you can get on Vultr for instance – often do not have much RAM. While the CPU power these Linux VPS have available will be enough for most tasks, you will find out that the system might experience RAM bottlenecks.
The solution: create and activate some Swap space on your Linux VPS instance. Especially when the Linux VPS instance has a SSD (Solid State Drive) drive the Swap space will perform quite fast.
The tutorial:
- Open the Terminal of your Linux VPS instance and execute the commands given below.
- Change directory to root:
cd /
- Create an empty file of the required size. In this example I am creating a 3GB swap file. Should you need more (or less) space, enter your own value. E.g. to set the size of the swap file at 1GB, change the count value from 3000 (in the example above) to 1000.
sudo dd if=/dev/zero of=swapfile bs=1M count=3000
- Now change the file created to a swap file with the command below.
sudo mkswap swapfile
- Change the permissions of the swap file to 600.
sudo chmod 600 swapfile
- Turn on the swap file.
sudo swapon swapfile
- To ensure that the swap file is turned on automatically at system startup, open the file system table configuration file (
/etc/fstab
) with your text editor of choice (I am usingnano
in this example).
sudo nano /etc/fstab
- Add the following line to the file system table configuration file.
/swapfile none swap sw 0 0
- Save and close the file system table configuration file.
- Check if the system is using the swap file you created with the following command.
cat /proc/meminfo
I really hope this tutorial can be useful to some of you! Please do not hesitate dropping a comment in case you have any questions.