swapon has no effect, /proc/swaps remains empty

Solution 1:

First, erase your original swapfile:

sudo rm /myswapfile

Next, run the following commands one by one:

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
ls -lh /swapfile

The output should be similar to the following:

-rw------- 1 root root 1.0G Aug  3 18:20 /swapfile

Now, run the following commands:

sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon -s

Now, edit /etc/fstab to make the changes permanent:

sudo nano /etc/fstab

and add the following line to the end of the file:

/swapfile    none            swap    sw              0       0

Press CTRL + o and then press ENTER to save the file.

Press CTRL + x to exit nano.

source: https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04


If that still doesn't work, you may want to check swappiness:

cat /proc/sys/vm/swappiness

This command should return a number. The default should be 60.

If no output, run the following commands one by one:

sudo sysctl vm.swappiness=60
echo "vm.swappiness = 60" | tee -a /etc/sysctl.conf

The -a is important after tee or you will overwrite the entire file!!!

Solution 2:

Use fdisk to create a swap. Add a new partition, select Linux swap, write the changes. mkswap /dev/sd? You need to add it to your /etc/fstab/. Change XXX to the swap UUID.

UUID=XXX none            swap    sw              0       0

Run swapon -a to make it available from /etc/fstab.

Solution 3:

You swap file should be in /swapfile with 600 rights.

#moving file and changing rights
sudo mv /root/myswapfile /myswapfile
sudo chmod 600 /myswapfile

sudo mkswap /myswapfile

add swap file. I think you forget this command

sudo swapon /myswapfile
#check swap is working
sudo swapon -s