Ubuntu Core images come with everything needed to enable swap, but it’s disabled by default. To enable it:

  1. edit /etc/default/swapfile and set SIZE to a non-zero value.
  2. sudo systemctl enable --now swapfile.service

That’s all that’s needed.

Now some more details:

  • /etc/default/swapfile is writable, but some editors might be confused by its directory being readonly. The provided vi is fine with it though.
  • If SIZE is zero, even if the service is enabled and started, no swapfile is created (see the /usr/bin/mkswapfile shell script for details).
  • If you set FILE to something on a different partition, you should add an appropriate After= to the service file.
  • To add that After=, edit /etc/systemd/system/swapfile.service, and systemctl daemon-reload before you enable and start the service.

That’s it.