How to Exclude Specific Package from apt-get Upgrade

When working on some projects, sometimes you need the stability of your environment. Updating a package can cause the entire project to fail. Sometimes, we accidentally upgrade those packages which and this creates a serious issue on the server. This is why it can be important to maintain a specific version of a package. This article will show you how you can exclude a specific package from the upgrade on Ubuntu 16.04

Read alsoHow to Install Specific Version of Package using apt-get

1) Exclude packages with some commands

It exists some various methods to exclude or hold a specific package during the full upgrade of the system. It is possible to mark a package as held back. In this article, we will use the virtualbox package for our examples. You can see the installed version

# dpkg -l | grep virtualbox
ii virtualbox 5.0.18-dfsg-2build1 amd64 x86 virtualization solution - base binaries

Now you can see that we have an updated version available

# apt list --upgradable
Listing... Done
virtualbox/xenial-updates 5.0.40-dfsg-0ubuntu1.16.04.2 amd64 [upgradable from: 5.0.18-dfsg-2build1]

You can mark packages using the commands below

apt-mark

You can use the apt-mark command followed by the hold option. This will prevent the package from being automatically installed, upgraded or removed. The syntax is as below

apt-mark hold package

You can use it practically as below

# apt-mark hold virtualbox virtualbox set on hold.

Now you can check as below

# apt upgrade
Reading package lists... Done
Building dependency tree Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back: virtualbox
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

You can see that you can not upgrade the package.

To unmark, use the syntax below

apt-mark unhold package

Practically you have the command below

# apt-mark unhold virtualbox
Canceled hold on virtualbox.

dpkg

You can also use the dpkg --set-selection command to mark package.The syntax is as below

# echo " hold" | dpkg --set-selections

Practically, you can do it as below

# echo "virtualbox hold" | dpkg --set-selections

You don’t have any return which shows the result. So, check directly with an upgrade

# apt upgrade
Reading package lists... Done
Building dependency tree Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back: virtualbox
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

You can see that it works.

Now to unhold, the command is different and respects the syntax

echo "package install" | sudo dpkg --set-selections

so, use the command

# echo "virtualbox install" | dpkg --set-selections

You will not have a return. Because we are already root user, we don’t use the sudo

aptitude

You can also use the aptitude command. By default, this command is not present on your system so you need to install it as below

# apt install aptitude
Reading package lists... Done
Building dependency tree Reading state information... Done
The following additional packages will be installed: aptitude-common libcwidget3v5

To mark a package with the command, use the syntax

aptitude hold package

When you mark a package with aptitude, you don’t have a return.

# aptitude hold virtualbox
No packages will be installed, upgraded, or removed.
0 packages upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
Need to get 0 B of archives. After unpacking 0 B will be used.

If you check the result with an aptitude upgrade, you will not have the mention of a marked package but you will have it if you try apt upgrade.

You can unhold with the syntax

aptitude unhold package

just as below

# aptitude unhold virtualbox
No packages will be installed, upgraded, or removed.
0 packages upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

dselect

You can use the dselect command which is a front-end to dpkg that is used to manage software packages in Debian and Debian-based Linux distributions. It is not present by default so you need to install it

# apt install dselect

Launch the command

Now read the help carefully.

Now you can use the space keyboard to exit the help and find the package to hold. You can use /to make a research

Now you can hold the package with the H or =

now you can exit with Q. It will open the first menu, choose Quit. You can check by upgrading

# apt upgrade
Reading package lists... Done
Building dependency tree Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back: virtualbox
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

You can see that it works.

If you want to unmark the package, you will need to use the + instead of H or = used to hold to have three stars on the result as below

2) Block package using apt configuration files

One way to mark package is to edit the /etc/apt/preferences file. To don’t upgrade a package while upgrading the whole system, we must set an apt pin priority less than zero (0). if the file doesn’t exist, you must create it. You need to respect the syntax below

Package:  ('*' for all packages)
Pin: release * (o=Ubuntu for Ubuntu origin)
Pin-Priority: 

On the Release Pin line, you can add the originator of the packages and for the Pin-Priority numbers, a higher number means more preference and -1 equals to ignore that package. There are some other options which can be used.

To block the virtualbox package, edit the file as below

Package: virtualbox
Pin: release o=Ubuntu
Pin-Priority: -1

save and exit. It is all you need to block the package. To unhold the package you can comment the lines of the file or simply delete the file.

You can see that it is possible to use some methods to block a package. This can be useful to maintain a stable version of your project. You can choose your preferred method. You don’t need to used two or more methods for the same goal.