Check whether a package is installed

New to Debian (Or Linux in general)? Ask your questions here!
Post Reply
Message
Author
User avatar
alienspy
Posts: 199
Joined: 2023-02-12 15:37
Has thanked: 102 times
Been thanked: 9 times

Check whether a package is installed

#1 Post by alienspy »

Hello! As a noob and non-techy i have been using for a long time apt install command for finding out whether a package is installed.

apt install nano, oh, it is already installed, cool, ok.

Now i try to educate myself and read the Debian Administrator's Handbook. So i am looking for a more appropriate alternative.

For example in Handbook is mentioned apt-cache policy packagename command

Lets test three options. Package is installed (nano is installed on my system). Not installed (vim is not installed). Package is probably installed, but i have forgot the full name. Gnome-text-editor is installed. But i won't enter the full name.

apt-cache policy packagename. It shows both installed and non-installed packages. For example i see that nano is installed, and vim is not. But if i enter gnome-text-ed (editor) it gives me "N: Unable to locate package gnome-text-ed"

Code: Select all

$ apt-cache policy nano
nano:
  Installed: 7.2-1
  Candidate: 7.2-1
  Version table:
 *** 7.2-1 500
        500 https://deb.debian.org/debian bookworm/main amd64 Packages
        100 /var/lib/dpkg/status

Code: Select all

$ apt-cache policy vim
vim:
  Installed: (none)
  Candidate: 2:9.0.1378-2
  Version table:
     2:9.0.1378-2 500
        500 https://deb.debian.org/debian bookworm/main amd64 Packages

Code: Select all

$ apt-cache gnome-text-ed
E: Invalid operation gnome-text-ed
apt search packagename understands that i want gnome-text-ed(itor). But for nano and vim its output is huge and irrelevant.

Code: Select all

$ apt search nano
Sorting... Done
Full Text Search... Done
alpine-pico/stable 2.26+dfsg-1 amd64
  Simple text editor from Alpine, a text-based email client

arduino-core-avr/stable,stable 1.8.6+dfsg-1 all
  Arduino Core for AVR microcontroller

bornagain/stable 1.19.0-3+b5 amd64
  Simulate and fit X-ray and neutron GISAS -- binary........

Code: Select all

$ apt search gnome-text-ed
Sorting... Done
Full Text Search... Done
gnome-text-editor/stable,now 43.2-1 amd64 [installed]
  simple text editor for GNOME
apt list -a packagename has the same problem as apt-cache policy. It shows (not)installed files, but fails to understand incomplete package names.

Code: Select all

$ apt list -a nano
Listing... Done
nano/stable,now 7.2-1 amd64 [installed]

nano/stable 7.2-1 i386

Code: Select all

$ apt list -a vim
Listing... Done
vim/stable 2:9.0.1378-2 amd64

vim/stable 2:9.0.1378-2 i386

Code: Select all

$ apt list -a gnome-text-ed
Listing... Done
Some even advise which packagename, but it just shows no output for non-installed packages, which can be confused with a mistake.

apt list | grep packagename works fine in general. But again it gives huge list for nano and vim. Though it understands "text-edito"

Code: Select all

$ apt list | grep nano

libghc-nanospec-dev/stable 0.2.2-4+b1 amd64
libghc-nanospec-dev/stable 0.2.2-4+b1 i386
libghc-nanospec-doc/stable,stable 0.2.2-4 all...

Code: Select all

$ apt list | grep gnome-text-ed

gnome-text-editor/stable,now 43.2-1 amd64 [installed]
gnome-text-editor/stable 43.2-1 i386
Also there is

dpkg -l | grep -i packagename

It understands incomplete package name. But i can't understand its output for vim. "ii means "It should be installed and it is installed"" (src). Does it mean that vim is partially installed? "Common" files and "compact" version? apt install wants me to install only vim-runtime.

Code: Select all

$ dpkg -l | grep nano
ii  nano                                               7.2-1                                amd64        small, friendly text editor inspired by Pico

Code: Select all

$ dpkg -l | grep vim
ii  vim-common                                         2:9.0.1378-2                         all          Vi IMproved - Common files
ii  vim-tiny                                           2:9.0.1378-2                         amd64        Vi IMproved - enhanced vi editor - compact version

Code: Select all

$ dpkg -l | grep gnome-text-ed
ii  gnome-text-editor                                  43.2-1                               amd64        simple text editor for GNOME
Lets try Konsole (not installed) instead of vim

Code: Select all

$ dpkg -l | grep konsole
$
No output. So it says nothing if the package is not installed. Not good.

But last two options are not a single commands unfortunately, but a combination of commands. Possible to remember, though, after a number of uses, i think.

What commands do you use for this purpose? Some articles even advise bash scripts for that.

amin11
Posts: 53
Joined: 2023-03-25 09:04
Has thanked: 7 times
Been thanked: 7 times

Re: Check whether a package is installed

#2 Post by amin11 »

Code: Select all

apt list -i *package_name*
For example

Code: Select all

apt list -i *vim*

User avatar
fabien
Forum Helper
Forum Helper
Posts: 1040
Joined: 2019-12-03 12:51
Location: Anarres (Toulouse, France actually)
Has thanked: 92 times
Been thanked: 233 times

Re: Check whether a package is installed

#3 Post by fabien »

alienspy wrote: 2024-05-29 19:43 apt-cache policy packagename. It shows both installed and non-installed packages. For example i see that nano is installed, and vim is not. But if i enter gnome-text-ed (editor) it gives me "N: Unable to locate package gnome-text-ed"
apt-cache policy and apt-cache list support glob, see man 7 glob
man 8 apt wrote:list
list is somewhat similar to dpkg-query --list in that it can display a list of packages satisfying certain criteria. It supports glob(7) patterns for matching package names as well as options to list installed (--installed), upgradeable (--upgradeable) or all available (--all-versions) versions.

Code: Select all

$> apt list "*text-edit*"
Listing... Done
gnome-text-editor/stable 43.2-1 amd64
$> apt policy "gnome-text-edit*"
gnome-text-editor:
  Installed: (none)
  Candidate: 43.2-1
  Version table:
     43.2-1 500
        500 https://deb.debian.org/debian bookworm/main amd64 Packages
apt search understands regular expressions (regex), see man 1 grep
man 8 apt wrote:search (apt-cache(8))
search can be used to search for the given regex(7) term(s) in the list of available packages and display matches.
man 8 apt-cache wrote:search performs a full text search on all available package lists for the POSIX regex pattern given, see regex(7). It searches the package names and the descriptions for an occurrence of the regular expression

Code: Select all

$> apt search ".*text-edit.*"
Sorting... Done
Full Text Search... Done
gnome-text-editor/stable 43.2-1 amd64
  simple text editor for GNOME

jove/stable 4.17.5.1-2 amd64
  Jonathan's Own Version of Emacs - a compact, powerful editor

wadc/stable 3.1-1 all
  programming environment for creating Doom maps
You can search on package names only

Code: Select all

$> apt --names-only search ".*text-edit.*"
Sorting... Done
Full Text Search... Done
gnome-text-editor/stable 43.2-1 amd64
  simple text editor for GNOME
alienspy wrote: 2024-05-29 19:43 apt search packagename understands that i want gnome-text-ed(itor). But for nano and vim its output is huge and irrelevant.

Code: Select all

$> apt search "^nano$|^vim$"
Sorting... Done
Full Text Search... Done
nano/stable,now 7.2-1 amd64 [installed]
  small, friendly text editor inspired by Pico

vim/stable,now 2:9.0.1378-2 amd64 [installed]
  Vi IMproved - enhanced vi editor
alienspy wrote: 2024-05-29 19:43 Some even advise which packagename, but it just shows no output for non-installed packages, which can be confused with a mistake.
which is different and has nothing to do with package search.
man 1 which wrote:DESCRIPTION
which returns the pathnames of the files (or links) which would be executed in the current environment

Code: Select all

$> which csplit
/usr/bin/csplit
$> apt search "csplit"
Sorting... Done
Full Text Search... Done
coreutils/stable,now 9.1-1 amd64 [installed]
  GNU core utilities
alienspy wrote: 2024-05-29 19:43 apt list | grep packagename works fine in general. But again it gives huge list for nano and vim. Though it understands "text-edito"

Code: Select all

$> apt list | grep "^nano/\|^vim/\|^[^/]\+text-edito[^/]\+/"

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

gnome-text-editor/stable 43.2-1 amd64
nano/stable,now 7.2-1 amd64 [installed]
vim/stable,now 2:9.0.1378-2 amd64 [installed]
alienspy wrote: 2024-05-29 19:43 Also there is

dpkg -l | grep -i packagename

It understands incomplete package name. But i can't understand its output for vim. "ii means "It should be installed and it is installed"" (src). Does it mean that vim is partially installed?
dpkg -l lists only installed packages, or packages that have been removed but not purged (rc), or packages in an error state, or marked for installation... The explanation of the codes is in the header.

Code: Select all

$> dpkg -l | head -n5
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
+++-====================================-====================================-============-===========
In your case, vim is not installed, in my case it is.

Code: Select all

$> dpkg -l | grep "vim"
ii  vim                                  2:9.0.1378-2                         amd64        Vi IMproved - enhanced vi editor
ii  vim-common                           2:9.0.1378-2                         all          Vi IMproved - Common files
ii  vim-runtime                          2:9.0.1378-2                         all          Vi IMproved - Runtime files
ii  vim-tiny                             2:9.0.1378-2                         amd64        Vi IMproved - enhanced vi editor - compact version

Code: Select all

$> dpkg -l | grep "^ii.[[:blank:]]vim[[:blank:]]"
ii  vim                                  2:9.0.1378-2                         amd64        Vi IMproved - enhanced vi editor
alienspy wrote: 2024-05-29 19:43 What commands do you use for this purpose? Some articles even advise bash scripts for that.
All these commands, and I also have my own super secret scripts :)
ImageShare your Debian SCRIPTS
There will be neither barrier nor walls, neither official nor guard, there will be no more desert and the entire world will become a garden. — Anacharsis Cloots

User avatar
alienspy
Posts: 199
Joined: 2023-02-12 15:37
Has thanked: 102 times
Been thanked: 9 times

Re: Check whether a package is installed

#4 Post by alienspy »

amin11 wrote: 2024-05-29 20:32

Code: Select all

apt list -i *package_name*
For example

Code: Select all

apt list -i *vim*
Yes, but it has the downside that you cant search for an incomplete name or a slightly wrong name of a package.

User avatar
alienspy
Posts: 199
Joined: 2023-02-12 15:37
Has thanked: 102 times
Been thanked: 9 times

Re: Check whether a package is installed

#5 Post by alienspy »

fabien wrote: 2024-05-29 21:40 All these commands, and I also have my own super secret scripts :)
Wow, much to learn! Thanks

User avatar
sunrat
Administrator
Administrator
Posts: 7164
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 126 times
Been thanked: 616 times

Re: Check whether a package is installed

#6 Post by sunrat »

alienspy wrote: 2024-05-29 21:45
amin11 wrote: 2024-05-29 20:32

Code: Select all

apt list -i *package_name*
For example

Code: Select all

apt list -i *vim*
Yes, but it has the downside that you cant search for an incomplete name or a slightly wrong name of a package.
Asterisks are wildcards. It will search incomplete names but not slightly wrong names. Try

Code: Select all

$ apt list -i *image*
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
pbear
Posts: 492
Joined: 2023-08-27 15:05
Location: San Francisco
Has thanked: 2 times
Been thanked: 81 times

Re: Check whether a package is installed

#7 Post by pbear »

My preferred command is a variant of one already mentioned: apt list --installed | grep xyz, where xyz can be any part of the package name, no wildcards or globs required.
Synaptic package manager has a search function and sometimes I use that (mainly if I expect to use Synaptic after doing the search).

mrmazda
Posts: 440
Joined: 2023-06-02 02:22
Has thanked: 13 times
Been thanked: 52 times

Re: Check whether a package is installed

#8 Post by mrmazda »

pbear wrote: 2024-05-30 03:26 My preferred command is a variant of one already mentioned: apt list --installed | grep xyz, where xyz can be any part of the package name, no wildcards or globs required.
Kinda like mine:

Code: Select all

# alias | grep pkg
alias Dq='echo dpkg-query -l ; dpkg-query -l | grep '
:D

Bulkley
Posts: 6400
Joined: 2006-02-11 18:35
Has thanked: 5 times
Been thanked: 44 times

Re: Check whether a package is installed

#9 Post by Bulkley »

I use a package browser called packagesearch. It's quick, easy and informative.

Dai_trying
Posts: 1139
Joined: 2016-01-07 12:25
Has thanked: 9 times
Been thanked: 26 times

Re: Check whether a package is installed

#10 Post by Dai_trying »

I don't know if this will add anything to the thread but I often use dpkg with the parameter --get selections piped into grep to check on installed packages.

Code: Select all

$ dpkg --get-selections | grep apt
apt						install
apt-file					install
apt-listchanges					install
apt-transport-https				install
apt-utils					install
laptop-detect					install
libapt-pkg-perl					install
libapt-pkg6.0:amd64				install
libatk-adaptor:amd64				install
libfreeaptx0:amd64				install
libraptor2-0:amd64				install
python-apt-common				install
python3-apt					install
synaptic					install
task-laptop					install

lindi
Debian Developer
Debian Developer
Posts: 531
Joined: 2022-07-12 14:10
Has thanked: 2 times
Been thanked: 101 times

Re: Check whether a package is installed

#11 Post by lindi »

You can use dpkg-query without grep quite efficiently:

Code: Select all

$ time dpkg-query -Wf '${db:Status-Status}' apt
installed
real	0m0.030s
user	0m0.023s
sys	0m0.007s

Dai_trying
Posts: 1139
Joined: 2016-01-07 12:25
Has thanked: 9 times
Been thanked: 26 times

Re: Check whether a package is installed

#12 Post by Dai_trying »

lindi wrote: 2024-05-31 22:37 You can use dpkg-query without grep quite efficiently:

Code: Select all

$ time dpkg-query -Wf '${db:Status-Status}' apt
installed
real	0m0.030s
user	0m0.023s
sys	0m0.007s
The only issue with this (for me) would be that you must enter the exact package name and it doesn't list other similar named packages (which is my preference) but it probably answers the OP more exactly.

Post Reply