> For the complete documentation index, see [llms.txt](https://prime-stake-pool.gitbook.io/node-setup-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://prime-stake-pool.gitbook.io/node-setup-guide/server-setup-basics/linux-swap-space/add-swap-space.md).

# Add Swap Space

Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.                                                                                                                                             We will consider creating a swap file on your cloud or local server, with no swap partition created during operating system installation.

### &#x20;**Recommended System Swap Space**

<table><thead><tr><th>Amount of RAM in the system</th><th>Recommended swap space</th><th data-hidden>Recommended swap space if allowing for hibernation</th><th data-hidden></th></tr></thead><tbody><tr><td>⩽ 2 GB</td><td>2 times the amount of RAM</td><td>3 times the amount of RAM</td><td></td></tr><tr><td>> 2 GB – 8 GB</td><td>Equal to the amount of RAM</td><td>2 times the amount of RAM</td><td></td></tr><tr><td>> 8 GB – 64 GB</td><td>At least 4 GB</td><td>1.5 times the amount of RAM</td><td></td></tr><tr><td>> 64 GB</td><td>At least 4 GB</td><td>Hibernation not recommended</td><td></td></tr></tbody></table>

{% hint style="info" %}
"Recommended System Swap Space" are especially important on systems with low memory (1 GB and less). Failure to allocate sufficient swap space on these systems can cause issues such as instability or even render the installed system unbootable.
{% endhint %}

### **Steps involved in swap space/file creation**

#### Step 1 - Checking the system for swap information

Before we begin, we can check if the system already has some swap space available. It is possible to have multiple swap files or swap partitions, but generally one should be enough.

We can see if the system has any configured swap by typing:

```
sudo swapon --show
```

If you don’t get back any output, this means your system does not have swap space available currently.

You can verify that there is no active swap using the `free` utility:

```
free -h
```

<figure><img src="/files/1Die4Zu6HEDEMyntmK2t" alt=""><figcaption></figcaption></figure>

As you can see in the **Swap** row of the output, no swap is active on the system.

#### Step 2 – Checking Available Space on the Hard Drive Partition <a href="#step-2-checking-available-space-on-the-hard-drive-partition" id="step-2-checking-available-space-on-the-hard-drive-partition"></a>

Before we create our swap file, we’ll check our current disk usage to make sure we have enough space. Do this by entering:

```
df -h
```

<figure><img src="/files/OsWWFiP9YO2gJFTXVMml" alt=""><figcaption></figcaption></figure>

The device with `/` in the `Mounted on` column is our disk in this case. We have plenty of space available in this example (only 43G used). Your usage will probably be different.

Although there are many opinions about the appropriate size of a swap space, it really depends on your personal preferences and your application requirements. Generally, an amount equal to or double the amount of RAM on your system is a good starting point. Another good rule of thumb is that anything over 4G of swap is probably unnecessary if you are just using it as a RAM fallback.

#### Step 3 – Creating a Swap File <a href="#step-3-creating-a-swap-file" id="step-3-creating-a-swap-file"></a>

Now that we know our available hard drive space, we can create a swap file on our filesystem. We will allocate a file of the size that we want called `swapfile` in our root (`/`) directory.

The best way of creating a swap file is with the `fallocate` program. This command instantly creates a file of the specified size.

Since the server in our example has 4G of RAM, we will create a 4G file in this guide. Adjust this to meet the needs of your own server:

```
sudo fallocate -l 8G /swapfile
```

We can verify that the correct amount of space was reserved by typing:

```
ls -lh /swapfile
```

<figure><img src="/files/QWHI2bdXQxdgH1juP0gl" alt=""><figcaption></figcaption></figure>

Our file has been created with the correct amount of space set aside.

{% hint style="info" %}
If you are not using swap or want to change the swap size, you can remove it from fstab and delete `/swapfile`. You will also need to turn off the swapfile to remove it.

There is nothing in this operation that may be called "unsafe".
{% endhint %}

```
sudo swapoff /swapfile   
```

```
sudo rm -f /swapfile
```

Now remove this line from /etc/fstab

```
/swap/swapfile swap swap defaults 0 0
```

#### Step 4 – Enabling the Swap File <a href="#step-4-enabling-the-swap-file" id="step-4-enabling-the-swap-file"></a>

Now that we have a file of the correct size available, we need to actually turn this into swap space.

First, we need to lock down the permissions of the file so that only users with **root** privileges can read the contents. This prevents normal users from being able to access the file, which would have significant security implications.

Make the file only accessible to **root** by typing:

```
sudo chmod 600 /swapfile
```

Verify the permissions change by typing:

```
ls -lh /swapfile
```

<figure><img src="/files/n6pGpaI7Emvh4JRcatfc" alt=""><figcaption></figcaption></figure>

As you can see, only the **root** user has the read and write flags enabled.

We can now mark the file as swap space by typing:

```
sudo mkswap /swapfile
```

<figure><img src="/files/NtdrEXjmf2Z5m5Q8ATmC" alt=""><figcaption></figcaption></figure>

After marking the file, we can enable the swap file, allowing our system to start using it:

```
sudo swapon /swapfile
```

Verify that the swap is available by typing:

```
sudo swapon --show
```

```
Output
NAME      TYPE  SIZE USED PRIO
/swapfile file    4G   0B   -2
```

We can check the output of the `free` utility again to corroborate our findings:

```
free -h
```

<figure><img src="/files/TFNqz6r5TxtpLt6MwHBI" alt=""><figcaption></figcaption></figure>

Our swap has been set up successfully and our operating system will begin to use it as necessary.

#### Step 5 – Making the Swap File Permanent <a href="#step-5-making-the-swap-file-permanent" id="step-5-making-the-swap-file-permanent"></a>

Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our `/etc/fstab` file.

Back up the `/etc/fstab` file in case anything goes wrong:

```
sudo cp /etc/fstab /etc/fstab.bak
```

Add the swap file information to the end of your `/etc/fstab` file by typing:

```
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
```

Next we’ll review some settings we can update to tune our swap space.

> This article has a reference from Digital Ocean's community tutorial: [How To Add Swap Space on Ubuntu 22.04](https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-22-04).
>
> For more about [Swap Space](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/storage_administration_guide/ch-swapspace) read Red Hat Linux documentation page.
