Lua is a free and open-source, powerful, robust, minimal, and embeddable scripting language. It’s extensible and interpreted scripting language that is dynamically typed, and run by interpreting bytecode with a register-based virtual machine.
Lua runs on all if not most Unix-like operating systems including Linux and Windows; on mobile operating systems (Android, iOS, BREW, Symbian, Windows Phone); on embedded microprocessors (ARM and Rabbit); on IBM mainframes, and many more.
See how Lua programs work in the live demo.
Lua Features:
- Builds on all systems with a standard C compiler.
- It’s remarkably lightweight, fast, efficient, and portable.
- It’s easy to learn and use.
- It has a simple and well-documented API.
- It supports several types of programming (such as procedural, object-oriented, functional, and data-driven programming as well as data description).
- Implements object-oriented via meta-mechanisms.
- It also brings together straightforward procedural syntax with formidable data description constructs rooted around associative arrays and extensible semantics.
- Comes with automatic memory management with incremental garbage collection (thus making it perfect for real-world configuration, scripting, and also breakneck prototyping).
How to Install Lua in Linux
Lua package is available in official repositories of major Linux distributions, you can install the latest version using the appropriate package manager on your system.
------- On Debian, Ubuntu & Mint ------- $ sudo apt install lua5.3 ------- On RHEL, CentOS, Rocky & AlmaLinux ------- # yum install epel-release # yum install lua ------- On Fedora Linux ------- # dnf install lua
Note: The current version of the Lua package in the EPEL repository is a little older, therefore to install the latest release, you need to build and install it from the source as explained below.
Install Lua from Sources
First, ensure that you have development tools installed on your system, otherwise, run the command below to install them.
------- On Debian, Ubuntu & Mint ------- $ sudo apt install build-essential libreadline-dev ------- On RHEL, CentOS, Rocky & AlmaLinux and Fedora ------- # yum groupinstall "Development Tools" # yum install readline readline-devel
Then to build and install the latest release (version 5.4.4 at the time of this writing) of Lua, you need to download the lua source file or run the following commands to download the package tarball, extract, build and install it.
$ mkdir lua_build $ cd lua_build $ curl -R -O http://www.lua.org/ftp/lua-5.4.4.tar.gz $ tar zxf lua-5.4.4.tar.gz $ cd lua-5.4.4 $ make linux test $ sudo make install
Once you have installed it, run Lua interpretor as shown.
$ lua Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio >
Using your favorite text editor, you can create your first Lua program as follows.
$ vi hello.lua
And add the following code to the file.
print("Hello World") print("This is Tecmint.com and we are testing Lua")
Save and close the file. Then run your program as shown.
$ lua hello.lua
For more information and to learn how to write Lua programs, go to: https://www.lua.org/home.html
Lua is a versatile programming language being used in numerous industries (from the web to gaming to image processing and beyond), and it’s designed with a high priority for embedded systems.
If you encounter any errors during installation or simply want to know more, use the comment form below to send us your thoughts.
Thanks for sharing…
How do I remove this Lua installation?
I want to upgrade to the newest 5.4.4 Lua on Linux Mint 20.3
@Aum,
I have updated the instructions to install the latest Lua in Linux systems.
How exactly do you add Lua to conky? I am a newbie and therefore need step-by-step instructions.
For those who need a Windows solution, I found a good source that installs Lua 5.1, 5.2, and 5.3, plus LuaRocks for all three. It’s current, and actively maintained. It’s called “Multi-Lua for Windows” and can be found on GitHub: https://github.com/Tieske/luawinmulti
You’ll need to install a C compiler, such as MinGW, as this project compiles everything from source, Lua and Luarocks alike.
Rather than running a Lua interpreter in its own shell window, this Lua runs in the built-in Windows Command Shell window.
Suggestion: After installing Lua, install “LuaRocks“, which is Lua’s package repository (similar to CPAN for Perl and PyPI for Python).
Lua was designed to be embedded and extensible, so it depends on external libraries for a number of important functions that are internal to most other languages. This includes libraries for working with the host file system, for socket I/O (networking), etc., plus things like unit testing, logging, and so forth.
Lua’s repo packages are called “rocks” (Lua rocks… Moon rocks… get it?), so you can search for, install, update your “rocks” using LuaRocks. LuaRocks installs separate local package caches for each Lua version.
Lua rocks are typically coded in
C
, so they need to be compiled from source by LuaRocks in order to be installed. This is usually not an issue for Linux hosts, but for Windows installations you’ll need to have aC
compiler installed. I use MinGW for this on my Win platforms.Finally, there are two ways to install LuaRocks. I suggest the “bootstrapping” method, which installs LuaRocks as a Lua rock. The advantage of this method is that LuaRocks can be updated in the future by asking LuaRocks to update itself: “
luarocks install luarocks
“.@Brainstorms
Thanks a lot for sharing this. We are grateful for your contribution.
This is what worked for me on Linux Mint 19:
@Benji,
Thanks for sharing the instructions, hope it will help Mint users..
Did you need Lua 5.3.4 for a particular reason? Linuxmint 19 is based on Ubuntu Bionic, which has Lua 5.0, 5.1, 5.2, and 5.3 in its repositories. The repo version for 5.3 is 5.3.3, however. Is there a significant fix for 5.3.4?
I want to know how to do ssh with lua to a remote server and run tcpdump command process in background on remote server.
@Leema
You may consult the Lua documentation.
More Lua features: full lexical closures, co-routines, and multiple return values. There are only 8 data types: nil, Number, String, Boolean, Table, Userdata, Function, and Thread. Any type can be assigned to a variable, and all types are first-class.
All structured data is made from Tables (associative arrays). Data types and instances of Table/Userdata can have “meta-Tables” that provide powerful extensibility (including object-oriented structure & behavior). Has a very nice C API which helps make it both embeddable and extensible. Nearly a linguistically/semantically perfect language…
@Brainstorms
This is great, we are grateful to you, for sharing this.
I use devilspie2 to embed various things like terminals, browsers, or media players into my desktop, and devilspie uses Lua for configuration. Straight forward and great documentation.
@Jeff
Many thanks for sharing this useful information.