Apt-get force IPV4

EDIT:
Instead of just forcing apt to follow ipv4 it's better to make the pi enforce ipv4 for everything. Here's how you do that.

sudo su  
touch /etc/sysctl.d/local.conf  
echo net.ipv6.conf.all.disable_ipv6=1 > /etc/sysctl.d  
reboot  

I came across an interesting issue recently while trying to configure my new Raspberry Pi 3 Model B. It was taking forever to execute the upgrade command.

# apt-get upgrade

The update worked just fine but upgrade didn't. First I thought it was an issue with the nameserver, which is understandable. But then ping didn't reveal any issues getting in touch with google's DNS.

ping -c 3 8.8.8.8

And I was able to ping http://archive.raspberrypi.org/ as well, yet it still won't budge.

But then I noticed it was trying to enforce ipv6 resolution which was strange.
0% [Connecting to archive.raspberrypi.org (2a00:1098:0:80:1000:13:0:8)]

It was confirmed using traceroute6

pi@raspberrypi:~ $  traceroute6 archive.raspberrypi.org  
traceroute to archive.raspberrypi.org (2a00:1098:0:82:1000:13:0:5), 30 hops max, 80 byte packets  
 1  * * *
 2  * * *
 3  * * *
 4  * * *
 5  * * *
 6  * * *
 7  * * *
 8  * * *
 9  * * *
10  * * *  
11  * * *  
12  * * *  
13  * * *  
14  * * *  
15  * * *  
16  * * *  
17  * * *  
^c

BUT! traceroute (ipv4) worked just fine

pi@raspberrypi:~ $  traceroute archive.raspberrypi.org  
traceroute to archive.raspberrypi.org (93.93.128.211), 30 hops max, 60 byte packets  
 1  10.0.1.1 (10.0.1.1)  104.246 ms  121.182 ms  122.965 ms
 2  96.120.89.53 (96.120.89.53)  189.617 ms  191.155 ms  196.719 ms
 3  port-channel92-ur01.fremontcev3.ca.sfba.comcast.net (68.85.100.221)  199.111 ms  203.607 ms  210.356 ms
 4  te-0-7-0-11-sur04.fremont.ca.sfba.comcast.net (68.85.154.121)  216.530 ms  222.878 ms  245.780 ms
 5  hu-0-16-0-2-ar01.hayward.ca.sfba.comcast.net (68.85.57.81)  248.319 ms hu-0-16-0-3-ar01.hayward.ca.sfba.comcast.net (68.85.57.85)  253.118 ms hu-0-16-0-2-ar01.hayward.ca.sfba.comcast.net (68.85.57.81)  257.612 ms
 6  lag-14.ear2.SanJose1.Level3.net (4.68.72.101)  262.847 ms  35.267 ms  32.159 ms
 7  ae-129-3515.edge6.London1.Level3.net (4.69.166.73)  195.280 ms  199.156 ms  206.506 ms
 8  IDNET.edge6.London1.Level3.net (212.113.9.66)  212.845 ms  215.623 ms  220.960 ms
 9  redbus-gw6-gi3-18.idnet.net (212.69.63.9)  218.548 ms  221.657 ms  227.426 ms
10  mythic-gw1.idnet.net (212.69.63.205)  230.013 ms  227.836 ms  237.149 ms  
11  93.93.128.211 (93.93.128.211)  275.356 ms  290.946 ms  292.222 ms  

So now, it was clear that it was trying to enforce IPV6 dns resolution so the solution was to enforce IPV4. Thanks to this thread, using this command it worked like a charm

# apt-get -o Acquire::ForceIPv4=true upgrade