When administering Debian/Ubuntu systems, we often encounter situations where we need just the dependencies of a package without installing the package itself. The common workaround of installing then removing the package is inefficient and can sometimes leave residual configuration files.
The most precise method involves using apt-rdepends
to recursively list dependencies, then installing them directly:
sudo apt install apt-rdepends
apt-rdepends --state-follow=Installed --state-show=NotInstalled package-name | grep -v "^ " | xargs sudo apt install
For systems without apt-rdepends, you can use apt-cache with a bit of shell magic:
sudo apt install $(apt-cache depends --recurse --no-recommends --no-suggests \
--no-conflicts --no-breaks --no-replaces --no-enhances \
package-name | grep "^\w" | sort -u)
Let's say we need dependencies for python3-dev development headers:
# Method 1:
apt-rdepends --state-follow=Installed --state-show=NotInstalled python3-dev | grep -v "^ " | xargs sudo apt install
# Method 2:
sudo apt install $(apt-cache depends --recurse --no-recommends python3-dev | grep "^\w" | sort -u)
By default, these commands ignore recommended packages. To include them:
apt-rdepends --state-follow=Installed --state-show=NotInstalled --follow=recommends package-name | grep -v "^ " | xargs sudo apt install
For frequent use, create a shell function in your .bashrc:
install_deps() {
if ! command -v apt-rdepends &> /dev/null; then
sudo apt install apt-rdepends
fi
apt-rdepends --state-follow=Installed --state-show=NotInstalled "$1" |
grep -v "^ " |
xargs sudo apt install
}
After installation, verify what was actually installed:
apt-mark showmanual | grep -F -f <(apt-rdepends --state-follow=Installed --state-show=NotInstalled package-name | grep -v "^ ")
If you want to remove these dependencies later:
sudo apt autoremove --purge $(apt-rdepends --state-follow=Installed package-name | grep -v "^ ")
When you run apt install package-name
, APT automatically installs both the package and all its recommended/suggested dependencies. But what if you only need the required dependencies? Maybe you're:
- Building a minimal container image
- Debugging dependency chains
- Creating a custom package configuration
First, install the tool that shows reverse dependencies:
sudo apt install apt-rdepends
Then use this one-liner to install only mandatory dependencies:
sudo apt install $(apt-rdepends -d --follow=Depends package-name | grep -v "^ ")
Let's examine what each part does:
apt-rdepends -d --follow=Depends package-name # Lists all dependencies
grep -v "^ " # Removes package-name itself
$(...) # Feeds results to apt install
To install only Nginx's essential dependencies without Nginx itself:
sudo apt install $(apt-rdepends -d --follow=Depends nginx | grep -v "^ ")
This will install packages like:
- libc6
- libpcre3
- zlib1g
- libssl3
For systems without apt-rdepends, try:
sudo apt install $(apt-cache depends --recurse --no-recommends --no-suggests \
--no-conflicts --no-breaks --no-replaces --no-enhances \
package-name | grep "^\w" | sort -u)
- Always test in a non-production environment first
- Some packages may have circular dependencies
- The methods show runtime dependencies only
- For build dependencies, use
apt build-dep
After installation, check what was actually installed:
apt-mark showmanual | grep -v "package-name"