Create Swap Partition in Ubuntu: Easy Guide
To create a swap partition in Ubuntu, you can follow these steps:
- Open the terminal and run it as administrator.
- Use the fdisk -l command to view the available disk partitions on the current computer. Identify the disk where you want to create the swap partition.
- Use the command “sudo fallocate -l
/swapfile” to create a swap file, where represents the amount of space you want to allocate to the swap partition, for example, 1G for 1GB. Make sure to choose an appropriate size. - Run the command “sudo chmod 600 /swapfile” to make sure only the root user can read and write to the file.
- Execute the “sudo mkswap /swapfile” command to create a swap partition.
- Enable the swap partition by running the command “sudo swapon /swapfile”.
- Use the command “sudo nano /etc/fstab” to edit the fstab file, and add the following line to the end of the file:
- Do not use a swapfile; disable swap.
- Save and close the file.
- Run the command “sudo sysctl vm.swappiness=10” to adjust the usage level of the swap partition. This will make the system more likely to use memory instead of the swap partition. You can adjust this value according to your needs.
- To adjust the system’s usage of file cache, run the command “sudo sysctl vm.vfs_cache_pressure=50”. This will affect the speed at which the system clears file caches. You can adjust this value according to your needs.
- Restart the computer to apply the changes.
After completing the above steps, your system will have a new swap partition. You can use the “free -h” command to verify if the swap partition was successfully created.