How to find out if a package is installed in Linux?

A Linux package is a software package that contains lot of files and information about those files. Every software on Linux is installed using packages.

Package installation on Linux sometimes fails with the following error “package is already installed; nothing to do”.

To avoid this you should first check if the package is installed on the system and only then try its installation.

There are some commands to quickly check whether a package is already installed or not. Let’s discuss each of them.

Several articles have been written in the past on how to manage packages in Linux. Each article was written for a different purpose, and you can check them out by clicking the embedded link in this sentence.

1) How to find out if a package is installed or not in Linux

You might know that the installed package can be easily identified using the Linux distribution package manager.

However, there are some general commands in Linux that allow you to verify the package information regardless of distribution.

1.a) Using which command

The ‘which’ command returns an executable path that can be executed when the command is entered in the terminal.

‘which’ command searches directories listed in the current user’s PATH environment variable, not for all users.

Run the following command to print the full path of the VIM executable file location. If VIM package is installed on your computer, it will show the installed path as shown below:

# which vi
/usr/bin/vi

1.b) Using whereis command

The ‘whereis’ command is used to search the binary, source, and man page files for a given command.

If the following output shows the location of a given package binary or executable file, the given package is already installed on the system. Otherwise, the package is not installed on the system.

# whereis vi
vi: /usr/bin/vi /usr/share/man/man1/vi.1p.gz /usr/share/man/man1/vi.1.gz

1.c) Using locate command

The ‘locate’ command performs faster than the find command because it uses an updatedb database, whereas the find command searches in real time.

It uses a database rather than looking for individual directory paths to get a given file.

If the following output shows the location of a given package binary or executable file, the given package is already installed on the system. Otherwise, the package is not installed on the system.

# locate --basename '\nano'
/usr/bin/nano
/usr/share/nano
/usr/share/doc/nano

2) How to find out whether a package is installed or not in Linux, using package manager

Distribution official package managers can be used to find out whether a package is installed or not in Linux.

2.a) On Arch Linux

Use pacman command to check if the given package is installed or not in Arch Linux and its derivatives.

If the below command returns nothing then the ‘nano’ package is not installed in system. If it is installed, the respective name will be displayed as follows.

$ pacman -Qs nano
local/nano 4.3-1 (base)
    Pico editor clone with enhancements

2.b) On CentOS / Red Hat (RHEL) 6/7

Use yum command or rpm command to determine whether a given package is installed or not in Red Hat and their clones like CentOS, Oracle Linux.

Using yum command:

# yum list installed openssh
Loaded plugins: fastestmirror, universal-hooks
Loading mirror speeds from cached hostfile
 * EA4: 203.174.85.202
 * cpanel-addons-production-feed: 203.174.85.202
 * cpanel-plugins: 203.174.85.202
 * base: centos.netonboard.com
 * epel: mirror.airenetworks.es
 * extras: centos.netonboard.com
 * nux-dextop: mirror.li.nux.ro
 * updates: centos.netonboard.com
Installed Packages
openssh.x86_64                                                                                          7.4p1-16.el7                                                                                          @base

Using rpm command:

# rpm -qa nano
nano-2.3.1-10.el7.x86_64

2.c) On Fedora/CentOS 8/RHEL 8

Use the dnf command or rpm command to determine if a package is installed in Fedora and CentOS/RHEL 8 systems.

Using dnf command:

# dnf list installed httpd
Last metadata expiration check performed 0:44:26 ago on Tue Jun  9 22:52:44 2019.
Installed Packages
httpd.x86_64                        2.4.12-1.fc22                        @System

Using rpm command:

# rpm -qa nano

2.d) On Ubuntu/Debian

There are several commands that can be used in Debian-based system (Ubuntu, Linux Mint, etc.) to find this information as listed below:

  • apt command
  • apt-cache command
  • dpkg command
  • dpkg-query command

Using apt-cache command:

# apt-cache policy nano
nano:
  Installed: 2.9.3-2
  Candidate: 2.9.3-2
  Version table:
 *** 2.9.3-2 500
        500 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 Packages
        100 /var/lib/dpkg/status

Using apt command:

# apt -qq list nano
nano/bionic,now 2.9.3-2 amd64 [installed]

Using dpkg-query command:

# dpkg-query --list | grep -i nano
ii  nano    2.9.3-2    amd64    small, friendly text editor inspired by Pico

Using dpkg command:

# dpkg -l | grep -i nano
ii  nano     2.9.3-2    amd64   small, friendly text editor inspired by Pico

2.e) On openSUSE

Use the zypper command or rpm package manager to check if a package is installed on openSUSE.

Using zypper command:

$ zypper se -i nano

 Loading repository data…
 Reading installed packages…
 S  | Name      | Summary                             | Type
 ---+-----------+-------------------------------------+--------
 i+ | nano      | Pico editor clone with enhancements | package
 i+ | nano-lang | Translations for package nano       | package

Using rpm command:

# rpm -qa nano

Closing Notes

In this guide, we have shown you several commands to find out whether a package is installed in Linux.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *