16

On my work network any apt-get (or aptitude) commands take a very long time, it's look's like admins blocked some port for it (for unknown reason). For example

sudo apt-get update

take like 2 days and all I get - a very long list of responses like

Get: 36 http://security.ubuntu.com precise-security/universe amd64 Packages [11.6 kB]
Get: 37 http://security.ubuntu.com precise-security/universe amd64 Packages [11.6 kB]
Get: 38 http://security.ubuntu.com precise-security/universe amd64 Packages [11.6 kB]
Get: 39 http://security.ubuntu.com precise-security/universe amd64 Packages [11.6 kB]
Get: 40 http://security.ubuntu.com precise-security/universe amd64 Packages [11.6 kB]

Same situation then I try to download software

Get:1 http://archive.ubuntu.com/ubuntu/ precise/main dash i386 0.5.7-2ubuntu2 [85.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ precise/main dash i386 0.5.7-2ubuntu2 [85.8 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ precise/main dash i386 0.5.7-2ubuntu2 [85.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ precise/main dash i386 0.5.7-2ubuntu2 [85.8 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ precise/main dash i386 0.5.7-2ubuntu2 [85.8 kB]

Is there something I can do to change port for apt-get or something else

Braiam
  • 67,791
  • 32
  • 179
  • 269

6 Answers6

14

My ubuntu servers were always very fast at apt-get updates and upgrades. Typically the lines on the screen went too fast to be seen. Then all of a sudden they slowed down to a crawl overnight.

It was IPv6 doing it.

I found the following solution:

sudo nano /etc/gai.conf

Uncomment the following line:

precedence ::ffff:0:0/96 100

For me it was line 54 (make sure you don't uncomment the one that ends in 10).

Save and exit.

sudo nano /etc/apt/apt.conf.d/99force-ipv4 # (this is to make a new file)

Add the following line to the new file:

Acquire::ForceIPv4 false;

Save and exit.

Ryan
  • 241
  • 2
  • 7
  • 4
    I'm not a fan of these responses as there is no actual explanation as to why this is a solution. – Gray Adams Aug 01 '17 at 20:47
  • 2
    @GrayAdams If you have an explanation you can add it. I have none. Wish I did :( I just knew what worked for me so I posted it for others to fix the problem, at least temporarily. – Ryan Aug 08 '17 at 21:14
  • 1
    It's Acquire::ForceIPv4 false; - add the 'c' into the first word to correct. – volvox Jul 09 '23 at 20:32
  • @volvox nice catch. Took 7+ years for anyone to catch that lol. – Ryan Sep 02 '23 at 20:07
5
sudo nano /etc/gai.conf

add / uncomment these lines:

reload yes
precedence ::ffff:0:0/96 100
precedence ::/0 10

when I updated with above lines my /etc/gai.conf it solved , and it used ipv4 directly instead of ipv6 and installed quickly.

swift
  • 3,281
  • 2
  • 23
  • 46
  • YES!!!!! Thank you!!! I had no idea what the f was happening here. Had to wait 10 minutes for each apt-get install. – Jacko May 06 '17 at 12:51
2

The default config uses Ipv6 to establish connections and upgrade the packages and IPv4 is disabled by default. This solution enables precedence for IPv4 addresses by enabling Ipv4. It did not help to execute it in seconds but did speed up the process a little bit as expected.

This is because parsing either IPv4 or IPV6 should not take way much time even if done with each package. It was IPv6 doing it.

sudo nano /etc/gai.conf

Uncomment the following line:

precedence ::ffff:0:0/96 100

This may be a different line for you. Make sure to select the one with /96 rule as seen above. This is CIDR representation for IPv4 addresses. it enables precedence for the same. By default it is commented out.

Save and exit.

sudo nano /etc/apt/apt.conf.d/99force-ipv4 # (this is to make a new file)

This creates a new file and when the config in next line is added it sets the config to false allowing IPV4 traffic.

Add the following line to the new file:

Aquire::ForceIPv4 false;

Save and exit.

  • What is this answer if not a different version of the top answer? It improves it by adding explanations as to what might be the cause, but I don't understand why this hasn't been done through an edit of the most upvoted answer, which is a couple of years old. – Daniel F Mar 09 '22 at 12:54
1

I suggest you begin by measuring your typical Internet/network throughput using one of the performance checking sites. One possibility is www.speedtest.net. You can also use a simple Google search to look for other sites you could use to test your Internet connection speed.

If testing shows that the problem is not a limitation of your Internet connection, then the next step might be to take a look at the answers to this question:
How can I get apt to use a mirror close to me, or choose a faster mirror?

I suggest you start by trying the most popular answer to that question. It boils down to adding the two lines below to the top of /etc/apt/sources.list. This is supposed to encourage the update process to automatically pick a download mirror with better performance.

deb mirror://mirrors.ubuntu.com/mirrors.txt precise main restricted universe multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt precise-updates main restricted universe multiverse
  • Note: The lines above are meant to used with Ubuntu 12.04 (Precise Pangolin). For earlier releases replace precise in the two lines above with the corresponding codename of the release you are using.

    Lucid (10.04), Maverick (10.10), Natty (11.04), Oneiric (11.10)

If this approach does not help or is simply not something you want to do then you might also consider letting Update Manager attempt to pick a "fastest" mirror. This is described in more detail in the other answers to the question I provided a link to above.

  • My Internet works fine. I tried a lot of mirrors - but result the same, I can't download anything from sites – ShockwaveNN Jun 13 '12 at 07:59
  • @ShockwaveNN If your Internet is working fine then you should have no problem downloading from most of the mirrors. There is nothing significantly different in terms of the Internet which happens during, say, sudo apt-get update and using the firefox browser. Have you tried booting from a Live CD/USB and trying sudo apt-get update to see if there is a difference? If there is it would point at something being wrong with your Ubuntu install. If you get the same result, then I have no other ideas at the moment. – irrational John Jun 13 '12 at 16:26
0

Run

sudo apt-get clean

before running

sudo apt-get update
0

First, 2 facts:
apt-get uses http (port 80) by default in standard linux distros.

apt-get generally downloads ~20MB of data to update the lists. If you just ran apt-get update and run it again it downloads ~100KB of data only.
So if you are able to browse the world wide web (i.e. websites using browser) and you are using standard distros (not customized to use any other port), nothing has been blocked by the admin which is stopping you from running this command.
The problem might be that requests from your machine to current servers being used for updates by apt-get are facing some issues (i.e. it might be just a download issue).
To solve your problem, my suggestions(as you are using ubuntu):

Open update-manager -> click on settings at the bottom left -> click on first tab ubuntu software -> click on the drop down box next to Download from option -> change the server to some place which is nearer to your location by choosing other in the list. Also check the protocol being used for updates. It should be http for general users. -> Now run sudo apt-get update or use update manager to check for updates. Now things should work fine for you. If things don't work even after following this, comment here.


Since it din't help you, it seems to be the problem of obsolete or partial files collected while trying to update some time back.
I would suggest you to run this commands in terminal:

sudo mv /var/cache/apt/archives/partial{,bakjune9}
sudo mv /var/lib/apt/lists{,bakjune9}

These commands will rename the directories where files are stored when apt-get downloads them while updating and upgrading. (You can rename them back if something goes wrong. So no risk of making situation worse than present)
After that run:

sudo apt-get update 

It will download something like 20MB of data.
If it runs successfully, your problem is solved.

Now you should be able to run sudo apt-get upgrade to install the updates.
If it does not, comment here back.

drake01
  • 3,457
  • This didn't help, I change server to another country and check, that protocol was 'http' but I have same problem. Also I can browse Internet (except some sites like youtube.com), and can go to http://archive.ubuntu.com/ubuntu/ in my browser and it's work fine – ShockwaveNN Jun 09 '12 at 07:40
  • I added next steps which I feel should help you. See if it works. – drake01 Jun 09 '12 at 09:09
  • Second variant didn't work either. I'm strongly think, that this is some problem with connection, any ways to diagnose my connection for problems? – ShockwaveNN Jun 09 '12 at 09:27
  • Have you added a lot of software sources other than main repositories? If you have a few dozen ppa's that are down or abandoned or really slow, that can make an update slow. It should also give you an error message every time it can't connect to one. – Marc Dec 15 '13 at 14:32