Check if an apt-get package is installed and then install it if it's not on Linux

后端 未结 22 1144
轮回少年
轮回少年 2020-12-02 04:02

I\'m working on a Ubuntu system and currently this is what I\'m doing:

if ! which command > /dev/null; then
   echo -e \"Command not found! Install? (y/n)         


        
相关标签:
22条回答
  • 2020-12-02 04:25

    This command is the most memorable:

    dpkg --get-selections <package-name>
    

    If it's installed it prints:

    <package-name> install

    Otherwise it prints

    No packages found matching <package-name>.

    This was tested on Ubuntu 12.04.1 (Precise Pangolin).

    0 讨论(0)
  • 2020-12-02 04:28

    It seems that nowadays apt-get has an option --no-upgrade that just does what the OP wants:

    --no-upgrade Do not upgrade packages. When used in conjunction with install, no-upgrade will prevent packages listed from being upgraded if they are already installed.

    Manpage from https://linux.die.net/man/8/apt-get

    Therefore you can use

    apt-get install --no-upgrade package
    

    and package will be installed only if it's not.

    0 讨论(0)
  • 2020-12-02 04:29

    Many things has been told but for me simplest way is:

    dpkg -l | grep packagename
    
    0 讨论(0)
  • 2020-12-02 04:30

    This will do it. apt-get install is idempotent.

    sudo apt-get install command
    
    0 讨论(0)
提交回复
热议问题