Install Lua from Source Code on Ubuntu 20.04

Install Lua from Source Code on Ubuntu 20.04

Lua is a high-level, dynamically typed programming language that designed for embedded use in applications. Lua is often used as scripting language for game development.

This tutorial shows how to install Lua from source code on Ubuntu 20.04.

Prepare environment

Make sure you have installed build-essential package in your system. It will used to compile Lua from source code.

sudo apt update
sudo apt install -y build-essential

Compile Lua

Get the latest version tag of Lua release from GitHub and assign version tag to variable.

LUA_VERSION=$(curl -s "https://api.github.com/repos/lua/lua/releases/latest" | grep -Po '"tag_name": "v\K[0-9.]+')

Navigate to home directory and download Lua source code.

cd ~
curl -o lua.tar.gz "http://www.lua.org/ftp/lua-${LUA_VERSION}.tar.gz"

Extract source code from archive to newly created directory.

mkdir lua-src
tar xf lua.tar.gz --strip-components=1 -C lua-src

Next, navigate to the Lua source directory.

cd lua-src

Run the following command to compile Lua:

make -j$(nproc)

Install Lua

We will create Debian package and install Lua from it. Later, we can easily remove Lua.

Install Lua to temporary directory:

make install INSTALL_TOP=~/lua/usr/local

Navigate to home directory and create DEBIAN directory to store package control file.

cd ~
mkdir lua/DEBIAN

Run the following commands to create control file and add content to it:

echo "Package: lua" >> lua/DEBIAN/control
echo "Version: ${LUA_VERSION}-1" >> lua/DEBIAN/control
echo "Architecture: amd64" >> lua/DEBIAN/control
echo "Maintainer: $USER@$HOSTNAME" >> lua/DEBIAN/control
echo "Description: Lua programming language" >> lua/DEBIAN/control

Create Debian package:

dpkg-deb --build lua lua.deb

Install Lua:

sudo apt install -y ./lua.deb

After installation, we can check Lua version:

lua -v

Remove unnecessary directories and files:

rm -rf lua.tar.gz
rm -rf lua-src
rm -rf lua
rm -rf lua.deb

Testing Lua

Create a main.lua file:

nano main.lua

Add the following line to it:

print('Hello world')

Test a program:

lua main.lua

Uninstall Lua

You can completely remove Lua using the following command:

sudo apt purge --autoremove -y lua

You can also remove build tools:

sudo apt purge --autoremove -y build-essential cpp make binutils

Leave a Comment

Cancel reply

Your email address will not be published.