How to reliably check OS version?


I'm working on a package that needs to run slightly differently depending on different operating systems, and now versions of OS by the looks.

I'm wondering what the best way to check for different OS versions is?
I know that I can find some System information using and various combinations of the output from that. Is this the best (most reliable) way to get the OS version? I'm especially interested in detecting when a machine is running macOS Monterey. It seems that["release"] gives "21.x.x" for macOS Monterey, and "20.x.x" for Big Sur, but I have limited access to macOS, so it's difficult for me to test this.


sessionInfo() returns as its third line

running under: Pop!_OS 21.10

on the box I'm logged in with. It returns

running under: macOS Big Sur 10.16

on my difficult-to-migrate Airbook


Running under: macOS Monterrey 12.1

on my current Mactop

Yes, this is the best way, according to my knowledge. See here for the corresponding macOS versions: Darwin (operating system) - Wikipedia

(FWIW this is not the same as the OS that R was compiled for, which is typically older, that is in R.Version()$os and it might be missing the trailing zero versions, so on arm64 mac and R 4.1.x it is just darwin20.)

sessionInfo() is not reliable because if the R version is older than the macOS version you are running it on, it typically gives wrong results. E.g. these are for the same OS, and they are all mostly wrong, maybe you can consider the last one to be correct:

❯ R-3.4 -q -e 'sessionInfo()$running'
> sessionInfo()$running
[1] "macOS  10.16"

❯ R-4.1 -q -e 'sessionInfo()$running'
> sessionInfo()$running
[1] "macOS Big Sur 10.16"

❯ R-4.2 -q -e 'sessionInfo()$running'
> sessionInfo()$running
[1] "macOS Big Sur/Monterey 10.16"
1 Like

Thanks for the confirmation Gabor!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.