1

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?

James Newton
  • 1,145
  • 1
  • 17
  • 31

1 Answers1

4

You need to call apt-get -y update if:

  1. It has not been called for a long time (e.g. in the last 24h)
  2. 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.

Zanna
  • 72,312