How to check whether the given package is already installed in Debian/Ubuntu systems.

We have recently published an article about bulk package installation.

While doing that, I struggled to get the installed package information and did a small google search and found few methods about it.I would like to share it in our website so, that it will be helpful for others too.

There are numerous ways we can achieve this.

I have add seven ways to achieve this. However, you can choose the method which suites your situation.

Those methods are listed below.

  • apt-cache Command: apt-cache command is used to query the APT cache or package metadata.
  • apt Command: APT is a powerful command-line tool for installing, downloading, removing, searching and managing packages on Debian based systems.
  • dpkg-query Command: dpkg-query is a tool to query the dpkg database.
  • dpkg Command: dpkg is a package manager for Debian based systems.
  • which Command: The which command returns the full path of the executable that would have been executed when the command had been entered in terminal.
  • whereis Command: The whereis command used to search the binary, source, and man page files for a given command.
  • locate Command: locate command works faster than the find command because it uses updatedb database, whereas the find command searches in the real system.

Method-1 : How to check whether the given package is already installed in Ubuntu system using apt-cache Command?

apt-cache command is used to query the APT cache or package metadata from APT’s internal database.

It will search and display the information about the given package. It shows whether the package is installed or not, installed package version, source repository information.

The below output clearly shows that nano package has already installed in the system. Since installed part is showing the installed version of nano package.

# 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

Method-2 :How to check whether the given package is already installed in Ubuntu system using apt Command?

APT is a powerful command-line tool for installing, downloading, removing, searching and managing as well as querying information about packages as a low-level access to all features of the libapt-pkg library. It contains some less used command-line utilities related to package management.

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

Method-3 :How to check whether the given package is already installed in Ubuntu system using dpkg-query Command?

dpkg-query is a tool to show information about packages listed in the dpkg database.

In the below output first column showing ii. It means, the given package has already installed in the system.

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

Method-4 : How to check whether the given package is already installed in Ubuntu system using dpkg Command?

DPKG stands for Debian Package ,It is a tool to install, build, remove and manage Debian packages, but unlike other package management systems, it cannot automatically download and install packages or their dependencies.

In the below output first column showing ii. It means, the given package has already installed in the system.

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

Method-5 : How to check whether the given package is already installed in Ubuntu system using which Command?

The which command returns the full path of the executable that would have been executed when the command had been entered in terminal.It’s very useful when you want to create a desktop shortcut or symbolic link for executable files.

which command searches the directories listed in the current user’s PATH environment variable not for all the users. I mean, when you are logged in your own account and you can’t search for root user file or directory.

If the following output shows the given package binary or executable file location then the given package has already installed in the system. If not, the package is not installed in system.

# which nano
/bin/nano

Method-6 : How to check whether the given package is already installed in Ubuntu system using whereis Command?

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

If the following output shows the given package binary or executable file location then the given package has already installed in the system. If not, the package is not installed in system.

# whereis nano
nano: /bin/nano /usr/share/nano /usr/share/man/man1/nano.1.gz /usr/share/info/nano.info.gz

Method-7 : How to check whether the given package is already installed in Ubuntu system using locate Command?

locate command works faster than the find command because it uses updatedb database, whereas the find command searches in the real system.

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

locate command doesn’t pre-installed in most of the distributions so, use your distribution package manager to install it.

The database is updated regularly through cron jobs. Even, we can update it manually.

If the following output shows the given package binary or executable file location then the given package has already installed in the system. If not, the package is not installed in system.

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

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 *