I am writing a bash script to install a number of packages. Do I need to call apt-get -y update
each time before I call apt-get install <package_name>
? Or is it enough to call apt-get -y update
once at the beginning of the script?
If it needs to be called multiple times, could you explain why?
You need to call
apt-get -y update
if: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
/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.