Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Summary

How can I make apt-get prefer local repository packages instead of downloading again the same package version from official repositories? The local repository is signed and trusted. We've already tried apt preferences priority.


Original question was : "How to assign highest priority to local repository without using sources.list file"

I have setup a local repository with packages I downloaded. I use it to avoid downloading the same packages over the Internet, when I need to reinstall my Ubuntu. It is a basic repository, created with apt-ftparchive packages . > Packages. I made this a trusted repository to avoid "unauthenticated repository" warning. (When you have a untrusted repository, apt or synaptic try to download the same packages over the Internet, 'cause it is trusted).

I have been using this local repository for at least 1 years. But I have to always put my local repository line at the top of the sources.list file to use this. But this is annoying, since I must open a terminal and do some typing on it every time I reinstall Ubuntu, though there is a better tool software-properties-gtk. I cannot use this tool since it place the source line at the end of `sources.list.

And the real problem is that, the apt or synaptic always download a package from the source which is mentioned earlier, without inspecting whether the packages are already available in the local repository. So, I have no choice but to place the local source at the top of sources.list doing terminal (I actually don't hate terminal, but I need a solution) .

I have tried this method. But this does not help me.

My preference file is this in /etc/apt/preferences.d/local-pin-900

    Package: *
    Pin: release o=Local,n=ubuntu-local
    Pin-Priority: 900

My release file is this

    Origin: Local
    Label: Local-Ubuntu
    Description: Local Ubuntu Repository
    Codename: ubuntu-local
    MD5Sum:
     ed43222856d18f389c637ac3d7dd6f85          1043412 Packages
     d41d8cd98f00b204e9800998ecf8427e                0 Sources

When I enable the apt-preference, the apt-cache policy correctly shows the preference, e.g. It shows the local repository has the highest priority.

But when I do this sudo apt-get install <package-name>, apt tries to download it from Internet. But when I place my local-repo at the top, it installs from local repository.

So, My question is -> 'Is it possible to force apt to use local repository when the package is available in local repository, without explicitly placing "the local source" at the top of my repository list (e.g sources.list file) ?'

Edit: output of apt-cache policy $package_name is as follows

  
nautilus-wipe:
  Installed: (none)
  Candidate: 0.1.1-2
  Version table:
     0.1.1-2 0
        500 http://archive.ubuntu.com/ubuntu/ precise/universe i386 Packages
        900 file:/media/Main/Linux-Software/Ubuntu/Precise/  Packages

It is showing that my local repository has higher preference, though it is not the one which comes first in sources.list file.

Here is the output of apt-get install nautilus-wipe

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  nautilus-wipe
0 upgraded, 1 newly installed, 0 to remove and 131 not upgraded.
Need to get 30.7 kB of archives.
After this operation, 150 kB of additional disk space will be used.
'http://archive.ubuntu.com/ubuntu/pool/universe/n/nautilus-wipe/nautilus-wipe_0.1.1-2_i386.deb' nautilus-wipe_0.1.1-2_i386.deb 30730 MD5Sum:7d497b8dfcefe1c0b51a45f3b0466994

It is still trying to get the file from Internet, though I think it should be happy with the local one.


Result of using "" or localhost as mediageek suggested

Using localhost has no effect. but "" has but was not different than my method

    Package: *
    Pin: origin ""
    Pin-Priority: 1001

I have used them before posting the question. But again i tried this (for surity). The same result. apt-cache policy showing that my local repository has indeed the highest priority. but when i tried to install it is saying "Need to get XXX/XXX KB of archive".

share|improve this question
    
Was the package in your local repository newer/equal to the official package? –  tumbleweed Jun 5 '12 at 0:50
    
Yes, of course, I verified it with apt-get command. It says "0 kb need to be downloaded". –  Anwar Shah Jun 5 '12 at 3:03
    
You want to verify it with apt-cache policy –  tumbleweed Jun 5 '12 at 9:56
    
The previous comment was a mistake. I thought i was giving it in my another question (regarding installing software with software center from local repository). Yes i have already verified it with apt-cache policy and saw that local repository has higher priority. But still apt-get trying to install it from internet. I an thinking that it is simply not possible without using sources.list file. –  Anwar Shah Jun 5 '12 at 13:23
    
I was asking if apt-cache policy showed that the version was the same –  tumbleweed Jun 5 '12 at 23:08

1 Answer 1

up vote 10 down vote accepted
+100

The short answer:

The APT preferences file /etc/apt/preferences can be used to control which versions of packages will be selected for installation.

Here is the file /etc/apt/preferences, which apt-get use to decide, which version it's have to select if there are many versions of a single packge. With this, any package that comes from the local repository, will take precedence over the other packages.

Package: *  
Pin: origin ""  
Pin-Priority: 1001  

This won't control the version, but the source preference if both packages has the same version. If you need to assign highest priority for the same package version in your local repo, list them at the top to the /etc/apt/sources.list file>

Answer to the original question:

How to assign highest priority to local repository without using sources.list file?

There is no way to assign highest priority to local repository without using sources.list file. you must put them in top of "sources.list" if you want to assign highest priority to your local repo.

Summary answer to this question:

How can I make apt-get prefer local repository packages instead of downloading again the same package version from official repositories? The local repository is signed and trusted. We've already tried apt preferences priority.

Using the /etc/apt/sources.list file, You need to put your local repository line at the top of this file. like the below one.

deb cdrom:[Kubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120424)]/ precise main restricted
deb file:/media/Main/Precise-Debs-New /
deb file:/media/Main/Linux-Software/Ubuntu/Precise /

deb http://archive.ubuntu.com/ubuntu precise main universe restricted multiverse
deb-src http://archive.ubuntu.com/ubuntu precise universe main multiverse restricted #Added by software-properties

# deb cdrom:[Xubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120423.1)]/ precise main multiverse restricted universe
# deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120423)]/ precise main restricted

# deb http://archive.canonical.com/ubuntu precise partner
# deb-src http://archive.canonical.com/ubuntu precise partner
# deb http://extras.ubuntu.com/ubuntu precise main
# deb-src http://extras.ubuntu.com/ubuntu precise main

And yes, "apt_preference" has nothing to do with selecting instances of the packages with same version. The point here is, you can't use /etc/apt/preference file to force to select/use a .deb file from your local repository by assigning highest priority to your local one. That file(preference) is used to select an specific version, not a specific instance (.deb file) of the same version. For the latter case, you need to use /etc/apt/sources.list file with above mentioned method.


Explanation:

Seems we all misunderstood the apt_preference purpose , the apt_preference manual describes this in the first 3 paragraph!!

The APT preferences file /etc/apt/preferences can be used to control which versions of packages will be selected for installation.

Several versions of a package may be available for installation when the sources.list(5) file contains references to more than one distribution (for example, stable and testing). APT assigns a priority to each version that is available. Subject to dependency constraints, apt-get(8) selects the version with the highest priority for installation. The APT preferences file overrides the priorities that APT assigns to package versions by default, thus giving the user control over which one is selected for installation.

And the very important one is in third paragraph

Several instances of the same version of a package may be available when the sources.list(5) file contains references to more than one source. In this case apt-get(8) downloads the instance listed earliest in the sources.list(5) file. The APT preferences file does not affect the choice of instance, only the choice of version.

Those Italics were made by mine

Let's see this with an example:

I have precise's version of compiz-fusion-plugins-extra, but I disabled that for demonstration

Here is the file /etc/apt/preferences, which apt-get use to decide, which version it's have to select if there are many versions of a single packge. (In this example, compiz-fusion-plugins-extra has several versions.

Package: *  
Pin: origin ""  
Pin-Priority: 1001  

This file says apt-get to install from local repository for all packages which have several versions exists in different repository.

In other words, if I have package xapps with version 1.0 in my local repository, and this same package xapps exists in Precise's repository with higher version, say 2.0, apt-get will prefer the version from local repository, i.e version 1.0.

Here is my /etc/apt/sources.list file

deb http://archive.ubuntu.com/ubuntu precise main universe restricted multiverse
deb-src http://archive.ubuntu.com/ubuntu precise universe main multiverse restricted #Added by software-properties
deb http://security.ubuntu.com/ubuntu/ precise-security universe main multiverse restricted
deb http://archive.ubuntu.com/ubuntu precise-updates universe main multiverse restricted
deb http://archive.ubuntu.com/ubuntu precise-backports universe main multiverse restricted

# deb http://archive.canonical.com/ubuntu precise partner
#deb-src http://archive.canonical.com/ubuntu precise partner
# deb http://extras.ubuntu.com/ubuntu precise main
# deb-src http://extras.ubuntu.com/ubuntu precise main


deb file:/media/Main/Linux-Software/Ubuntu/Oneiric / #this is oneiric repository.
deb file:/media/Main/Linux-Software/Ubuntu/Natty/natty /
deb file:/media/Main/Linux-Software/Ubuntu/Natty/kubuntu /
deb file:/media/Main/Linux-Software/Ubuntu/Natty/natty_new /

Although, I have listed the precise's repository earlier in the file, it does not cause apt-get to select package from that repository. As described in the manual

The APT preferences file does not affect the choice of instance, only the choice of version.

So, the output of the command apt-cache policy compiz-fusion-plugins-extra was as expected as below

anwar@edubuntu-lenovo:/etc/apt$ apt-cache policy compiz-fusion-plugins-extra 
compiz-fusion-plugins-extra:
  Installed: (none)
  Candidate: 0.9.5.94-0ubuntu1  / **selecting lower versions for priority** /
  Version table:
     0.9.7.0~bzr9-0ubuntu6 0
        500 http://archive.ubuntu.com/ubuntu/ precise/universe i386 Packages
     0.9.5.94-0ubuntu1 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Oneiric/  Packages
     0.9.4-0ubuntu3 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages

Here, the candidate version is 0.9.5.94-0ubuntu1, (which is in the local repository), not 0.9.7.0~bzr9-0ubuntu6 0, which is in precises' repository. although the precises' one was listed first in the sources.list file.

An example run of apt-get trying to install compiz-fusion-plugins-extra after setting highest priority to local repository.

anwar@edubuntu-lenovo:~$ sudo apt-get install compiz-fusion-plugins-extra 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  x11-apps x11-xfs-utils xinit x11-session-utils libfs6
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  compiz-core compiz-plugins-default compiz-plugins-extra
  compiz-plugins-main-default libdecoration0 xserver-xorg-core
  xserver-xorg-video-intel
Suggested packages:
  compizconfig-settings-manager xfonts-100dpi xfonts-75dpi
The following packages will be REMOVED:
  compiz compiz-gnome compizconfig-backend-gconf edubuntu-desktop
  libcompizconfig0 ubuntu-desktop unity xorg xserver-xorg
  xserver-xorg-input-all xserver-xorg-input-evdev xserver-xorg-input-mouse
  xserver-xorg-input-synaptics xserver-xorg-input-vmmouse
  xserver-xorg-input-wacom xserver-xorg-video-all xserver-xorg-video-ati
  xserver-xorg-video-cirrus xserver-xorg-video-fbdev xserver-xorg-video-geode
  xserver-xorg-video-mach64 xserver-xorg-video-mga xserver-xorg-video-neomagic
  xserver-xorg-video-nouveau xserver-xorg-video-openchrome
  xserver-xorg-video-qxl xserver-xorg-video-r128 xserver-xorg-video-radeon
  xserver-xorg-video-s3 xserver-xorg-video-savage
  xserver-xorg-video-siliconmotion xserver-xorg-video-sis
  xserver-xorg-video-sisusb xserver-xorg-video-tdfx xserver-xorg-video-trident
  xserver-xorg-video-vesa xserver-xorg-video-vmware
The following NEW packages will be installed:
  compiz-fusion-plugins-extra compiz-plugins-extra
The following packages will be DOWNGRADED:
  compiz-core compiz-plugins-default compiz-plugins-main-default
  libdecoration0 xserver-xorg-core xserver-xorg-video-intel
0 upgraded, 2 newly installed, 6 downgraded, 37 to remove and 698 not upgraded.
Need to get 0 B/5,772 kB of archives.
After this operation, 8,294 kB disk space will be freed.
Do you want to continue [Y/n]? 

Strange!!!, It is trying to downgrade the already installed compiz packages of precise to resolve dependencies (matching with Oneiric's one) with newly selected compiz-fusion-plugins-extra!!!.

Which is more dangerous is , it is trying to remove graphics driver packages (xserver-xorg-....) to avoid conflict with new compizes.

(Probably, If I also had them in my local repository, they may safely want to downgrade. But as those packages are usually installed, i did not download them, so aren't in local repo, hence this result).

Conclusion: So, using /etc/apt/preferences is only recommended to use with local repository, if you have complete repository in local system.

What if several instances (.deb file) of the same version exists in many repository.

Which is described by the apt_preference manual

Several instances of the same version of a package may be available when the sources.list(5) file contains references to more than one source. In this case apt-get(8) downloads the instance listed earliest in the sources.list(5) file.

An example of use cases for /etc/apt/preferences file:

An example of forcing apt-get to select version available from a custom repo, instead of Ubuntu's standard one may be the "Linux Mint" distribution. They use this custom preference file.

Package: *
Pin: release o=linuxmint
Pin-Priority: 700

Package: *
Pin: origin packages.linuxmint.com
Pin-Priority: 700

Package: *
Pin: release o=Ubuntu
Pin-Priority: 500

This effectively causes them, to provide packages when they have those in their repositories. But if they don't have them, they use Ubuntu's standard repository.


An Example running apt-get after setting highest priority to local repository

I just wanted to install kubuntu-desktop package over Ubuntu to test KDE. (I forgot that, i have set a preference using /etc/apt/preference file). I had an iso of the kubuntu-12.04-alternate-installer, so i thought I can use that.

(after properly setting the iso as repository source), I ran
sudo apt-get install kubuntu-desktop, then surprised by seeing the output below.


anwar@edubuntu-lenovo:~$ sudo apt-get install kubuntu-desktop
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 kubuntu-desktop : Depends: language-selector-kde but it is not going to be installed
                   Recommends: amarok but it is not going to be installed
                   Recommends: apturl-kde but it is not going to be installed
                   Recommends: gdebi-kde but it is not going to be installed
                   Recommends: gpgsm but it is not going to be installed
                   Recommends: ibus-qt4 but it is not going to be installed
                   Recommends: jockey-kde but it is not going to be installed
                   Recommends: kdegraphics-strigi-plugins but it is not going to be installed
                   Recommends: kdepim-wizards but it is not going to be installed
                   Recommends: kpackagekit but it is not going to be installed
                   Recommends: libreoffice-kde but it is not going to be installed
                   Recommends: libreoffice-style-oxygen but it is not going to be installed
                   Recommends: network-manager-pptp-kde but it is not going to be installed
                   Recommends: plasma-widget-facebook but it is not going to be installed
                   Recommends: plasma-widget-kimpanel but it is not going to be installed
                   Recommends: usb-creator-kde but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

I was surprised by the result. suddenly i remember that, I have set a preference (for writing this answer) in /etc/apt/preference to set highest priority to my local repository.

so, I ran this: apt-cache policy kubuntu-desktop, the output was as below:

anwar@edubuntu-lenovo:~$ apt-cache policy kubuntu-desktop 
kubuntu-desktop:
  Installed: (none)
  Candidate: 1.222.1kde47~ppa2
  Version table:
     1.254 0
        500 cdrom://Kubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120424)/ precise/main i386 Packages
        500 http://archive.ubuntu.com/ubuntu/ precise/main i386 Packages
     1.222.1kde47~ppa2 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222.1 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/kubuntu/  Packages

In this output, we see that both the "cdrom" and "precise main repository" has version number 1.254, while the local repository has version 1.222.1kde47~ppa2, which is lower version. Although, they are placed before the local ones in sources.list file, local packages got precedence.

To explicitly see that apt-get is indeed trying to install from local one, I ran synaptic and got this, which is showing that, the Apt system consider 1.222.1kde47~ppa2 as the candidate version to install.

enter image description here

Also apt-cache policy kubuntu-desktop showing the same things

anwar@edubuntu-lenovo:~$ apt-cache policy kubuntu-desktop
kubuntu-desktop:
  Installed: (none)
  Candidate: 1.222.1kde47~ppa2
  Version table:
     1.254 0
        500 cdrom://Kubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120424)/ precise/main i386 Packages
        500 http://archive.ubuntu.com/ubuntu/ precise/main i386 Packages
     1.222.1kde47~ppa2 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222.1 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/kubuntu/  Packages

To solve this, I removed the /etc/apt/preference file by sudo rm /etc/apt/preference.

Then ran again this again apt-cache policy kubuntu-desktop. only seeing this expected output.

anwar@edubuntu-lenovo:~$ sudo rm /etc/apt/preferences
anwar@edubuntu-lenovo:~$ sudo apt-cache policy kubuntu-desktop
kubuntu-desktop:
  Installed: (none)
  Candidate: 1.254
  Version table:
     1.254 0
        500 cdrom://Kubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120424)/ precise/main i386 Packages
        500 http://archive.ubuntu.com/ubuntu/ precise/main i386 Packages
     1.222.1kde47~ppa2 0
        500 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222.1 0
        500 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222 0
        500 file:/media/Main/Linux-Software/Ubuntu/Natty/kubuntu/  Packages

Also ran this again. sudo apt-get install kubuntu-desktop to see the output below:

......... Unnecessary details deleted .........

0 upgraded, 213 newly installed, 0 to remove and 150 not upgraded.
Need to get 0 B/89.6 MB of archives.
After this operation, 269 MB of additional disk space will be used.
Do you want to continue [Y/n]? 

You can check the full result here

Then I pretty much sure about the Explanation portion.


Reference 1 : The apt_preference manual

Reference 2: How to Keep a specific version of packages installed

share|improve this answer
1  
+1 for the "What if several instances of the same version" part, explains the behaviour. But I'll just wait until the end of the week, maybe someone has a better explanation. :) –  medigeek Jun 20 '12 at 7:53
    
should I add an the example using the local repo? –  Anwar Shah Jun 20 '12 at 8:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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