Subject says it all. I would like to know if my host interpreter is running Lua 5.2 or 5.1
Asked
Active
Viewed 2.4k times
23
-
1Ah ... there is a global variable called _VERSION: – henryaz Apr 27 '13 at 21:58
-
World of Warcraft (8.2 Battle for Azeroth): `5.1` – Ian Boyd Jul 10 '19 at 12:03
3 Answers
34
There is global variable _VERSION (a string):
print(_VERSION)
-- Output
Lua 5.2
UPD :
Other methods to distinguish between Lua versions:
if _ENV then
-- Lua 5.2
else
-- Lua 5.1
end
UPD2 :
--[=[
local version = 'Lua 5.0'
--[[]=]
local n = '8'; repeat n = n*n until n == n*n
local t = {'Lua 5.1', nil,
[-1/0] = 'Lua 5.2',
[1/0] = 'Lua 5.3',
[2] = 'LuaJIT'}
local version = t[2] or t[#'\z'] or t[n/'-0'] or 'Lua 5.4'
--]]
print(version)

Egor Skriptunoff
- 23,359
- 2
- 34
- 64
-
1
-
4
-
I think you meant `Lua 5.2 or greater`, as `Lua 5.3` also has the `_ENV` global variable. – jeromej Jan 17 '16 at 04:22
-
2
-
1[More](http://lua-users.org/lists/lua-l/2016-05/msg00297.html) about detecting Lua version – Egor Skriptunoff Sep 26 '16 at 20:28
-
I think `9^33 == 27^22` is true even on LuaJit (At least on LuaJit 2.0.4). – Royi Jun 29 '18 at 15:57
-
@Royi - The result depends on whether 64-bit double (LuaJIT) or 80-bit extended (vanilla Lua) is used for calculation. – Egor Skriptunoff Jun 29 '18 at 21:45
-
I just tried it on the latest Zero Brane Studio which uses LuaJit 2.0.4. It yields: `9^33 == 27^22 true print(jit.version) LuaJIT 2.0.4`. I followed https://stackoverflow.com/questions/20335340. – Royi Jun 29 '18 at 21:58
-
One more crazy check: `if 0x123456789 == 0x987654321 then --Lua 5.1` – Egor Skriptunoff Jul 12 '18 at 08:03
-
[Now with Lua 5.4 support](http://lua-users.org/lists/lua-l/2020-07/msg00323.html) – Egor Skriptunoff Jul 25 '20 at 18:20
3
_VERSION
holds the interpreter version. Check the manual for reference.

jbochi
- 28,816
- 16
- 73
- 90

Tom Regner
- 6,856
- 4
- 32
- 47
3
If you also need the third digit in Lua version (not available in _VERSION
) you need to parse the output of lua -v
command on the command line.
For platforms that support io.popen this script will do the trick, but only if the script is run by the standalone interpreter (not in interactive mode).IOW the arg
global table must be defined:
local i_min = 0
while arg[ i_min ] do i_min = i_min - 1 end
local lua_exe = arg[ i_min + 1 ]
local command = lua_exe .. [[ -v 2>&1]] -- Windows-specific
local fh = assert( io.popen( command ) )
local version = fh:read '*a'
fh:close()
-- use version in the code below
print( version )
print( version:match '(%d%.%d%.%d)' )
Note that lua -v
writes on stderr
on Windows (for Linux I don't know), so the command
for io.popen (which only captures stdout
) must redirect stderr
to stdout
and the syntax is platform-specific.

LorenzoDonati4Ukraine-OnStrike
- 6,875
- 4
- 37
- 56
-
The third digit identifies different bug-fix releases with no change of functionality. It seems pointless to test those. – lhf Aug 19 '13 at 00:32
-
@lhf in general I fully agree, but for special needs it is a trick useful to know. E.g. say you want to discover if your script is run by an interpreter that has a particular patch applied or it is an older one (maybe you must run the script on a system not under your control, so you don't know whether a workaround in the code must be used or not). But I admit it is a bit on the nitpicker's side :-) – LorenzoDonati4Ukraine-OnStrike Aug 19 '13 at 00:43
-
On Linux '`lua -v`' writes to `stdout` with Lua 5.2 but to `stderr` with Lua 5.1, but since the syntax for the redirection is the same in the (Bourne) shell, you can use the same command string on Linux and Windows (and almost certainly on OS X too). – Chris Arndt Sep 27 '14 at 14:33