以前 Ubuntu 16.04 でスワップ領域を増やした ときの Ubuntu 26.04 版です
今回の環境はそもそもスワップがなかったので、スワップファイルを新しく作ります
試した OS は
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 26.04.1 LTS
Release: 26.04
Codename: resolute$ free -h
total used free shared buff/cache available
Mem: 1.9Gi 1.6Gi 82Mi 69Mi 489Mi 350Mi
Swap: 0B 0B 0B
$ swapon --show
# なにもでない実メモリ 2GB で、スワップは 0B、swapon --show も何も表示されません
スワップなしでメモリの空きも 350Mi ほどなので、メモリが足りなくなるとプロセスが落ちてしまいます
4GB で /swapfile を作ります
$ sudo mkswap --file /swapfile --size 4G
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx最近の util-linux の mkswap は --file と --size オプションでファイルの作成までやってくれます
パーミッションも 600 で作られるので、fallocate や chmod は不要
$ ls -l /swapfile
-rw------- 1 root root 4294967296 Dec 1 10:00 /swapfileちなみに、Btrfs の場合はコピーオンライトを切る必要があるので sudo btrfs filesystem mkswapfile --size 4G /swapfile を使います
ext4 なら上の手順で OK です
ext4かどうか確認するには
$ df -T /
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/vda2 ext4 206295300 16468004 179325336 9% /$ sudo swapon /swapfile
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 4G 0B -2
$ free -h
total used free shared buff/cache available
Mem: 1.9Gi 1.6Gi 82Mi 69Mi 489Mi 350Mi
Swap: 4.0Gi 0B 4.0Giswapon だけだと再起動で外れてしまうので、/etc/fstab に追記します
$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
/swapfile none swap sw 0 0念の為、再起動してきちんとスワップ領域ができてるか確認して完了です
$ sudo reboot
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 4G 0B -2
このリンクは、アフィリエイトリンクです