You need to call apt-get -y update
if:
- It has not been called for a long time (e.g. in the last 24h)
- If the list of repositories has changed since the last update
For the first point: obviously in a simple case it would be sufficient to call update just once at the beginning. In a more complicated script it may be useful to call an internal update procedure before each call to apt-get -y install
and inside of this internal procedure you would automatically detect if the last update was run a long time ago and needs to be executed. See How to know last time `apt-get update` was executed?
For the second point: If you have a more complicated scenario and your script may possibly add repositories between the calls to install the packages, you may want to check the last modification dates of
/etc/apt/sources.list
- all files in
/etc/apt/sources.list.d/
in order to determine if new repositories were added after the last call of apt-get update
, and if it needs to be called again in that case.