Well that’s two hours of my life I’m never getting back. You know how sometimes you try to do a simple five-minute job and the damn thing just snowballs…? Well that was today.
First a quick summary so you know what this is about – I was having problems with apt-get stalling when trying to use it on a Raspberry Pi. SPOILER ALERT: It was IPv6 causing the problem.
So here’s how today went. I’ve decided to use my PiDP box – a PDP-8i replica that runs on a Raspberry Pi – as a Mosquitto broker for home automation projects. Simple: install Mosquitto (just ‘apt-get install mosquitto’ and the job’s done) and, while I’m at it, do a system update because I haven’t used this machine in a while (‘apt-get update && apt-get -y upgrade’).
Fate had other ideas. I’ve never had problems with apt-get before. But when I typed apt-get update, apt-get install or apt-get upgrade the process would start and then hang. With update, for example, the first item would stall at 0%, sometimes for minutes. If I was lucky, it would then burst back into life, but would often hang again before completing. Same deal with upgrade.
What do you do in such circumstances? Why, head over to raspberrypi.stackexchange.com of course. Now, don’t get me wrong, I’m a huge fan of StackExchange and StackOverflow. (I’ve heard that, these days, a ‘developer’ is defined as someone capable of cutting and pasting from Stack Overflow. Cruel but fair.) But I instantly got one of those respondents. I swear there are people who do nothing but hang out on these boards waiting for people to post so they can respond with pompous, irrelevant and entirely unhelpful comments. As usual, this idiot castigated me for not providing enough information (a standard response and untrue in this instance) and for running my Raspberry Pi as root (we’ll save that argument for another day, but let’s just say that it was irrelevant in this instance.)
It was obvious I couldn’t wait around for some StackExchange respondent to cure my problem. In the meantime, I’d discovered that there’s a whole new build – or at least configuration – of Raspbian just for the PiDP. ‘Oh well,’ I thought, ‘time to install a new OS’. Which I did, along with all the configuration I like to do of Raspberry Pi machines on my network.
Only to find it had the exact same issue with apt-get. Gah! Back to Google…
Eventually I found it – apparently, apt-get can have issues with IPv6. I’m not entirely clear what the issue is, and why this has just started happening on a machine that never had this problem before is anybody’s guess. But in case anyone else has this issue, here’s the fix.
You can disable IPv6 entirely on the machine. I’m told, but cannot confirm, that adding the following lines to /etc/sysctl.conf will do the job.
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
Like I said, I can’t confirm this because I haven’t tried it. (And if you do, you might also want to comment out any IPv6 references in /etc/hosts.)
A less radical way is to just force apt-get to use IPv4. You can do this with each command with (for example):
sudo apt-get -o Acquire::ForceIPv4=true update
sudo apt-get -o Acquire::ForceIPv4=true upgrade
sudo apt-get -o Acquire::ForceIPv4=true install mosquitto
It’s a lot of typing, though, and easy to get wrong. So an easier way is to make the change persistent. To do this, create a file /etc/apt/apt.conf.d/99force-ipv4 containing the text:
You could do this using Nano or some other editor (as root), but a better way, as a normal user, is to use the following on the command line.
echo ‘Acquire::ForceIPv4 “true”;’ | sudo tee /etc/apt/apt.conf.d/99force-ipv4
From then on, apt-get will always use IPv4 and you won’t be at the mercy of idiots on StackExchange.
And now, back to my life…