How do I find out the number of packages available for update?

opkg:

opkgUpgradable="$(opkg list-upgradable 2> /dev/null | wc -l)" 
echo "$opkgUpgradable packages can be upgraded."

put this code in /root/.profile.

Every time I log in via SSH, I can see how many packages are available for update, if the count is 0, I don't need to run the update command.

How should I write the code when working with apk?

Upgrading packages (via the CLI opkg upgrade/apk upgrade commands or the LuCI Upgrade... button) can result in major problems. It is generally highly discouraged, unless you know what you are doing or if there is specific instruction to do so.

$ echo "Packages out of date: $(( $(apk update >/dev/null && apk version | wc -l) - 1 ))"
Packages out of date: 22

And to reiterate what @psherman said, don't use apk upgrade.

I just remembered that apk won't tell you when there's an upgrade available on the same branch, so its reporting is only for currently installed kernel. Same device, but using owut check shows 69 packages available, because it checks kernel, libc, kmods...

$ owut check
Upstream       https://downloads.openwrt.org
Target         mediatek/filogic
Profile        zyxel_nwa50ax-pro
Package-arch   aarch64_cortex-a53
Version-from   25.12.3 r32912-6639b15f62 (kernel 6.12.85)
Version-to     25.12.4 r32933-4ccb782af7 (kernel 6.12.87)
69 packages are out-of-date
There are 0 missing and 1 modified default packages
It is safe to proceed with an upgrade (re-run with '--verbose' for details)

I am using an x86-based system equipped with two mSATA drives. If an update goes awry, I can simply swap back to the original, working mSATA drive. I just want to know how many packages are available for updating today.

Thank you and psherman too. I will try carefully.

I remember flashing Openwrt on WZR-HP-AG300H and WHR-G300N V2 quite a while ago?

Then Openwrt -> LEAN-> Openwrt.

Now I use Image Builder to builde my ROM.

If I use

owut check

Then returrn "It is safe to proceed with an upgrade" , means I can run

apk upgrade

?

Current stable release - OpenWrt 25.12.4

run 25.12.4 Image Builder now :grinning_face:

You're already on the latest and greatest, what are you upgrading to ?

Correct, that's owut check's purpose in life.

EDIT
Don't use apk upgrade, use owut upgrade...

Still 25.12.4 but with all the newest packages from the rolling package builds...

EDIT
In other words, safe apk upgrade...

Wouldn't it be the better / safer way to run owut upgrade
instead of apk upgrade?

You are absolutely correct, I missed that completely.

NO apk upgrade
YES owut upgrade

As efahl said, if I install some packages, such as

adblock
banip
luci-app-adblock
luci-app-banip
luci-i18n-adblock-***
luci-i18n-banip-***

They will continue to be updated.

Like you installed Ubuntu 26.04 does not mean that all packages are up to date; there will still be packages requiring updates, some of them may be security patches.

First to upgrade always use 'owut upgrade'

Security problems are usually in the base files (which includes the kernel and packages for basic operation e.g. DNSMasq) and those are only updated with a point release. The updates you see are from the add-on packages which are on a rolling release but these packages seldom have security problems, it is mostly about bugfixes and enhancements.
So there could be daily updates for those packages but that is seldom about severe security problems (to add to the confusion those packages can often be updated individually but unless you really know that or instructed to do so do not do that but use owut upgrade)

OpenWRT will issue a new point release in case of severe security problems, so if you are always on the latest point release, as of now 25.12.4, you should be good security wise.

root@OpenWrt:~# owut check
ASU-Server     https://sysupgrade.openwrt.org
Upstream       https://downloads.openwrt.org
Target         x86/64
Profile        generic
Package-arch   x86_64
Version-from   25.12.4 r32933-4ccb782af7 (kernel 6.12.87)
Version-to     25.12.4 r32933-4ccb782af7 (kernel 6.12.87)
2 packages are out-of-date
There are 0 missing and 2 modified default packages
It is safe to proceed with an upgrade (re-run with '--verbose' for details)
one is "banIP"

then

root@OpenWrt:~# owut upgrade
ASU-Server     https://sysupgrade.openwrt.org
Upstream       https://downloads.openwrt.org
Target         x86/64
Profile        generic
Package-arch   x86_64
Version-from   25.12.4 r32933-4ccb782af7 (kernel 6.12.87)
Version-to     25.12.4 r32933-4ccb782af7 (kernel 6.12.87)
2 packages are out-of-date
There are 0 missing and 2 modified default packages
Request hash:
  3443dde302e265808d01c623203a6ce39c3cc07327df15296cc4e2b8ebc7815b
--
Status:   queued - 0 ahead of you
Progress:   0s total =   0s in queue +   0s in build
--
Status:   container_setup
Progress:   1s total =   0s in queue +   1s in build
--
Status:   validate_manifest
Progress:  42s total =   0s in queue +  42s in build
--
Status:   building_image
Progress: 204s total =   0s in queue + 204s in build
--
Status:   done
Progress: 208s total =   0s in queue + 208s in build

Build succeeded in 208s total =   0s in queue + 208s to build:
Image saved : /tmp/firmware.bin
Installing /tmp/firmware.bin and rebooting...
root@OpenWrt:~# Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(192.168.1.1) at 11:48:55.

Type `help' to learn how to use Xshell prompt.
[e:\~]$ 

Connecting to 192.168.1.1:22...

Then I can not connect roter.

Now I switch to old mstat(run 24.10.6)

I have never encountered a scenario where a package automatically installs immediately after compilation. However, I have experienced boot failures caused by installing or updating packages via opkg with a problem I’ve faced ever since I started using OpenWrt on my Buffalo WZR-G300NH(2013?).

Other Linux distributions also check which library files need updating when updating packages, and ultimately ask the user whether to reboot immediately—it isn't done automatically. Is this the "risk" you were referring to? If so, does that mean I need to create a backup every time I perform an update, and then use imgbuilder to generate a new image and flash it? I'm finding it a bit hard to follow.

Yes that is correct but you can also use owut to update which does the same for you so it creates a new image with updated packages and installs it, so much more convenient then doing it yourself with the imagebuilder but it amounts to the same.