How to find out if a specific package is installed on Debian?

I’m working with a Raspberry Pi B+ and Raspbian 5/5/2015 and some guides that are a few years old. I’ve got an external NTFS HDD hooked up to the Pi. Due to the articles age(s), common practices have changed, and it turns out that certain packages and features and functions are now built-in and/or automatic.

For example, I found out that Raspbian will handle the automounting of an NTFS drive/volume if you just install ntfs-3g. All instructions and guidance I could find (IRC, raspberry pi forum, and a Linux dude I know) all went extremely technical doing who-knows-what to my system to try to assist me, when in reality we were all tripping over automatic features that no one knew about or thought to check. I’ve since installed Raspbian fresh to a new SD card and so far just turned on SSH, updated apt-get, and installed ntfs-3g.

This is the article I am using right now: http://www.howtogeek.com/139433/how-to-turn-a-raspberry-pi-into-a-low-power-network-storage-device/?PageSpeed=noscript

My next step is to get Samba up and running. Yes, I know Linux people hate Windows, but I’m a Windows guy, so until I can know enough about Linux for a Linux system to be my fallback, Windows machines are my fallbacks. Eventually this Raspberry Pi will get a multi-TB drive, though for right now it’s just a few GB; which is why it needs to be NTFS.

I would like to check if Samba is installed, since the next step in my instructions tell me how to install and configure it. I’ve tried a few commands and I’m not sure what to do with the results or how to use them. I searched here and the technical details are above my capabilities and I don’t think they address my seemingly simple need to find out if a package is installed or not.

I tried:

apt-cache dump

this gave me way too much data. It scrolled down for a few minutes. I got this from The Raspberry Pi Handbook 3rd Edition (Link – Amazon)

I tried

apt-cache showpkg samba
Package: samba
Versions:
2:3.6.6-6+deb7u5 (/var/lib/apt/lists/mirrordirector.raspbian.org_raspbian_dists_                                  wheezy_main_binary-armhf_Packages)
 Description Language:
                 File: /var/lib/apt/lists/mirrordirector.raspbian.org_raspbian_d                                  ists_wheezy_main_binary-armhf_Packages
                  MD5: 0122ac62ef5f4ae21eb2e195eb45ad1d


Reverse Depends:
  winbind,samba 2.2.3-2
  task-file-server,samba
  swat,samba 2:3.6.6-6+deb7u5
  smbclient,samba 2.999+3.0.alpha21-4
  samba4-common-bin,samba 2:3.3.0~rc2-5
  samba4,samba 2:3.3.0~rc2-5
  samba-dbg,samba 2:3.6.6-6+deb7u5
  samba-common-bin,samba 3.0.20b-1
  samba-common,samba 3.0.20b-1
  qtsmbstatus-server,samba
  qemu-system,samba
  nautilus-share,samba 3.0.27a
  libwbclient0,samba 2:3.4.1
  libpam-winbind,samba 2.2.3-2
  libpam-smbpass,samba
  libnss-winbind,samba 2.2.3-2
  gadmin-samba,samba
  education-main-server,samba
  dpsyco-samba,samba
Dependencies:
2:3.6.6-6+deb7u5 - samba-common (5 2:3.6.6-6+deb7u5) libwbclient0 (5 2:3.6.6-6+d                                  eb7u5) libacl1 (2 2.2.51-8) libattr1 (2 1:2.4.46-8) libc6 (2 2.13-28) libcap2 (2                                   2.10) libcomerr2 (2 1.01) libcups2 (2 1.4.0) libgcc1 (2 1:4.4.0) libgssapi-krb5                                  -2 (2 1.10+dfsg~) libk5crypto3 (2 1.6.dfsg.2) libkrb5-3 (2 1.10+dfsg~) libldap-2                                  .4-2 (2 2.4.7) libpam0g (2 0.99.7.1) libpopt0 (2 1.14) libtalloc2 (2 2.0.4~git20                                  101213) libtdb1 (2 1.2.7+git20101214) zlib1g (2 1:1.1.4) debconf (18 0.5) debcon                                  f-2.0 (0 (null)) libpam-runtime (2 1.0.1-11) libpam-modules (0 (null)) lsb-base                                   (2 3.2-13) procps (0 (null)) update-inetd (0 (null)) adduser (0 (null)) dpkg (2                                   1.15.7.2) openbsd-inetd (16 (null)) inet-superserver (0 (null)) smbldap-tools (0                                   (null)) ldb-tools (0 (null)) ctdb (0 (null)) logrotate (0 (null)) tdb-tools (0                                   (null)) samba4 (3 4.0.0~alpha6-2) samba-common (1 2.0.5a-2)
Provides:
2:3.6.6-6+deb7u5 -
Reverse Provides:
pi@raspberrypi ~ $ apt-cache showpkg ntfs-3g
Package: ntfs-3g
Versions:
1:2012.1.15AR.5-2.1+deb7u2 (/var/lib/apt/lists/mirrordirector.raspbian.org_raspbian_dists_wheezy_main_binary-armhf_Packages) (/var/lib/dpkg/status)
 Description Language:
                 File: /var/lib/apt/lists/mirrordirector.raspbian.org_raspbian_dists_wheezy_main_binary-armhf_Packages
                  MD5: b2df024e8627b5d253b85f35263376ef


Reverse Depends:
  udisks,ntfs-3g
  ntfsprogs,ntfs-3g
  ntfs-config,ntfs-3g
  ntfs-3g-dev,ntfs-3g 1:2012.1.15AR.5-2.1+deb7u2
  ntfs-3g-dbg,ntfs-3g 1:2012.1.15AR.5-2.1+deb7u2
  kvpm,ntfs-3g
  fsarchiver,ntfs-3g
Dependencies:
1:2012.1.15AR.5-2.1+deb7u2 - debconf (18 0.5) debconf-2.0 (0 (null)) libc6 (2 2.13-28) libfuse2 (2 2.8.1) libgcc1 (2 1:4.4.0) libgcrypt11 (2 1.4.5) libgnutls26 (2 2.12.17-0) multiarch-support (0 (null)) fuse (0 (null)) libntfs-3g75 (0 (null)) ntfsprogs (3 1:2011.10.9AR.1-3~) libntfs-3g75 (0 (null)) ntfsprogs (0 (null))
Provides:
1:2012.1.15AR.5-2.1+deb7u2 -
Reverse Provides:
pi@raspberrypi ~ $ apt-cache showpkg ntfsprogs
Package: ntfsprogs
Versions:
1:2012.1.15AR.5-2.1+deb7u2 (/var/lib/apt/lists/mirrordirector.raspbian.org_raspbian_dists_wheezy_main_binary-armhf_Packages)
 Description Language:
                 File: /var/lib/apt/lists/mirrordirector.raspbian.org_raspbian_dists_wheezy_main_binary-armhf_Packages
                  MD5: 95f41af9cf1d0b5b66afb7d2a9e7c75d


Reverse Depends:
  partitionmanager,ntfsprogs
  ntfs-3g,ntfsprogs
  ntfs-3g,ntfsprogs 1:2011.10.9AR.1-3~
  gparted,ntfsprogs
  fsarchiver,ntfsprogs
  fai-setup-storage,ntfsprogs
Dependencies:
1:2012.1.15AR.5-2.1+deb7u2 - ntfs-3g (0 (null))
Provides:
1:2012.1.15AR.5-2.1+deb7u2 -
Reverse Provides:

but I’m not sure what to make of the results. I can’t tell if it’s going to apt-get the servers and getting information, or pulling it from my system.

I tried :

dpkg --get-selections

which I got from here:
http://www.howtogeek.com/howto/linux/show-the-list-of-installed-packages-on-ubuntu-or-debian/?PageSpeed=noscript
but I think I’m running into the same problem. It seems the syntax has changed since 2007. The man page / help file seems to lead me to believe that the command should work

Usage: dpkg [<option> ...] <command>

--get-selections [<pattern> ...] Get list of selections to stdout.

but I get an error:

dpkg –get-selections samba
dpkg: error: need an action option

I found a few wuestions here that are related, but don’t give me what I am looking for. I am interested in just knowing what’s installed, but I guess that’s a topic for another question.

dpkg -l $PACKAGENAME

is enough. The first two characters in the line will show the package status

Answered By: Marco d'Itri

apt-cache showpkg shows detailed information about potentially installable packages. It does indicate whether the package is installed, kind of, but not in a very readable way:

Versions:
2:3.6.6-6+deb7u5 (/var/lib/apt/lists/mirrordirector.raspbian.org_raspbian_dists_wheezy_main_binary-armhf_Packages)

If the package was installed, you’d see (/var/lib/dpkg/status) at the end of the line. However, this isn’t fully reliable, because you’d also see this indication if the package was known to your system but not fully installed, e.g. if it was in the “package uninstalled but configuration files left over” state.

A more useful apt-cache subcommand is apt-cache policy. It clearly shows the installed version (if any) and the available version(s). For example, here’s output from a machine which has samba installed but not samba-dev:

samba:
  Installed: 2:4.1.17+dfsg-2
  Candidate: 2:4.1.17+dfsg-2
  Version table:
 *** 2:4.1.17+dfsg-2 0
        500 http://ftp.fr.debian.org/debian/ jessie/main amd64 Packages
        100 /var/lib/dpkg/status
samba-dev:
  Installed: (none)
  Candidate: 2:4.1.17+dfsg-2
  Version table:
     2:4.1.17+dfsg-2 0
        500 http://ftp.fr.debian.org/debian/ jessie/main amd64 Packages

Alternatively, you can use the dpkg command to get information about your current system. APT is the software that manages the download of packages, dependency analysis, etc. Dpkg is the low-level software that carries out the actual installation of a package file.

dpkg -l samba

This shows a line beginning with i if the package is installed, and a line beginning with u or p or nothing at all if the package is not installed.

$ dpkg -l samba samba-dev
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
ii  samba          2:4.1.17+dfs amd64        SMB/CIFS file, print, and login s
dpkg-query: no packages found matching samba-dev

(dpkg-query is the dpkg subcommand that returns information about the package database.)

Note that if you just want to ensure that a package is installed, you can simply run

apt-get install samba

This won’t do anything if the latest version of the package that’s available in your distribution is already installed. It will install the package if it isn’t installed yet, and it will upgrade it if you have an older version.

dpkg -l | grep -e package1 -e package2 ....

Will list packages you are interested with their current insstallation stauts. The output will be something like this

bala@bala-laptop:~$ dpkg -l | grep apache2
ii  apache2                                2.4.10-10                            amd64        Apache HTTP Server

The first 2 characters tell the status – ii here means
First i – marked for installation
Second i – Successfully installed

There are other status codes like r meaning marked for removal, p meaning marked for purging etc.

aptitude show foo

will show status of the pakage

root@srv:# aptitude show python-jmespath
Package: python-jmespath
Version: 0.9.0-2
State: not installed
Priority: optional
Section: python
Maintainer: Debian Python Modules Team python-modules-team@lists.alioth.debian.org
Architecture: all
Uncompressed Size: 97.3 k
Depends: python (>= 2.7)
Description: JSON Matching Expressions (Python 2)
 JMESPath is python library which allows you to declaratively specify how to extract elements from a JSON document.

 This package contains the module for Python 2.
Homepage: https://github.com/boto/jmespath


root@cx-ops02:#
Answered By: valch85

I hope you can solve this problem.

  • dpkg --help for help about installing and deinstalling packages [*];

  • use apt or aptitude for user-friendly package management;

  • type dpkg -Dhelp for a list of dpkg debug flag values;
  • type dpkg --force-help for a list of forcing options;
  • type dpkg-deb --help for help about manipulating *.deb files;

Options marked [*] produce a lot of output – pipe it through less or more !

Answered By: مالك القلب

A slightly simpler alt to dpkg -l | grep ... IMO:

dpkg-query -W -f='${Status}' pkg | grep -q " installed"

Will return 0 (success) iff pkg is installed.

Answered By: spinkus
grep-status 'package'

It is not preinstalled in Debian based distro. It can be installed through:

sudo apt install dctrl-tools

Here is an example:

$ grep-status dctrl-tools

Package: dctrl-tools
Status: install ok installed
Priority: optional
Section: utils
Installed-Size: 296
...

grep-status manpages

Answered By: GAD3R

Seems to me

dpkg-query -s <pkg>

might be simpler than other options.

This command is compatible with script testing, by ignoring the output:

dpkg-query -s <pkg> >/dev/null 2>&1

and returning 0 if the package is installed, or 1 if it is not installed.

Answered By: Rizzer

I’ve previously used dpkg-query, which enabled testing of multiple packages in one invocation.

Using Bash (for array variables):

REQUIRED_TOOLS=(gnupg libcdio-utils python-apt realpath syslinux wget)
MISSING_TOOLS=$(dpkg-query -Wf'${db:Status-Abbrev}${binary:Package}n' "${REQUIRED_TOOLS[@]}" 2>&1 | sed -n 's/.* no packages found matching /n/;s/^[^i]//p') | xargs echo

ERRORS="${MISSING_TOOLS:+Missing tools - please install: $MISSING_TOOLS}"
Answered By: Toby Speight

Over the years the syntax of apt got a lot simpler.

Now you can just use apt list <packagename>

Answered By: mashuptwice
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.