How do I set a static IP in Ubuntu?

I had the same problem and this was my solution:

sudo nano /etc/network/interfaces

and paste (altering for your network) this under # The primary network interface:

auto enp0s25
iface enp0s25 inet static
address 192.168.0.16
netmask 255.255.255.0
gateway 192.168.0.1
dns-nameservers 8.8.4.4 8.8.8.8

You can get correct interface name using Terminal command ifconfig -a on ubuntu 16.04 or ip address on 18.04+

Shutdown your Virtual Machine and then!!! Go to network settings and click on refresh MAC address button a few times :)

enter image description here

and start your VM and you should get internet!

UPDATE 20.02.2019

For ubuntu 18.04+ you need to edit this file

/etc/netplan/50-cloud-init.yaml

network:
    ethernets:
        enp0s3:
            addresses: [192.168.0.55/24]
            gateway4: 192.168.0.1
            dhcp4: no
            nameservers:
              addresses: [1.1.1.1,8.8.8.8]
            optional: true
    version: 2

Setting the static IP address as above in the accepted answer here works, but one has to flush the old IP addr setting and then restart networking.service:

sudo ip addr flush enp0s25
sudo systemctl restart networking.service

Then verify it is correct:

ip add

# interfaces(5) file used by ifup(8) and ifdown(8)
# auto lo
# iface lo inet loopback
auto enp2s0 
iface enp2s0 inet static
    address 172.16.9.124
    netmask 255.255.240.0
    gateway 172.16.0.9
dns-nameservers 8.8.8.8