This small snippet disables IPv6 in Ubuntu (tested on 18.10 cosmic).

  1. Add the following lines to the file /etc/sysctl.d/99-sysctl.conf:

    net.ipv6.conf.all.disable_ipv6 = 1
    net.ipv6.conf.default.disable_ipv6 = 1
    net.ipv6.conf.lo.disable_ipv6 = 1
    
  2. Read sysctl values again.

    sudo sysctl -p
    

Automation

echo -e "net.ipv6.conf.all.disable_ipv6 = 1\nnet.ipv6.conf.default.disable_ipv6 = 1\nnet.ipv6.conf.lo.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.d/99-sysctl.conf && sudo sysctl -p

Warning: Do not run the command multiple times as it will append the directives multiple times.