Skip to content

Blog

Add swap space to Ubuntu

Truffle compilation was crashing with memory-overrun issues in my 512 MB basic droplet. So I added 2 GB of swap space with following commands.

Make sure you have no swap config previously. There should be no output.

$ sudo swapon --show
$

The simple way of doing this is by creating a swap file. Lets look at all the partitions to see how much free space in each of them.

$ df -h
kaba@ubuntu-512mb-blr1-01:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            238M     0  238M   0% /dev
tmpfs            49M  5.9M   43M  13% /run
/dev/vda1        20G  5.2G   15G  27% /
tmpfs           245M   24M  221M  10% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           245M     0  245M   0% /sys/fs/cgroup
/dev/vda15      105M  3.4M  102M   4% /boot/efi
tmpfs            49M     0   49M   0% /run/user/0
tmpfs            49M  8.0K   49M   1% /run/user/1000
$
* Other than the ones with /dev/ are virtual filesystem and are not usable. * I'm gonna create my swap file in /dev/vda1.

Use fallocate utility to create a preallocated file in root directory.

$ sudo fallocate -l 2G /swapfile
* As in previous command output, dev/vda1 is mounted on / * 2G specifies 2 GB of space. Use whatever size you want in that place.

Make the is file accessible to root user alone

sudo chmod 600 /swapfile

Mark the file as swap space

$ sudo mkswap /swapfile

Enable the swap file, so your system will start using it

$ sudo swapon /swapfile

Done!

Now swapon command shouldn't return an empty result

$ sudo swapon --show
NAME      TYPE SIZE   USED PRIO
/swapfile file   2G 771.6M   -1


Make swap settings permanent

You need to edit /etc/fstab file. So better take a backup of it.

$ sudo mv /etc/fstab /etc/fstab.orig

Update the file with swap information

$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Configure Vim

The above picture is from my favorite serial Mr.Robot. Elliot Alderson is writing a script to hack Evil Corp.

I have three development environments * One Cent-OS machine for C programming in office * Ubuntu-16.04 Digitalocean and AWS virtual machines to host some servers and Ethereum developemnt * And one Raspberry PI 3 at home as an SSH client

As the Cent-OS machine has older version of vim, Plugins that require latest version of vim and plugins that are only needed for Web/Ethereum development are kept under an if condition. So modify it based on your need.

Here I didn't give any explanation for any config or plugin. Copy pasting them in Google will give you the information.

Backup you vimrc file

$ mv ~/.vimrc ~/.vimrc.orig

Clone Vundle into your ~/.vim directory

$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Copy this vimrc file into your home directory.

Install plugins using PluginInstall command. Open vim with no file. Ignore if any error shows up. Press Esc key then : and type the command PluginInstall followed by Enter key. A new window will open and install all plugins.

$ vim

:PluginInstall

Don't forget to install 'YouCompleteMe' in your machine.

$ cd ~/.vim/bundle/YouCompleteMe/
$ ./install.py --js-completer --clang-completer --system-libclang
Installing auto-complete feature for JavaScript and C Language. See YouCompleteMe GitHub page for more details.