How to install only the dependencies of a package?

Solution 1:

This will install all packages in the package's Depends and PreDepends field:

sudo apt-get install $(apt-cache depends <PACKAGE> | grep Depends | sed "s/.*ends:\ //" | tr '\n' ' ')

Basically you ask for all dependencies, filter out the (Pre)Depends, and format that output for apt-get.

One problem are dependencies like

Depends: pulseaudio
  pulseaudio:i386

or virtual packages like

Depends: <java6-runtime-headless>
  default-jre-headless
  openjdk-6-jre-headless

So: use with care - it doesn't work in all cases!

Solution 2:

If you don't mind copy/past, just simulate an apt-get install with -s. That way you will see which other packages will get installed and/or upgrade, then you just remove the package name you don't want to install from that list and voila.

sudo apt-get install -s <package>

Solution 3:

apt-get build-dep <package> will do the trick.

Solution 4:

To list all dependencies of a given package not being installed, you could use aptitude

aptitude search '!~i?reverse-depends("^PACKAGE_NAME$")'

To install the dependencies

aptitude search '!~i?reverse-depends("^PACKAGE_NAME$")' -F "%p" | xargs sudo apt-get install

Examples

  • List the dependencies

    % aptitude search '!~i?reverse-depends("^mc$")'
    p    mc-data         - Midnight Commander - a powerful file manager -- data files
    
  • Show only the package name

    % aptitude search '!~i?reverse-depends("^mc$")' -F "%p"
    mc-data                            
    
  • Install the dependencies for, e.g. mc

    % aptitude search '!~i?reverse-depends("^mc$")' -F "%p" | xargs sudo apt-get install     
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    The following NEW packages will be installed:
      mc-data
    0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
    Need to get 1.166 kB of archives.
    After this operation, 5.550 kB of additional disk space will be used.
    Get:1 http://archive.ubuntu.com/ubuntu/ wily/universe mc-data all 3:4.8.13-3 [1.166 kB]
    Fetched 1.166 kB in 0s (1.250 kB/s)
    Selecting previously unselected package mc-data.
    (Reading database ... 606748 files and directories currently installed.)
    Preparing to unpack .../mc-data_3%3a4.8.13-3_all.deb ...
    Unpacking mc-data (3:4.8.13-3) ...
    Processing triggers for doc-base (0.10.6) ...
    Processing 1 added doc-base file...
    Registering documents with scrollkeeper...
    Processing triggers for man-db (2.7.4-1) ...
    Processing triggers for hicolor-icon-theme (0.15-0ubuntu1) ...
    Setting up mc-data (3:4.8.13-3) ...