# \[SWAP\] Simple method to install a Swap file

**URL:** https://forum.clockworkpi.com/t/swap-simple-method-to-install-a-swap-file/5126
**Category:** Tutorials
**Tags:** tinkering
**Created:** [January 10, 2020, 8:59pm UTC](https://forum.clockworkpi.com/t/swap-simple-method-to-install-a-swap-file/5126 "2020-01-10T20:59:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lasvegas](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/lasvegas/32/769_2.png) [@lasvegas](https://forum.clockworkpi.com/u/lasvegas)
#### Post date: [January 10, 2020, 8:59pm UTC](https://forum.clockworkpi.com/t/swap-simple-method-to-install-a-swap-file/5126/1 "2020-01-10T20:59:51Z")

</div>

A swap file can be very handy in the GameShell, especially when compiling new games… I’m still fairly new to Linux. I was considering adding a swap partition, but remembering the hassle when expanding the partition in CW 1.0, I decided that a swap file would be the simpler and safer route to go. A little research resulted in the following procedure:

1. Create an empty file (1K \* 1M = 1GiB).

```auto
sudo mkdir -v /var/cache/swap
cd /var/cache/swap
sudo dd if=/dev/zero of=swapfile bs=1K count=1M
sudo chmod 600 swapfile

```

1. Convert newly created file into a swap space file.

```auto
sudo mkswap swapfile

```

1. Enable file for paging and swapping.

```auto
sudo swapon swapfile

```

Verify by: `swapon -s` or `top` :

```auto
top -bn1 | grep -i swap

```

Should display line like: `MiB Swap: 1024.0 total, 1024.0 free` To disable, use `sudo swapoff swapfile` command.

1. Add it into `fstab` file to make it persistent on the next system boot.

```auto
echo "/var/cache/swap/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

```

1. Re-test swap file on startup by:

```auto
sudo swapoff swapfile
sudo swapon -va

```

Note: This procedure was taken directly from the [AskUbuntu.com](https://askubuntu.com/questions/33697/how-do-i-add-swap-after-system-installation).  
Shutdown and Restart now take a little longer… It’s not really a problem for me.

---

<div class="post-metadata">

### Author: ![javelinface](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/javelinface/32/3079_2.png) [@javelinface](https://forum.clockworkpi.com/u/javelinface)
#### Post date: [January 10, 2020, 9:56pm UTC](https://forum.clockworkpi.com/t/swap-simple-method-to-install-a-swap-file/5126/2 "2020-01-10T21:56:06Z")

</div>

Thank you so much for this!! This is a much needed tutorial that anyone compiling things should be aware of, especially when say, rebuilding Retroarch. No longer will we need to reduce the -j4 jobs down to two!  
I’d be happy to have this almost as standard. Or even have a scripted toggle in settings to enable/disable the swap file.  
Also haha I had no idea you were new to Linux. I just assumed you were a coder. Thanks again! I am bookmarking this!

---

<div class="post-metadata">

### Author: ![lasvegas](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/lasvegas/32/769_2.png) [@lasvegas](https://forum.clockworkpi.com/u/lasvegas)
#### Post date: [January 10, 2020, 10:05pm UTC](https://forum.clockworkpi.com/t/swap-simple-method-to-install-a-swap-file/5126/3 "2020-01-10T22:05:52Z")

</div>

Hehe… I was a coder for Mac OS. I have a fair amount of experience with Darwin, Apple’s variant of Unix; Very similar, but not quite Linux. It does make it easier for me to understand what I’m dealing with, but crippling when I expect something to work the same…

---

<div class="post-metadata">

### Author: ![adcockm](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/adcockm/32/1664_2.png) [@adcockm](https://forum.clockworkpi.com/u/adcockm)
#### Post date: [January 11, 2020, 12:16am UTC](https://forum.clockworkpi.com/t/swap-simple-method-to-install-a-swap-file/5126/4 "2020-01-11T00:16:18Z")

</div>

Cool, thanks for this, @lasvegas!

Since I wasn’t sure about having the swap on all the time, I created four tiny scripts based on your instructions. I’ll share here in case anyone wants similar reusable tools. (Your step 5 would only work if in that same swap directory from before.)

swapsetup.sh (only run once)

> sudo mkdir -v /var/cache/swap  
> cd /var/cache/swap  
> sudo dd if=/dev/zero of=swapfile bs=1K count=1M  
> sudo chmod 600 swapfile  
> sudo mkswap swapfile  
> sudo swapon swapfile  
> top -bn1 | grep -i swap

swapon.sh (to turn on)

> sudo swapon /var/cache/swap/swapfile

swapoff.sh (to turn off)

> sudo swapoff /var/cache/swap/swapfile

swapcheck.sh (to see if/how much in use)

> top -bn1 | grep -i swap

---

<div class="post-metadata">

### Author: ![r043v](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/r043v/32/672_2.png) [@r043v](https://forum.clockworkpi.com/u/r043v)
#### Post date: [January 11, 2020, 12:54am UTC](https://forum.clockworkpi.com/t/swap-simple-method-to-install-a-swap-file/5126/5 "2020-01-11T00:54:58Z")

</div>

have a 512MB file is enough for big compile like retroarch/kernel/mesa/mupen

swap files can be show using _swapon --show_, and file creation by using _fallocate_ command

i personally directly put the swap file in /

here is on / off oneliner :

> `sudo su - -c "fallocate -l 512M /swap && chmod 600 /swap && mkswap /swap && swapon /swap" && swapon --show`

> `sudo su - -c "swapoff /swap && rm -f /swap"`

arch port directly include an enabled 512MB swap file

we may have a look at [Zswap](https://wiki.archlinux.org/index.php/Zswap) to save a bit our sd cards life

---

<div class="post-metadata">

### Author: ![shell](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@shell](https://forum.clockworkpi.com/u/shell)
#### Post date: [January 14, 2020, 6:09am UTC](https://forum.clockworkpi.com/t/swap-simple-method-to-install-a-swap-file/5126/6 "2020-01-14T06:09:42Z")

</div>

```auto
sudo apt-get install dphys-swapfile

```

```auto
sudo vim /etc/dphys-swapfile

```

set CONF\_SWAPSIZE to the value you want

```auto
sudo systemctl restart dphys-swapfile

```
