Ubuntu – How to recursively list dependencies of a package that need to be installed

aptcommand linepackage-management

When doing apt install, I would get a list of packages that need to be installed, under the line:

The following NEW packages will be installed:

and if there are none, it would continue without prompt. I tried to retrieve this list, but would fail if there are none because the install would just proceed. I want to get this list without installing it.

I tried something like this:

apt-cache depends --recurse packagename | grep -v " " | sort -u

But it gives a full list including those that are already installed. I want to limit it to those that need to be installed.

I know I can compare the output above with the result of

dpkg --get-selections

to see which are installed and which are not, but it would involve loops within loops and both lists are quite long. There surely must be a more elegant way to do this.

Thanks for any suggestions. (This is my first time to ask a question here)

Edit: I checked out the method using rdepends as given in this question:
Recursive dependencies

It would have been what I am looking for, except that it results in a different list from what apt install gives. It even lists dependencies that are uninstallable (which can't be, given that the package itself installs successfully). What I'm looking for is the list of packages that apt would install before the given package. Anyway, I don't understand why the list is different. It should be the same result, right? But since it isn't, then it is not what I am looking for. I would appreciate, however, if someone can explain to me why they give different results.

Best Answer

Use: apt -s install ...

Passing the -s option to apt causes it to simulate installation but not actually install or modify anything. That shows what you can expect to see from apt when you install the package, including which packages if any are pulled in to satisfy its (direct and indirect) dependencies.

For example, to find out what will happen when you install the apache2 package, you would run:

apt -s install apache2

That shows you what steps would be taken by sudo apt install apache2.

The -s option can also be spelled as any of --simulate, --just-print, --dry-run, --recon, --no-act. So if you see (or write) any of those, they're doing the same thing.

The -s option is documented in man apt-get and not in man apt (the latter of which doesn't document most features and options), but both apt-get -s install ... and apt -s install ... are supported.

You can likewise simulate other actions with -s, such as the remove action.

Unlike with apt commands that actually make changes to your system, running apt -s as root is optional, so you can omit sudo.