How can i write a script to call many 'apt-get install' functions?

Posted on 2007-10-08
Last Modified: 2013-12-06
I need to install many dependencies on a few machines, of which i know the dependency names, and i really dont want to have to write these out each time.

How can i write a script to run these for me?

i.e. to do 'apt-get install this package' then 'apt-get install this package2' etc...

Question by:kerpoise
    LVL 18

    Accepted Solution

    Just create a file called whatever you want) and put the follwing code in it(assuming you use sudo):
    sudo apt-get install package1
    sudo apt-get install package2
    sudo apt-get install package3

    Save the file, and give it execute permissions: chmod u+x

    The when you want to run the script, use:

    You will still be prompted for a password if using sudo.
    LVL 6

    Assisted Solution

    Why do you want to call apt-get install for each package?
    apt-get allows you to install all necessary packages in one pass, so you can give all their names to it.
    For example, to install Apache2 and it's documentation, you can do it like this:
        $ sudo apt-get install apache2 apache2-doc

    Or, for example, you want to install all packages which names are started with apache2,
    so you can simply do it using regexp, like this:
        $ sudo apt-get install '^apache2'

    So you can make a script like this:
        sudo apt-get install package1 package2 ...
    and giving it an execution permissions like Morcalavin wrote above.

    PS Also you can do it simply by creating a simple text file, with packages names separated
    with space, just for example, call it 'list' and put all names of necessary packages there.
    For example it can be done like this:
        $ echo package1 package2 > list
    Or you can do it using your favourite text editor.
    Then you can use this file like a packages list for apt-get, for example, like this:
        $ apt_list=$(cat /path/to/list) && sudo apt-get install $apt_list

    And finally, you can easily write a simple script which will be doing everything automaticaly.
    For example like this:
        if [ ! -e $1 ]; then
        aptlist=$(cat $1)
        $aptget install $aptlist
    Then you have to make it executable, and can call it like this:
    $ ./ list.txt
    LVL 1

    Assisted Solution

    I suggest adding -y to the apt-get command line to answer yes to most questions automatically. Very useful when not running interactively.
    LVL 6

    Expert Comment

    In addition to -y option suggested by FilipeMaia I suggest you also to add -q option if it's used in automation mode, so it will produce output suiteable for logging.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now