ChatGPT解决这个技术问题 Extra ChatGPT

Upgrade all the casks installed via Homebrew Cask

I use Homebrew Cask to install applications on OS X. How do I upgrade all the installed casks?

Appended question: "how would I just upgrade one of the casks?"
I would be very interested in that... there does not seem to be any way to upgrade a cask, but it does not make sense. I have Brackets 1.3 installed, and I have installed plugins inside brackets. Now that 1.4 is out, I'd like to upgrad, but keep the plug-ins. I don't see how I am supposed to do that.
Regarding Brackets specifically, user extensions on OS X for Brackets are stored in ~/Library/Application Support/Brackets/extensions/user, and these should persist across upgrades. System plugins are indeed stored within the app bundle in Brackets.app/extensions/default, and these are lost when you replace the app bundle, but the easiest way would just be to copy the plugins from the old bundle to the new one.
FYI: Implement brew cask upgrade merged 15 commits into Homebrew:master
The only way I've found to upgrade a single cask is to brew cask uninstall my-cool-cask then brew cask install my-cool-cask.

r
rofrol

There is now finally an official upgrade mechanism for Homebrew Cask (see Issue 3396 for the implementation)! To use it, simply run this command:

brew upgrade --cask

However this will not update casks that do not have versioning information (version :latest) or applications that have a built-in upgrade mechanism (auto_updates true). To reinstall these casks (and consequently upgrade them if upgrades are available), run the upgrade command with the --greedy flag like this:

brew upgrade --cask --greedy

To get outdated:

brew outdated --cask --greedy --verbose


Thanks, but the answer asked specifically for all the casks. I'll try to make an answer.
@enrico.bacis: Chill. I didn't see your answer, and this is an obvious addition. (And for the record, you should've edited this answer instead of adding a new one.)
This has really irked me so I created this script to update all Brew apps and allow the user to choose which Cask apps to update. You can exclude apps from consideration too. github.com/derrekyoung/ScriptsAndUtils/blob/master/…
You can now use brew cask outdated | xargs brew cask reinstall to only reinstall outdated casks.
It's important to note, that brew cask outdated only prints out names of apps that don't have "auto-update" enabled. To upgrade ALL apps that are outdated, use brew cask outdated --greedy --verbose | grep -v '(latest)' | awk '{print $1}' | xargs brew cask reinstall
A
Atais

homebrew-cask-upgrade

I think this is by far the best solution to upgrade the casks.
source: https://github.com/buo/homebrew-cask-upgrade

Installation & usage

brew tap buo/cask-upgrade
brew update
brew cu

(Optional) Force upgrade outdated apps including the ones marked as latest:

brew cu --all

@XingangHuang yes
e
enrico.bacis

It is possible to list the installed casks with:

brew cask list

And force the re-installation of a cask with:

brew cask install --force CASK_NAME

So piping the output of the first command into the second, we update all the casks:

brew cask list | xargs brew cask install --force

That doesn't really update the casks. It forces reinstallation, even for casks that have no update available. Depending on how many casks you have installed and how long they have been installed, this might trigger unnecessary downloads, take a lot of time and prompt for sudo access.
C
Community

Bash script to upgrade packages

inspired by Pascal answer

#!/usr/bin/env bash

(set -x; brew update;)

(set -x; brew cleanup;)
(set -x; brew cask cleanup;)

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`

casks=( $(brew cask list) )

for cask in ${casks[@]}
do
    version=$(brew cask info $cask | sed -n "s/$cask:\ \(.*\)/\1/p")
    installed=$(find "/usr/local/Caskroom/$cask" -type d -maxdepth 1 -maxdepth 1 -name "$version")

    if [[ -z $installed ]]; then
        echo "${red}${cask}${reset} requires ${red}update${reset}."
        (set -x; brew cask uninstall $cask --force;)
        (set -x; brew cask install $cask --force;)
    else
        echo "${red}${cask}${reset} is ${green}up-to-date${reset}."
    fi
done

What it does

update brew/brew cask, cleanup

read the casks list

check the brew cask info for the newest version

install new version if available (and removes all old versions!)

source: https://gist.github.com/atais/9c72e469b1cbec35c7c430ce03de2a6b

one liner for impatient:

curl -s https://gist.githubusercontent.com/atais/9c72e469b1cbec35c7c430ce03de2a6b/raw/36808a0544628398f26b48f7a3c7b309872ca2c6/cask_upgrade.sh | bash /dev/stdin

save as /usr/local/bin/cask-upgrade, so you can run it locally as cask-upgrade later


Can't you just do brew cask info $cask | grep "Not installed"?
@Det hah... well I did not see it, but I do now. Well I might do it in next version ;-)
$ brew cask info $cask | grep "Not installed" gives the error Error: This command requires a Cask token
@eduncan911 you use it with the script, after the for cask in ${casks[@]}; do
smacks head obviously! can't believe I didn't catch that.
e
enrico.bacis

brew cask upgrade

The upgrade command has recently been introduced in Homebrew Cask and should deprecate all the other manual methods described in the other answers.


B
Bojoer

As of December 2017 use: brew cask upgrade

[DEPRECATED since Dec 2017 when Homebrew introduced upgrade command for cask] I simply use the following:

brew cask outdated | xargs brew cask reinstall


Basically what I do too, except I've been piping to awk '{print $1}' in between the two.
G
Gareth
brew list --cask | xargs brew upgrade

This cycles through all the applications installed by brew cask and upgrades them one at a time.

brew upgrade --cask

no longer works for me.


K
Khushraj Rathod

Here is the function I've written for handling this. Note that I personally didn't want it to just blindly re-install everything since some of the casks I use take a while to install or require additional prompting.

brew_cask_upgrade() { 
  if [ "$1" != '--continue' ]; then 
    echo "Removing brew cache" 
    rm -rf "$(brew --cache)" 
    echo "Running brew update" 
    brew update 
  fi 
  for c in $(brew cask list); do 
    echo -e "\n\nInstalled versions of $c: " 
    ls /opt/homebrew-cask/Caskroom/$c 
    echo "Cask info for $c" 
    brew cask info $c 
    select ynx in "Yes" "No" "Exit"; do  
      case $ynx in 
        "Yes") echo "Uninstalling $c"; brew cask uninstall --force "$c"; echo "Re-installing $c"; brew cask install "$c"; break;; 
        "No") echo "Skipping $c"; break;; 
        "Exit") echo "Exiting brew_cask_upgrade"; return;; 
      esac 
    done 
  done 
} 

I'm running OS X El Capitan, but when I tested this function, I had the shell throwing up errors because the - is not an allowed character in function names. Important distinction - although it works in bash it's disallowed for sh so if you're using #!/bin/sh for compatibility across systems with multiple shells this would not work. Changing the hyphens to underscores works fine, though.
F
Farab Alipanah

Casks with 'auto_updates' or 'version :latest' will not be upgraded; pass --greedy to upgrade them:

brew upgrade --cask --greedy

this one should be the correct answer
N
Nick the Community Scientist

I have used Homebrew for a while now (it's 2022 now), and here is my favorite one line command to run once everyday while I brew my morning coffee. This is excellent if you have all or most of your applications as casks and managed by Homebrew and you like to have the latest updates for new features & security reasons.

Warnings:

DO NOT use in a work environment where reliability and stability is key. Although having constantly the latest security updates sounds like a good idea, what is not a good idea is getting updates as soon as they come out. If you are a software developer, modify this command and remove brew upgrade --greedy. This is because it is always better to inspect the versions of the formulae/casks that are outdated before updating for compatibility with your development environments. You are better off upgrading manually the specific formulae/casks that you are sure will not interfere with your projects, and usually that requires inspecting release notes. When separately updating casks/formulae, use brew upgrade cask_name_here.

Here is the command: brew update && brew outdated --greedy && brew upgrade --greedy && brew cleanup

Let's explain what this does.

The brew update command is used to update Homebrew itself, before we do anything else.

The brew outdated --greedy command is used to list all casks/formulae that have updates available. The greedy parameter specifies that apps that auto update themselves and one's flagged with the version:latest should be included to this listing.

The brew upgrade --greedy command is used to update all casks/formulae which were previously listed as outdated. The greedy parameter specifies that apps that auto update themselves and one's flagged with the version:latest should be included in this update process. Be aware that if you see no output in the terminal after running this command it means that there is nothing to update, unlike the brew outdated command this one does not send a message back to the terminal informing users that nothing needs updating.

The brew cleanup command removes old lock files and outdated downloads for all formulae and casks, and remove old versions of installed formulae. In simpler words, old or leftover files from your previous versions of these casks/formulae.

Potential Solution for Developers & Work Environments If you would like to use this solution as described above and have critical dependencies in Homebrew, there is a solution:

brew pin [package_name_here]

The pin command will stop Homebrew from updating the specified package when all packages are updated with the command above. For more information, here is the pull request where the feature was added to Homebrew.


N
Nathan Smith

brew upgrade --cask $(brew list --cask)


This worked for me!
e
eduncan911

Based on @Atais' answer, I have enhanced his logic into something nicer. I wanted a way to inspect the packages to upgraded first, before actually forcing the upgrade.

$ brew-cask.sh just lists an output similar to Homebrew's brew update.

the list above shows all packages installed, with a green ✔ indicating any pending updates.

$ brew-cask.sh upgrade will force the upgrade of those packages.

Code:

# Usage:
#
#  $ brew update
#    You should execute this first to update everything locally.
#
#  $ brew-cask.sh [update]
#    This will list all of your cask packages and rather there is an upgrade
#    pending with a ✔ checkmark, just like Homebrew does with "brew update".
#    The update command is optional, as it doesn't actually do any tracking, there's
#    not really anything to "update" with cask.  But it keeps with the pattern of
#    of Homebrew's "brew update" pattern for those with memory muscle fingers (like me).
#
#  $ brew-cask.sh upgrade
#    This performs a "brew cask install <cask> --force" of all cask packages that have
#    an update pending.
#
# This code was inspired by http://stackoverflow.com/a/36000907/56693

# get the list of installed casks
casks=( $(brew cask list) )

if [[ "$1" == "upgrade" ]]; then
  for cask in ${casks[@]}; do
    current="$(brew cask info $cask | sed -n '1p' | sed -n 's/^.*: \(.*\)$/\1/p')"
    installed=( $(ls /opt/homebrew-cask/Caskroom/$cask))
    if (! [[ " ${installed[@]} " == *" $current "* ]]); then
      echo "Upgrading $cask to v$current."
      (set -x; brew cask install $cask --force;)
    else
      echo "$cask v$current is up-to-date, skipping."
    fi
  done
else
  echo "Inspecting ${#casks[@]} casks. Use 'brew-cask.sh upgrade' to perform any updates."
  for (( i = i ; i < ${#casks[@]} ; i++ )); do
    current="$(brew cask info ${casks[$i]} | sed -n '1p' | sed -n 's/^.*: \(.*\)$/\1/p')"
    installed=( $(ls /opt/homebrew-cask/Caskroom/${casks[$i]}))
    if (! [[ " ${installed[@]} " == *" $current "* ]]); then
      casks[$i]="${casks[$i]}$(tput sgr0)$(tput setaf 2) ✔$(tput sgr0)"
    fi
  done
  echo " ${casks[@]/%/$'\n'}" | column
fi

just install it (aka "I need it now!")

It's checked into my .dotfiles repo; so, you can install it quickly into your ~/binwith:

$ curl -L https://raw.githubusercontent.com/eduncan911/dotfiles/master/bin/brew-cask.sh --create-dirs -o ~/bin/brew-cask.sh
$ chmod 755 ~/bin/brew-cask.sh

Then use it like so:

$ brew-cask.sh
$ brew-cask.sh upgrade

If you dont have ~/bin in your path, prefix ~/bin/ to the above statements.


g
gbonetti

I think using

brew cask reinstall `brew cask outdated`

will do the trick. This will also help remove the previous version/s of the application and will install the newer version.


this should now be brew cask reinstall `brew cask outdated`
Thanks for the update @gbonetti. You are correct. With the new "outdated" option, the task becomes easier and faster as well :-).
H
Homer

To upgrade all the casks that are not marked as "auto-upgradable"

brew upgrade --cask

To upgrade all the casks ("auto-upgradable" and not "auto-upgradable")

brew upgrade --cask --greedy

As of 2021 brew cask is removed from brews command set. It looks like @Golamrabbi Azad's answer is the current related option.
^ You are right. I've updated this answer with the latest commands.
佚名

improving on the provided code from deinspanjer, I tried to imitate a noop command, much like the one from chocolatey (choco update --noop / choco outdated).

https://git.io/vgjiL

#!/bin/sh

fetch(){
    echo "Removing brew cache" 
    rm -rf "$(brew --cache)" 
    echo "Running brew update" 
    brew update 
}

lookup() { 
  for c in $(brew cask list); do 
    brew cask info $c 
  done 
} 

update(){
  var=$( lookup  | grep -B 3 'Not installed' | sed -e '/^http/d;/^Not/d;/:/!d'  | cut -d ":" -f1)
  if [ -n "$var" ]; then
  echo "The following installed casks have updates avilable:"
  echo "$var"
  echo "Install updates now?"
  select yn in "Yes" "No"; do
    case $yn in
      "Yes") echo "updating outdated casks"; break;;
      "No") echo "brew cask upgrade cancelled" ;return;;
      *) echo "Please choose 1 or 2";;
    esac
    done
  for i in $var; do
    echo "Uninstalling $c"; brew cask uninstall --force "$i"; echo "Re-installing $i"; brew cask install "$i"
  done
else
  echo "all casks are up to date"
fi
}

fetch
update

As one can see, I am using a modular approach since my use case differs a little. I do not want to sit in front of my computer and type yes/no for every app I have installed. While there is no real way of upgrading casks (just the reinstall the newest version), I first do brew update to have the information that there are actually updates available.

Next, I cycle through all the casks to display their information. Because I did brew update before, one is now provided with the information that some cask's latest version is not installed.

Inside my update method, I actually parse the info command for that specific line:

lookup  | grep -B 3 'Not installed' | sed -e '/^http/d;/^Not/d;/:/!d'  | cut -d ":" -f1

Which translates to: "Give the 3 lines above of info provided whenever you read the line "not installed". Then delete any line that has a link in it, also delete a line that has a ':' in it."

Given the structure of the brew cask info command, we end up with one line (no version info, no app URL), which reflects the cask's actual name that it also was installed with.

brew cask info output

In my version, this info is now printed out so one can easily see what casks are out of date and could be updated.

At this point I do a switch case, because maybe right now is not enough time to update things. It depends on your use-case. For me, I sometimes just want to see what's new (waiting for a new version, a bugfix) but don't actually have time to update things because right now I do not want to close my browser etc.

So if one opts for "yes", the list of cleaned names of casks is given to the update function where for each cask that was determined to be out of date the reinstall is issued.

Thanks again to deinspanjer, while trying to solve this issue for myself, I always forgot to issue brew update beforehand so there was no "not installed" line there to actually parse (the foundation of my whole approach).

I hope this was helpful.


P
Pavel Goltsev

I made such script by myself. Please look at the github https://github.com/pesh1983/brew_cask_upgrade. It has pretty good description, but if you have any additional question, feel free to ask me. It does fair upgrade: uninstall and install, so any necessary cleanup will be performed by 'brew' itself.


O
Omar Albeik
brew cask outdated | xargs brew cask reinstall --force

P
Pranab Agarwal

get outdated casks:

brew cask outdated

upgrade cask:

brew cask reinstall outdated-cask

demo script:

$ cat ~/bin/brew_cask_upgrade.sh
#!/bin/bash
red=$(tput setaf 1)
# green=$(tput setaf 2)
reset=$(tput sgr0)

(set -x; brew update;)

for cask in $(brew cask outdated | awk '{print $1}')
do
    echo "${red}update ${cask} ...${reset}."
    (set -x; brew cask install --force "$cask";)
done

echo "${red}brew clean up ...${reset}"
(set -x; brew cask cleanup;)
echo "${red}brew clean up done.${reset}"

A
Amir Marmul

Check outdated casks:

brew cask outdated

Upgrading all outdated cask:

brew cask upgrade

If you want upgrade specific cask, just adding cask-name after upgrade (ex: 4k-video-downloader):

brew cask upgrade 4k-video-downloader


Hi, welcome to StackOverflow. Given that this is an old question with multiple answers one of which is both extremely popular and accepted, please edit your answer to give more detail on how it answers the question and why it is preferable to the existing answers. Thanks.
P
Paul Cav

Based on what i have read i have created a script that will create a file that lists the files to be updated including apps that are defined as latest. You can then modify the file to suit your requirements and install updates using my olinst script.

For more information visit my github.

https://github.com/pacav69/caskroom-offline-install


D
Derrek

This has really irked me so I created this script to update all Brew apps and allow the user to choose which Cask apps to update. You can exclude apps from consideration too.

https://github.com/derrekyoung/ScriptsAndUtils/blob/master/brew-cask-upgrade.sh


m
mcarton

I use

brew cask install --force `brew cask list`

G
Golamrabbi Azad

I work with the fish shell. So I use: brew upgrade (brew list --cask). It works for me.