If I have to two repositories listed in my sources.list or sources.list.d is there a way to tell apt which is preferred?

My exact use case is if a package is found on the intranet repository and on the remote repository I want apt to always use the intranet one. Right now it seems to pick randomly between the two.


You can do it by modifying your /etc/apt/preferences file; it's commonly referred to as "APT pinning". Here are some guides with details:

  • 1
    I read the manpage for apt_preferences. It has this line: "The APT preferences file does not affect the choice of instance, only the choice of version." It looks like APT preferences wont solve my specific use case. – deft_code Oct 26 '10 at 17:14
  • I think apt-pinning is Debian-only due to the fact that Ubuntu doesn't differn between stable, testing, unstable... maybe for Multimedia or so... bit normally the versions should be never... – wishi Oct 26 '10 at 22:08

It should be enough to list the intranet source first, in sources.list. According to the manual:

It is important to list sources in order of preference, with the most preferred source listed first. Typically this will result in sorting by speed from fastest to slowest (CD-ROM followed by hosts on a local network, followed by distant Internet hosts, for example).

From a cursory look at the source, sources.list is traversed first, then the files in sources.list.d in an order that you can't control.

  • That's a bummer. I wanted to distribute a package that dropped the intranet repo info in sources.list.d. But it looks like I'll have to edit the sources.list in the post-install, yuck. – deft_code Oct 26 '10 at 23:19

Assuming your package repository is at repo.company.net, creating a file called e.g. /etc/apt/preferences.d/company-pinning containing the following will do the trick:

Package: *
Pin: origin repo.company.net
Pin-Priority: 600

The default priority is 500, so this will prefer the latest version from the intranet server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.