Raspberry Pi has only 256 MB of RAM, so I would like to use swap space (either on SD card or attached USB storage). How do I set it up?

  • 10
    Swap on the Pi (and similar devices) can be painfully slow, and anything that actually ends up using swap extensively will practically bring the system to a standstill. Careful!
    – MattJ
    Jun 12, 2012 at 21:40
  • 1
    You know what would be good is a USB RAM drive (not flash or SSD but actual volatile ram chip running at USB speeds) It would do good for a such a swap pretty good.
    – Piotr Kula
    Aug 15, 2012 at 9:34
  • 3
    @Tibor The question is "How to set up swap space?" In my opinion, a correct answer should describe how to do it as well as bring attention to some pitfalls and warn against misuse. Your answer does just that while the "most popular" answer only tells you why you should never "do this at all" (which is dumb, because there is a proper way to use it, and it actually is useful). Dec 19, 2013 at 19:21
  • 1
    There's about a million warnings against using swap with a raspberry pi on this page, but if you need to build something once, it's unavoidable.
    – Archonic
    Aug 17, 2015 at 22:59
  • 1
    @Archonic - +1. I run out of memory compiling files on occasion. A small swap file with a low swapiness will be an improvement over the current situation.
    – user50099
    Jul 28, 2016 at 2:21

5 Answers 5


Raspbian uses dphys-swapfile, which is a swap-file based solution instead of the "standard" swap-partition based solution. It is much easier to change the size of the swap.

The configuration file is:


The content is very simple. By default my Raspbian has 100MB of swap:


If you want to change the size, you need to modify the number and restart dphys-swapfile:

/etc/init.d/dphys-swapfile restart

Edit: On Raspbian the default location is /var/swap, which is (of course) located on the SD card. I think it is a bad idea, so I would like to point out, that the /etc/dphys-swapfile can have the following option too: CONF_SWAPFILE=/media/btsync/swapfile

I only problem with it, the usb storage is automounted, so a potential race here (automount vs. swapon)

  • 3
    This is the correct answer. None of other answers mentioned the dphys-swapfile. Nov 18, 2014 at 6:05
  • 2
    I have upvoted this answer as really correct one. Learnt all of that hard way quite recently, your answer could save me a lot of time. ;-) Feb 17, 2015 at 22:08
  • Could you clarify why using /var/swap is a bad idea? May 21, 2018 at 0:24
  • to install R packages, which failed before due to "system call failed cannot allocate memory": Set CONF_SWAPSIZE=1024. Less is not enough. Mar 15, 2019 at 10:29
  • Since this is useful only once a while, a separate swap file is a better solution IMHO. See user13's answer Mar 5, 2020 at 15:51

You can set up swap space quite simply. For example, if your USB drive is /dev/sdx, you would use (you must be root for this):

$ mkswap /dev/sdx
$ swapon /dev/sdx

Note that this would use the whole device and you will probably lose all the existing data on it.

You can also create a swap file (by using a loop device) like this:

$ dd if=/dev/zero of=/path/to/swapfile bs=1M count=1024 # For 1GB swap file
$ mkswap /path/to/swapfile
$ swapon /path/to/swapfile

When you no longer need the swap file (if you want to eject the USB drive for example), you must use swapoff <device>. Not doing so will probably result in system crash.

You should be careful though. SD cards have limited read/write limits and it will shorten its lifespan. If you are using an external hard drive, you should be fine, but it will be very slow.

  • 2
    Why add the loopback? Surely that's not necessary?
    – popey
    Jun 12, 2012 at 21:47
  • You're right, it is not. I edited the answer to reflect that.
    – user13
    Jun 12, 2012 at 21:49
  • 2
    I think you should change this to avoid users copy and pasting and accidentally running mkswap on their root partition. I think sdx is a good convention.
    – Jivings
    Jun 12, 2012 at 23:21
  • 3
    Users who choose to enable SWAP may be interested in adjusting kernel swappiness.
    – earthmeLon
    Jul 15, 2012 at 8:25
  • @earthmelon Raspbian Jessie has swappiness 1 by default.
    – kaay
    Mar 11, 2016 at 13:40

Do not do this at all.

You should not enable swap on the Raspberry Pi.

Although it is possible, it is not useful. Even on a class 10 SDHC card, it is just too slow. Also you will reduce the lifespan of the SD card.

On any flash-based storage device (SD card, SDD, USB thumb drives) you are also likely to see system-wide pauses while a large group of flash blocks is erased.

Possible exceptions:

  • If you connect a (magnetic) hard drive (though a USB-SATA or USB-IDE adapter)
  • If you use ZRAM or something similar
  • 3
    no doubt swapping on a USB bey will kill your key very fast, swapping on the SD card is also dangerous, even if newer ( class 10 ) SD cards could support it better. ZRAM is clearly the way to go if you need more RAM
    – neofutur
    Jun 22, 2012 at 16:05
  • 42
    Why does this answer have so many upvotes? There are plenty of reasons to use swap. I used a swap file on an external drive to compile a large library over the course of a few days. It's slow as dead monkeys floating in molasses, but that doesn't meant that you should "not do this at all." Dec 17, 2013 at 20:31
  • 2
    @finnw You just have to be careful, and yes, there are usually better options, but sometimes you just do what you have to do. Dec 17, 2013 at 22:11
  • 13
    Your warning, though well meaning, is way too strong. Finding out 'why not' as well as 'why' is exactly what the Raspberry Pi is for: try whatever you want with it and LEARN. There are also some good reasons to use an external drive for swap as well, such as satisfying a peak memory requirement for a one-off library compile; the search that brought me here. The times I learned the most is when I screwed up something minor and realised why. Sorry to say, you're not being as helpful as you think you are. May 13, 2015 at 11:05
  • 4
    Way too strong. "best not", maybe, but "bad, wasteful, damaging and useless" is misleading. Everything wears with use, bear in mind oxygen is toxic, and Raspbian does use a swapfile. A slowdown is usually better than a crash, and lets you compile huge things once every few months. Just set low swappiness and you're good.
    – kaay
    Mar 11, 2016 at 8:14

Raspbmc uses /etc/init/swap.conf to configure swap via /swap file. It first checks for presence of /home/pi/.enable_swap.

If you delete /home/pi/.enable_swap then swap file is not created, and then just recreate it with touch /home/pi/.enable_swap if you need swap turned on and reboot.


It is a pity that the Raspberry Pis do not have GigaBit Ethernet but it is at least theoretically possible to have swap space on a network device - the Linux Terminal Server Project can offer it from the server to the clients according to this item on their wiki.

I found a Foundation Forum topic "Tip: Swap over nfs" that shows how someone who already had some NFS mounts already in place used a swapfile on one of them and mounted it via a loop-mount (possibly needed because Linux does not allow a NFS mount to be used directly). Given that the remote swap-file will hold data that the OS must not lose I'd only consider this for a wired Ethernet network - a wireless link would be too fragile IMHO. Also, of course you must not allow the remote server to be shut-down without turning the swap off and allowing it to completely empty first!

  • RPi wouldn't benefit from GB Ethernet at all, due to the limited bandwidth between the Broadcom chip and the Ethernet controller. Nov 10, 2016 at 15:28
  • 1
    ...and the Ethernet controller as an add-on to the USB chip! Yeah, but we can dream of a hypothetical RPi design where the networking hardware was part of the SoC core device. 8-P
    – SlySven
    Nov 10, 2016 at 16:09
  • Ethernet controllers are often external chips, but USB as an inter-chip bus is indeed bad for both speed and power consumption. Nov 10, 2016 at 16:32