Open In App

How to Install and Setup Lua in Linux

Last Updated : 06 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Lua is a programming language that is used for scripting, embedded applications, as well as for quite other technologies including plugins and text editors. Lua is a lightweight and open-source programming language it has been used in game development engines like Love 2D cocos2D,  text editors like Neovim, desktop applications like MySQL workbench, WireShark, etc. It is quite easy to learn and start with Lua, so in this article, we will install Lua on a Linux machine. 

Features of Lua

Lua is a simple extensible and portable programming language, it can be used for creating simple as well as dynamic applications with minimal memory footprint. We will look into the major features of Lua as a programming language.

Cross-platform

Lua is cross-platform because its interpreter of compiled bytecode is written in ANSI C. So, Lua has a relatively simple C API that can be embedded into applications. So this feature of Lua allows to run it virtually anywhere making it truly a cross-platform language.

Lightweight

Lua has an extremely simple and thorough source code, making it lightweight and efficient in both performance and robustness. This feature of Lua makes it viable for easily integrating into multiple platforms and has a wide range of applications ranging from embedded applications to game engines.

Dynamic Data types

Since lua is a dynamically typed language like python, javascript, ruby, etc the variable doesn’t have types so to speak but the literal values stored inside of that variable. This makes it extremely easy for rapid prototyping of applications and creating high-level applications without explicit control over memory and the types. 

Efficiency

Lua is considered to be one of the fastest languages among dynamically typed programming languages. The reasons are the mentioned points the light-weightiness of runtime and writing speed. The speed can be even further increased by using the Just In Time compiler for Lua and making efficient use of memory and space. Not only it is faster in terms of runtime speed but it is quite easier to write code in Lua.

Installation

There are just a couple of steps you need to follow in order to install Lua in your system. If you are on Ubuntu or any Debian-based distribution, you can install with the apt package as a single command.

Debian based distributions( e.g. Ubuntu)

sudo apt install lua5.4

 

Arch Based Distributions

sudo pacman -S lua

Fedora

dnf install lua

With brew package manager

brew install lua

You can check for other specific details or build from the source from the documentation given on the official website of Lua

Verify Installation

Now, once the Lua runtime is installed, we can verify the same with the Lua command, if the command opens a REPL it means that the installation was successful.

lua 

 

You can now press CTRL + C to exit out of the repl. Lua ships with its own repl which allows us to write a bunch of Lua code for testing out a few things quickly. Though it can’t save those states, it can be used for quick prototyping of Lua scripts. 

You can also check if Lua is properly installed by checking with the version command,

lua -v

 

This command will give you the installed version of Lua in your system. This also gives an indication the installation of Lua was successful.

Install from Source

We can even build Lua from source using some baked-in binaries and source code which is open source. We can follow the below procedure for installing Lua from the source, the method is roughly similar in various operating systems but the configuration is definitely OS specific.

Install from the source code:

We need to install the source code zip files from the website with the link, here you can choose the version you require and install it as your preference either manual installation and unzipping or installing with wget or curl and unzipping with tar.

wget http://www.lua.org/ftp/lua-5.4.4.tar.gz

OR

curl -R -O http://www.lua.org/ftp/lua-5.4.4.tar.gz

This will create a zip file in your current directory with the name lua-5.4.4.tar.gz or any other version you have installed.

 

Unzip the folder:

tar xvfz lua-5.4.4.tar.gz

 

This will unzip the file and store the source code in a folder called the lua-5.4.4 or any version you might have downloaded, this folder will contain the necessary files for building the Lua runtime.

After the folder has been created, we can change our location and move it into that unzipped folder.  There we need to run the make command and set certain parameters for the installation of the Lua runtime.

cd lua-5.4.4

make linux install  INSTALL_TOP=/usr/local/lua/5_4_4 MYLIBS=”-lncurses”

 

This should build the environment for Lua, and it will be available locally for the folder to run Lua. 

If this didn’t work for you, you can try an alternate installation command with make as it will minimally install the Lua runtime without specifying the installation path and the library to use.

cd lua-5.4.4
make linux test
sudo make install

 

This will give you access to the Lua runtime and thereby you have installed Lua in your system. 

If it isn’t working for you to run Lua from anywhere on your system, there are a few environment variables you can set. Still, there are a few configurations needed for the environment to access the Lua binaries to be able to detect it globally on your system.

Set symbolic links for identifying the binary or header files for Lua runtime. What these below commands will do is set a symbolic link to the binary files for Lua from the current zipped and built binaries in the local directory to the system Lua binaries. This will ensure the Lua binaries are working properly  

ln -s /usr/local/lua/5_4_4/bin/lua /usr/local/bin/

ln -s /usr/local/lua/5_4_4/bin/luac /usr/local/bin/

ln -s /usr/local/lua/5_4_4/include/lauxlib.h /usr/local/include/

ln -s /usr/local/lua/5_4_4/include/lua /usr/local/include/

ln -s /usr/local/lua/5_4_4/include/luaconf.h /usr/local/include/

ln -s /usr/local/lua/5_4_4/include/lua.h /usr/local/include/

ln -s /usr/local/lua/5_4_4/include/lua.hpp /usr/local/include/

ln -s /usr/local/lua/5_4_4/include/lualib.h /usr/local/include/

ln -s /usr/local/lua/5_4_4/lib/liblua.a /usr/local/lib/

Also, you need to add environment variables for the Lua binaries into the bashrc or bash_profile for the proper working of the Lua ecosystem. So, add these lines to your ~/.bash_profile.

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

This will append the lib paths and package configuration of the system and update the environment variables. 

Using the Lua REPL

We can quickly get a view of Lua by using the repl and writing the basic program to understand the syntax of the Lua programming language.

We can use the print function to print text to the console.

print("Hello, Geeks!")

 

This function takes in a string “”, and returns the text to the console, we can also perform quick mathematical and logical operations inside the REPL.

 

So, this is how we can use REPL to use simple and general operations. For further reading, you can view the Lua documentation.

Running Lua scripts

We can use the Lua source file to interpret and run the scripts. So the Lua command can take in a few parameters and one of which is a source file to run.

lua <file_name>.lua

 

So, passing the name of the script will run the code and display the output if any. 

So, this was the installation and a basic overview of the Lua programming language.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads