Sometimes we upgrade packages accidentally which is not to update. It also happens during the full update or automatic packages upgrade process. This creates serious issues on the server. For example, You need to exclude kernel, PHP, MySQL and Apache packages from getting updated while updating system via apt update. This tutorial will help you find the answer of how to exclude packages from Apt-get update? How to use exclude package in Ubuntu? How to prevent packages from auto update using apt?
Hold or Exclude Packages from Upgrade
Use the following command to exclude or hold specific Debian package from an update. This will turn off the updates of specified packages from an automatic upgrade or manual upgrades.
- Using apt
sudo apt-mark hold package_name
- Using dpkg
echo "package_name hold" | sudo dpkg --set-selections
- Using aptitude
sudo aptitude hold package_name
Change the package_name with your actual package name to be set on hold.
List Packages on Hold
You can check the packages on hold anytime. Use the following command to view all packages already set to hold.
sudo dpkg --get-selections | grep "hold"
Unhold or Enable Package Upgrade
In future, If you need to upgrade any package set on hold. You need to unhold any package before an upgrade. Use one of the following commands to unhold any package.
- Using apt
sudo apt-mark unhold package_name
- Using dpkg
echo "package_name install" | sudo dpkg --set-selections
- Using aptitude
sudo aptitude unhold package_name
Now, the unhold packages will be upgraded with apt upgrade or unattended upgrades.
Conclusion
In this tutorial, you have learned to hold packages from unnecessary upgrades. You can remove hold any time from package to allow upgrade again.
4 Comments
Hi Rahul,
In my experience, in Ubuntu 16.04 LTS, with kernel 4.15.0-120, if you want to ‘hold’ the kernel it in that version, but if you still have installed the packages:
linux-image-generic
linux-headers-generic
linux-image-generic-hwe-16.04
linux-headers-generic-hwe-16.04
It will NOT work. Took me a while to figure it out, since apt-mark, aptitude hold, and dpkg –set-selections methods were not preventing the install of new version of the Linux kernel.
Basically, remove these packages. They do not provide the kernel, so you won’t break the system.
They are a placeholder for all kernel packages versions, so the apt system knows that it has to install (instead of update the package as it would be with all other packages — kernel is a special package in this concern!) a new package in order for the kernel to be updated.
This worked for me:
apt remove linux-image-generic linux-headers-generic linux-image-generic-hwe-16.04 linux-headers-generic-hwe-16.04
I hope this helps someone else 🙂
In the section: “Unhold or Include Package in Install”, the unhold command for aptitude is slightly wrong. Should be unhold not hold 🙂
Thanks Jack, I have corrected the command.
Thanks for the post!