How to test for installed package using shell script on Ubuntu and Debian


Use shell script to check whether a given packages is available/installed on Ubuntu or Debian Linux system.

Operating System and Software Versions

  • Operating System: – Ubuntu, Debian




  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user


Shell Script

The following script will check whether a package supplied to the below script via command line argument is installed on the system.

The script first uses dpkg to check whether package is installed. Depending whether the dpkg command executes successfully the script will print a package installation status to standard output.


dpkg -s $1 &> /dev/null

if [ $? -eq 0 ]; then
    echo "Package $1 is installed!"
    echo "Package $1 is NOT installed!"


Save the above script to eg. file and make it executable:

$ chmod +x

Next, use the script to check for installed package while supplying the package name as an argument. For example:

$ ./ vim
Package vim is installed!                                                                                                                                                                       
$ ./ nginx
Package nginx is NOT installed!

Comments and Discussions
Linux Forum