Lua online compiler
Write Lua 5.5 code below and run it free on the reference Lua interpreter, with no sign-in and no ads. io.read reads the Input tab: io.read("l") for a line and io.read("n") for a number. The same compiler is an API and an embeddable editor, for running Lua in your own app, course or docs.
Também em portuguêsTambién en español
Run Lua from your own app, bot or course. One API call returns the output, errors, exit code and run time. Free up to 100,000 runs a month, commercial use included, no card.
Checking a solution against several inputs? The Lua online compiler with test cases runs them all at once and marks each one passed or failed.
Lua examples: tables, string patterns and metatables
Count the words in the Input tab with io.lines and a gmatch pattern, then sort a table to print the top three.
-- Count the words in the Input tab and print the top three
local counts = {}
for line in io.lines() do
for word in line:lower():gmatch("%a+") do
counts[word] = (counts[word] or 0) + 1
end
end
local words = {}
for word, n in pairs(counts) do
words[#words + 1] = { word = word, n = n }
end
table.sort(words, function(a, b)
if a.n ~= b.n then return a.n > b.n end
return a.word < b.word
end)
for i = 1, math.min(3, #words) do
print(words[i].word, words[i].n)
endWith the cat and the hat and The cat sat on two lines of the Input tab, it prints the 3, cat 2 and and 1, one per line with a tab between.
A class built with a metatable, and an error caught with pcall:
local Account = {}
Account.__index = Account
function Account.new(owner, balance)
return setmetatable({ owner = owner, balance = balance or 0 }, Account)
end
function Account:deposit(amount)
self.balance = self.balance + amount
end
function Account:withdraw(amount)
if amount > self.balance then
error("insufficient funds", 0)
end
self.balance = self.balance - amount
end
local acc = Account.new("Ada", 100)
acc:deposit(50)
print(acc.owner, acc.balance)
print(pcall(acc.withdraw, acc, 500))It prints Ada 150, then false insufficient funds, again with tabs between.
What runs your code
| Version | Lua 5.5 |
|---|---|
| Compiled with | No separate step: the run command compiles or interprets it |
| Run with | lua code.lua |
| Limits | 30 seconds, 512 MB of memory (up to 2 GB through the API on Pro and above), 2 CPUs, 64 KB of output per stream, no network |
| Typical run | 0.56 s, and 0.63 s for the slowest 1 in 20 (how we measured) |
Good to know
- Runs with the reference interpreter, Lua 5.5.
io.read("l")reads the next line of the Input tab, andio.read("n")reads a number.- Lua 5.5 makes the variable of a
forloop read-only, so Lua 5.4 code that assigns to it needs a local copy.
Lua compiler API
To run Lua from your own app, send the code and its input to the sync endpoint with an API key, and the output comes back in the response. It's free up to 100,000 runs a month, with no credit card: get a free API key. The API docs cover the async endpoint with callbacks and every response field.
curl -X POST https://api.onlinecompiler.io/api/run-code-sync/ \
-H "Authorization: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"compiler": "lua-5.5",
"code": "local name = io.read(\"l\")\nprint(\"Hello, \" .. name .. \"!\")",
"input": "Ada"
}'The response has output ("Hello, Ada!\n" here), error, exit_code, time and memory.
Embed a Lua compiler in your website
The editor above is the embeddable widget. Add the script and a div with your widget key, and your readers can edit and run the code without an account:
<script src="https://onlinecompiler.io/widget.js"></script>
<div class="runcode" data-key="YOUR_WIDGET_KEY" data-lang="lua-5.5">
<script type="text/plain">
local name = io.read("l")
print("Hello, " .. name .. "!")
</script>
<a data-credit href="https://onlinecompiler.io/">Runs on OnlineCompiler.io</a>
</div>On a platform that takes an iframe but not a script, such as many course sites, use the iframe embed. For Docusaurus, MkDocs, Sphinx and Mintlify, see runnable code examples in docs.
Other languages
Python, C, C++, Java, C#, F#, Go, Rust, PHP, Ruby, Haskell, TypeScript, JavaScript, SQL, DuckDB, Bash, Fortran, Visual Basic, Kotlin, R, Swift, COBOL, Perl, Julia, Pascal, Octave, Assembly, OCaml, Common Lisp, Erlang, Elixir, Prolog, D, FreeBASIC, Dart, Clojure, Groovy, Scala, Zig, Nim, Crystal, Ada, PowerShell, Racket, Objective-C.
Frequently asked questions
Is this Lua online compiler free?
Yes. Running Lua on this page needs no account and shows no ads. To run Lua from your own app or site, the API and the widget are free up to 100,000 runs a month, commercial use included.
Is there a Lua compiler API?
Yes. POST the code, the compiler id lua-5.5 and any input to https://api.onlinecompiler.io/api/run-code-sync/ with an API key, and the response holds the output, errors, exit code, run time and memory. It's free up to 100,000 runs a month, and the same key works for the other 45 languages.
Can my Lua program read input?
Yes. Type it in the Input tab before you run, and the program reads it with io.read: io.read("l") reads a line, io.read("n") reads a number, and io.lines() loops over every line. The whole input is sent when you press Run, so a program can't ask for more while it runs.
Can I use Lua libraries?
Lua's standard libraries: string, table, math, io, os, utf8 and coroutine. LuaRocks modules can't be installed, and programs have no network access.
Is this a Lua compiler or an interpreter?
Both, the way Lua itself works. The lua command compiles the whole file to bytecode, then runs it on Lua's virtual machine, so a syntax error is reported before any line runs, such as code.lua:2: unexpected symbol near '='. There is no separate luac step to run.
Does Roblox Luau code run here?
Only the plain Lua parts. This is the reference Lua 5.5, so Luau's type annotations, such as local count: number, and compound assignments, such as count += 1, are syntax errors, and Roblox's game and workspace objects aren't there.
Can I share or save my code?
Yes. Share gives you a link that anyone can open and run, lasts 90 days and needs no account. Save keeps the code in your OnlineCompiler.io account until you delete it. When you are signed in, each run you make on this page is also kept in your account's run history.
How long can a program run?
Up to 30 seconds, with 512 MB of memory (up to 2 GB through the API on Pro and above) and 2 CPUs. A program still running at 30 seconds is stopped with exit code 124, and output over 64 KB per stream is cut.
Where does my code run?
On our servers, in a fresh gVisor sandbox with no network access that is deleted when the run ends. Each run is kept in execution history with its output; the security page says what is stored.