Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a list of applications with brew?

Tags:

bash

homebrew

I want to create a script to force update brew applications that are outdated.

I tried something like that, but it's not working:

var=$(brew cask outdated); for f in $var; do "brew upgrade $var --force"; done

I am a beginner in bash, I think what I'm searching is easy, but i need help.

like image 541
PxL Avatar asked Jul 15 '17 08:07

PxL


2 Answers

2021 update

During 2020 the sub-commands of the cask command were gradually integrated into the core of Homebrew and were deprecated. The core commands now operate on both formulae and casks. To limit their scope to only formulae or casks add the --formula or --cask option to the command line.

The cask command has been completely removed from Homebrew in version 2.6.0, released in December 2020.

There is a unified flow now for both formulae and casks:

brew update
brew outdated
brew upgrade

The first command (brew update) gets from the Homebrew servers the information about the most recent versions of the available formulae and casks.

The second command (brew outdated) lists the outdated installed formulae and casks. Add --formulae or --casks to the command line to limit the command scope to formulae or casks. Add --greedy to the command line to include in its output the casks that are marked as "auto-updateable".
This command is not needed. It can be used to see what programs will be upgraded before doing the actual upgrade.

The third command (brew upgrade) upgrades the packages listed by brew outdated. It also supports the options listed above for brew outdated and operates accordingly.


The original answer and the 2018 update are kept for historical reasons.


2018 update

In the meantime, the brew cask upgrade command has been implemented and can be used instead of brew cask reinstall in the script provided in the original answer.

It also works without arguments and upgrades all the casks displayed by brew cask outdated. It can replace the for block in the script that now becomes:

# Upgrade Homebrew and the information about formulas
$ brew update
# List the outdated casks (optional)
$ brew cask outdated
# Upgrade all outdated casks
$ brew cask upgrade

Additional info

Sometimes, brew cask outdated does not list all the outdated casks. This is by design. Some casks mark themselves as auto-updateable while others use the keyword latest as their version. These two categories of casks are not listed by brew cask outdated. To list them one have to add the --greedy option in the command line of brew cask outdated:

$ brew cask outdated --greedy

This command includes in its output the casks mentioned above. Those having latest as their version cannot be checked and are always listed, those marked as auto-updateable are checked and listed only if they are outdated indeed.

The command brew cask upgrade also accepts the --greedy option (internally it uses brew cask outdated to find the list of packages to upgrade). Using it ensures your system is up to date with the additional cost of reinstalling some applications that are not outdated.


The original answer (July 2017)

You are mixing brew commands with brew cask commands and it doesn't work this way.

Homebrew Cask is a component of Homebrew that allows management of graphical applications. It started as an extension of the original Homebrew; it is now an integrated command of brew. The Homebrew itself manages only command line applications.

Upgrading command line programs

Upgrading all outdated programs installed with brew (command line programs) is easy:

# Tell Homebrew to update itself and its taps (repositories of programs)
$ brew update
# List the outdated programs
$ brew outdated
# Upgrade all the outdated programs and their dependencies
$ brew upgrade

The brew update and brew outdated steps are optional. brew upgrade internally calls brew update if the last update happened too much time in the past. brew outdated only lists the outdated installed programs; it is useful if you do the upgrade manually and you don't want to upgrade everything. In this case you can pick the programs you want to upgrade and run brew upgrade <program> for each program you want to update. Replace <program> with the package name displayed by brew outdated.

Upgrading graphical programs

Upgrading all outdated graphical programs is not that straightforward at the moment. The process is slightly different than the above.

The first step is to run brew update. It updates Homebrew code and the taps (a tap is a repository of programs managed by Homebrew). Homebrew Cask provides the sub-command brew cask update but it is just an alias of brew update. You can use any of them; brew update is preferred because it is shorter.

The second step is to run brew cask outdated. This command shows the list of outdated programs managed by Cask, together with the installed version and the new version. The option --quiet tells it to show only the package names, without versions. You'll need this for automation on the third step.

The next paragraphs are deprecated since 2018, when the command brew cask upgrade has been implemented. The new flow is described in the section "2018 update" above.

Unfortunately there is no brew cask upgrade command at this moment (it is planned but not implemented yet). brew upgrade doesn't know how to handle the programs installed by brew cask either.

brew cask reinstall can be used instead of the missing brew cask upgrade. Unlike brew upgrade, that upgrades all the outdated packages, brew cask reinstall requires the name of one and only one package. This requires looping through the list returned by brew cask outdated.

A short shell script that upgrades all outdated programs installed using Homebrew Cask can look like this:

brew update
for i in $(brew cask outdated --quiet); do
    brew cask reinstall $i
done

Drawbacks

There are programs that require administrator rights to install drivers or other system components. Some of them ask for your password or for various confirmations using a graphical dialog box. brew cask itself sometimes asks for your password in the command line.

All these make upgrading the outdated programs automatically not always possible.

You may think that all these can be circumvented using sudo. However, the Homebrew developers know better and they programed the brew command to display a message and exit when it is executed as root (and this is the right way to do it.)

like image 117
axiac Avatar answered Oct 03 '22 15:10

axiac


In 2020

axiac's response is basically perfect. Just to note that brew cask upgrade is disabled in the latest versions of Homebrew, so you should use brew upgrade --cask instead.

like image 44
よまる Avatar answered Oct 03 '22 14:10

よまる