# 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:** 7
**Page:** 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:

---

<div class="post-metadata">

### Author: ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)
#### Post date: [January 8, 2024, 10:22pm UTC](https://meta.discourse.org/t/create-a-swapfile-for-your-linux-server/13880/77 "2024-01-08T22:22:51Z")

</div>

I’ve been trying to set up a larger swapfile (4GB instead of the default 2GB), but have run into this when running the script above:

> rm: cannot remove `swapfile’: Operation not permitted

To get around this, I’ve had to do this first:

1. Reboot the server (it wouldn’t let me `swapoff`, presumably due to heavy swapfile use)
2. `swapoff -a -v`

---

<div class="post-metadata">

### Author: ![Ed\_S](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ed_s/32/134015_2.png) [@Ed\_S](https://meta.discourse.org/u/Ed_S)
#### Post date: [January 9, 2024, 9:52pm UTC](https://meta.discourse.org/t/create-a-swapfile-for-your-linux-server/13880/78 "2024-01-09T21:52:20Z")

</div>

> [@nathank](#):
>
> I’ve been trying to set up a larger swapfile

For similar reasons, I run with two swapfiles:

```plaintext
# swapon
NAME TYPE SIZE USED PRIO
/var/local/swap/swapfile.0 file 1024M 44.9M -3
/var/local/swap/swapfile.1 file 1024M 1024M -2

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [January 13, 2024, 3:41pm UTC](https://meta.discourse.org/t/create-a-swapfile-for-your-linux-server/13880/79 "2024-01-13T15:41:54Z")

</div>

A second swap file could be a clever way to avoid the reboot. Just create a second swap file, enable it, and then remove the first.

---

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [December 18, 2024, 1:16pm UTC](https://meta.discourse.org/t/create-a-swapfile-for-your-linux-server/13880/80 "2024-12-18T13:16:08Z")

</div>

So we won’t be needing to follow this guide to make the 2GB swap file since the installation already does that if we have less that 2GB of memory?

---

<div class="post-metadata">

### Author: ![Ed\_S](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ed_s/32/134015_2.png) [@Ed\_S](https://meta.discourse.org/u/Ed_S)
#### Post date: [December 18, 2024, 3:03pm UTC](https://meta.discourse.org/t/create-a-swapfile-for-your-linux-server/13880/81 "2024-12-18T15:03:04Z")

</div>

> [@NateDhaliwal](#):
>
> the installation already does that

Yes - but if you change your server config, or perhaps if you do a migration, you might need to look after this yourself. (At the same time there are two important kernel configs to change which the installation does not look after - see [MKJ’s Opinionated Discourse Deployment Configuration](https://meta.discourse.org/t/mkjs-opinionated-discourse-deployment-configuration/193355)

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [April 4, 2025, 12:52pm UTC](https://meta.discourse.org/t/create-a-swapfile-for-your-linux-server/13880/82 "2025-04-04T12:52:47Z")

</div>

10 posts were split to a new topic: [High rebuild memory requirements: April 2025 edition](https://meta.discourse.org/t/high-rebuild-memory-requirements-april-2025-edition/360419)
