59

Running sudo apt-get install <PACKAGE> will install the package, its dependencies, and any other recommended packages.

However, there does not seem to be a way to install only the dependencies of a package and exclude the package itself.

How would one go about doing this?

Nathan Osman
  • 32,155
  • Just curious, why do you want to do this? – Kris Harper Nov 01 '11 at 18:01
  • @root45: Well it's a bit of a unique situation: I have the source code to a Python package in the repos. and it isn't starting. I need to install the dependencies for the package without installing the package itself (since I already have the code). – Nathan Osman Nov 01 '11 at 18:04
  • 1
    Couldn't you just do a dry-run of apt-get install and look at what is going to get installed, then just install everything but the app you are compiling? – duffydack Nov 01 '11 at 18:11
  • 2
    Or just install it and then remove the application (but not its dependencies). And/or install an updated/fixed package after building it. – JanC Nov 01 '11 at 18:27
  • I need the ability to do this so I can build and install a source package that isn't already built for my architecture. apt-get build-dep installs build dependencies, so apt-get -b source works, but runtime dependencies aren't installed, so dpkg -i *.deb fails. – Trevor Robinson Jan 26 '12 at 22:03
  • 1
    I don't think it's that unique of a situation. I just used @htorque's answer to install the development version of R without having to have the main build installed. I suspect any nightly build would benefit from this--there's no reason to not have things like gcc under version control just because you want to build a package from source. – Ari B. Friedman Apr 13 '14 at 09:18

6 Answers6

29

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!

htorque
  • 64,798
  • 2
    There are a lot of cases this doesn't handle, e.g. it will completely break on virtual packages (which are printed in angle brackets). See here for examples: http://askubuntu.com/questions/25361/in-apt-cache-depends-output-what-is-the-meaning-of-suggests-recommends – Trevor Robinson Jan 26 '12 at 22:06
  • Thanks for the hint, though I'm not really sure how the handle all those issues. :-( – htorque Jan 26 '12 at 22:10
  • 2
    If I may, I'd like to suggest simpler solution with AWK: sudo apt-get install $(apt-cache depends PACKAGE-NAME-HERE | awk '$1~/Depends/{printf $2" "}') – Sergiy Kolodyazhnyy Nov 18 '15 at 21:12
  • 2
    I ran into the virtual package issue you mention above; updating sed in the pipeline to the following worked for me: sed -e "s/.*ends:\ //" -e 's/<[^>]*>//'. In my case, the virtual package in < > was preceded by the package satisfying it, so removing the item in < > worked fine. – berto Mar 16 '16 at 14:27
9

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>

bksunday
  • 231
5

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) ...
    
cl-netbox
  • 31,163
  • 7
  • 94
  • 131
A.B.
  • 90,397
  • This solution is nice, but, like most of the other answers here, it will incorrectly try to install virtual packages. It also doesn't give apt the package versions, and that can cause problems sometimes. I think you can fix both problems by running aptitude search '!~i?reverse-depends("^PACKAGE_NAME$")' -F "%c %p %V" | awk '($1 != "v") {print $2"="$3}' | xargs sudo apt install instead. – Andrew Tapia Aug 09 '21 at 22:59
4

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

Eliah Kagan
  • 117,780
Laudeci
  • 97
2

You can parse the output of an apt install simulation to do this, here's a bash function to do so for you:

apt-install-depends() {
    local pkg="$1"
    apt-get install -s "$pkg" \
      | sed -n \
        -e "/^Inst $pkg /d" \
        -e 's/^Inst \([^ ]\+\) .*$/\1/p' \
      | xargs apt-get install
}

Usage:

apt-install-depends mopidy
muru
  • 197,895
  • 55
  • 485
  • 740
  • It's worth calling out that this will only find dependencies that are not yet installed on the system. It's not an exhaustive list of dependencies. That might be fine. I just wanted to call it out for others that come across this answer. – Taytay Mar 24 '21 at 13:38
  • 2
    I also discovered that this will install the parent package in addition to its dependencies. If you want to avoid that, you will have to explicitly filter it out. – Taytay Mar 24 '21 at 14:04
1

To install dependencies only, you can use apt-cache show package | grep Depends. This will give you a list of dependencies:

apt-cache show apache2 | grep Depends
Depends: apache2-mpm-worker (= 2.2.22-6ubuntu5.1) | apache2-mpm-prefork (= 2.2.22-6ubuntu5.1) | apache2-mpm-event (= 2.2.22-6ubuntu5.1) | apache2-mpm-itk (= 2.2.22-6ubuntu5.1), apache2.2-common (= 2.2.22-6ubuntu5.1)

then you can decide what package install with apt-get. There is also aptitude in the interactive mode, you look for the package select it and then install it's dependencies:

enter image description here

Braiam
  • 67,791
  • 32
  • 179
  • 269