当前位置: 首页>>技术教程>>正文


为我的本地存储库分配最高优先级

, , , , ,

问题描述

总结

如何使apt-get更喜欢本地存储库包而不是从官方存储库再次下载相同的包版本?本地存储库已签名并受信任。我们已经尝试过优先选择优先权。


原始问题是:“如何在不使用sources.list文件的情况下为本地存储库分配最高优先级”

我已经使用我下载的软件包设置了一个本地存储库当我需要重新安装我的Ubuntu时,我用它来避免通过Internet下载相同的软件包。它是使用apt-ftparchive packages . > Packages创建的基本存储库。我将其设为可信任的存储库以避免”unauthenticated repository”警告。 (当您有不受信任的存储库时,aptsynaptic尝试通过Internet下载相同的软件包,因为它是可信的)。

我一直在使用这个本地存储库至少1年。但我必须始终将我的本地存储库行放在sources.list文件的顶部以使用它。但这很烦人,因为每次重新安装Ubuntu时我必须打开一个终端并对其进行一些打字,尽管有一个更好的工具software-properties-gtk。我不能使用这个工具,因为它将源代码行放在`sources.list的末尾。

真正的问题是,aptsynaptic始终从前面提到的源代码下载包,而不检查包是否已在本地存储库中可用。所以,我别无选择,只能将本地源放在sources.list做终端的顶部(我实际上并不讨厌终端,但我需要一个解决方案)。

我试过this method。但这对我没有帮助。

我的偏好文件是在/etc/apt/preferences.d/local-pin-900

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

我的发布文件就是这个

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

当我启用apt-preference时,apt-cache policy正确显示首选项,例如它显示本地存储库具有最高优先级。

但是当我这样做sudo apt-get install <package-name>时,apt会尝试从Internet下载它。但是当我将local-repo置于顶部时,它将从本地存储库安装。

所以,我的问题是 – > ‘当包在本地存储库中可用时,是否有可能强制使用本地存储库,而没有明确地将“本地源”放在我的存储库列表的顶部(例如sources.list文件)?

编辑:apt-cache policy $package_name的输出如下

  
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

它显示我的本地存储库具有更高的优先级,尽管它不是sources.list文件中首先存在的存储库。

这是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

它仍然试图从互联网上获取文件,但我认为应该对本地文件感到高兴。


建议使用“”或localhost作为mediageek的结果

使用localhost无效。但“”与我的方法有所不同

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

我在发布问题之前使用过它们。但我又试过这个(为了潮汐)。同样的结果。 apt-cache policy显示我的本地存储库确实具有最高优先级。但当我试图安装时,它说“需要获取XXX /XXX KB的存档”。

最佳解决方法

简短的回答:

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

这是文件/etc/apt/preferencesapt-get用来决定,如果有一个包的许多版本,它必须选择哪个版本。有了这个,来自本地存储库的任何包都将优先于其他包。


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

这不会控制版本,但如果两个包具有相同的版本,则不会控制源首选项。如果您需要为本地仓库中的相同软件包版本分配最高优先级,请将它们列在/etc/apt/sources.list文件的顶部>

回答原始问题:

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

如果不使用sources.list文件,则无法为本地存储库分配最高优先级。如果要为本地仓库分配最高优先级,则必须将它们置于”sources.list”之上。

这个问题的总结答案:

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.

使用/etc/apt/sources.list文件,您需要将本地存储库行放在此文件的顶部。喜欢下面的一个。


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

是的,”apt_preference”与选择具有相同版本的软件包实例无关。关键在于,您不能使用/etc/apt/preference文件强制从本地存储库中选择/使用.deb文件,方法是为本地存储库分配最高优先级。该文件(首选项)用于选择特定版本,而不是同一版本的特定实例(.deb文件)。对于后一种情况,您需要使用上述方法的/etc/apt/sources.list文件。


说明:

似乎我们都误解了apt_preference的目的,apt_preference手册在前3段中描述了这个!

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.

而非常重要的是第三段

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.

那些斜体是我的

让我们看一个例子:

我有compiz-fusion-plugins-extra的精确版本,但我禁用了它用于演示

这是文件/etc/apt/preferencesapt-get用来决定,如果有一个包的许多版本,它必须选择哪个版本。 (在此示例中,compiz-fusion-plugins-extra有多个版本。


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

此文件说apt-get从本地存储库安装所有包含不同存储库中存在多个版本的包。

换句话说,如果我在我的本地存储库中拥有版本1.0的软件包xapps,并且相同的软件包xapps存在于具有更高版本的Precise的存储库中,例如2.0,则apt-get将更喜欢本地存储库中的版本,即版本1.0。

这是我的/etc/apt/sources.list文件


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 /

虽然,我已在文件的前面列出了精确的存储库,但它不会导致apt-get从该存储库中选择包。如手册中所述

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

因此,命令apt-cache policy compiz-fusion-plugins-extra的输出如下所示


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

这里,候选版本是0.9.5.94-0ubuntu1(位于本地存储库中),而不是0.9.7.0~bzr9-0ubuntu6 0,它位于precises的存储库中。虽然在一个sources.list文件中首先列出了一个。

运行apt-get尝试在将最高优先级设置为本地存储库后安装compiz-fusion-plugins-extra的示例。


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]? 

奇怪!!!,它试图降级已经安装的compiz软件包精确解决依赖关系(与Oneiric的匹配)与新选择的compiz-fusion-plugins-extra !!!

更危险的是,它试图删除图形驱动程序包(xserver-xorg -….)以避免与新的compizes冲突。

(也许,如果我也在我的本地存储库中使用它们,他们可能会安全地想要降级。但是由于这些软件包通常是安装的,所以我没有下载它们,因此不在本地存储库中,因此这个结果)。

结论:因此,如果您在本地系统中有完整的存储库,则建议仅使用/etc/apt/preferences与本地存储库一起使用。

如果许多存储库中存在相同版本的多个实例(.deb文件),该怎么办?

这是由apt_preference手册描述的

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.

/etc/apt/preferences文件的用例示例:

强制apt-get选择可从自定义仓库中获得的版本而不是Ubuntu的标准版本的示例可以是”Linux Mint”分布。他们使用此自定义preference文件。


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

这有效地使他们在他们的存储库中拥有包时提供包。但如果他们没有,他们会使用Ubuntu的标准存储库。


在将最高优先级设置为本地存储库后运行apt-get的示例

我只想在Ubuntu上安装kubuntu-desktop软件包来测试KDE。 (我忘了,我已经使用/etc/apt/preference文件设置了首选项)。我有一个kubuntu-12.04-alternate-installer的iso,所以我想我可以使用它。

(在正确设置iso作为存储库源之后),我运行了sudo apt-get install kubuntu-desktop,然后看到下面的输出感到惊讶。



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.

我对结果感到惊讶。突然我记得,我已经在/etc/apt/preference中设置了一个首选项(用于编写此答案),以便为我的本地存储库设置最高优先级。

所以,我跑了这个:apt-cache policy kubuntu-desktop,输出如下:


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

在此输出中,我们看到”cdrom”和“精确主存储库”的版本号都是1.254,而本地存储库的版本是1.222.1kde47~ppa2,它是较低版本。虽然它们位于sources.list文件中的本地文件之前,但本地包具有优先权。

为了明确地看到apt-get确实尝试从本地安装,我运行了synaptic并得到了这个,这表明,Apt系统认为1.222.1kde47~ppa2是要安装的候选版本。

apt,synaptic,software-sources,aptitude,localrepository,ubuntu

还有apt-cache policy kubuntu-desktop显示相同的东西


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

为解决此问题,我通过sudo rm /etc/apt/preference删除了/etc/apt/preference文件。

然后再次再次运行apt-cache policy kubuntu-desktop。只看到这个预期的输出。


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

还跑了这个。 sudo apt-get install kubuntu-desktop查看下面的输出:


......... 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]? 

您可以检查完整的结果here

然后我非常确定解释部分。


Reference 1 : The apt_preference manual

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

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/2932.html,未经允许,请勿转载。