# Create a swapfile for your Linux server

**URL:** https://meta.discourse.org/t/create-a-swapfile-for-your-linux-server/13880
**Category:** Self-Hosting
**Created:** [March 19, 2014, 9:32pm UTC](https://meta.discourse.org/t/create-a-swapfile-for-your-linux-server/13880 "2014-03-19T21:32:59Z")
**Posts on this page:** 1
**Showing post:** 1

<div class="post-metadata">

### Author: ![Discourse](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/discourse/32/148734_2.png) [@Discourse](https://meta.discourse.org/u/Discourse)
#### Post date: [March 19, 2014, 9:32pm UTC](https://meta.discourse.org/t/create-a-swapfile-for-your-linux-server/13880/1 "2014-03-19T21:32:59Z")

</div>

> 💡 For servers with \<= 4GB of RAM, running `./discourse-setup` will prompt for and automatically create a 2GB swapfile.

Most cloud virtual machine providers do not set up swapfiles as part of their server provisioning.

In particular, upgrading Discourse produces a lot of memory pressure. With a swap file, rather than randomly terminating processes with an out of memory error, things will slow down instead. Having a swap file is a cheap insurance policy that protects you from many other load related failures.

This can be done at any time from the command line on your server.

### Set up a 2GB swap file

We recommend a 2GB swap file for Discourse, unless your server has more than 4GB of memory.

In the shell you have opened to your droplet, do the following:

> ⚠ You will need to be root for much of this, so either be root or `sudo -s` before running these commands! Don’t forget to exit root afterwards.

1. Create an empty swapfile

2. Allocate a 2GB file named ‘swapfile’

3. tell linux this is the swap file:

4. Activate it

5. Add it to the file system table so its there after reboot:

6. Set the swappiness to 10 so its only uses as an emergency buffer

The whole thing as a single copy and pastable script:

```
install -o root -g root -m 0600 /dev/null /swapfile
fallocate -l 2G /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile swap swap auto 0 0" | tee -a /etc/fstab
sysctl -w vm.swappiness=10
echo 'vm.swappiness = 10' > /etc/sysctl.d/30-discourse-swap.conf

```

> Last edited by @JammyDodger 2022-05-23T18:45:59Z
> 
> > **Check document**
> >
> > Perform check on document:

---

_[View the full topic](https://meta.discourse.org/t/create-a-swapfile-for-your-linux-server/13880)._
