Table of Contents
In this article, I will take you through the steps to install Lua Programming language on Ubuntu 20.04 LTS. Lua is an open source, lightweight, high level programming language developed specifically for embedded use in applications. It is built on top of C programming language. Lua is dynamically typed programming language that runs by interpreting bytecode with a register-based virtual machine. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description.
The other important features of Lua programming language includes:-
- It is easy to use on any platform
- Automatic memory management with a generational garbage collection
- Ideal for configuration, scripting, and rapid prototyping
- No notion of main program
- Works embedded in a host client
- Does not use any global variables. Keep all information it needs in a dynamic structure.
How to Install Lua Programming Language on Ubuntu 20.04 LTS
Step 1: Prerequisites
a) You should have a running
Ubuntu 20.04 LTS System.
b) You should have
root access to run privileged commands.
c) You should have
apt utility available in your System.
Step 2: Update Your Server
In the first step, you need to update the System packages to the latest available version by using
sudo apt update command as shown below. This will sync the system packages with the latest versions available from the Ubuntu repo.
cyberithub@ubuntu:~$ sudo apt update [sudo] password for cyberithub: Hit:1 https://download.docker.com/linux/ubuntu focal InRelease Get:2 https://dl.google.com/linux/chrome/deb stable InRelease [1,811 B] Hit:3 http://in.archive.ubuntu.com/ubuntu focal InRelease Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Hit:5 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu focal InRelease Get:6 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:7 https://dl.google.com/linux/chrome/deb stable/main amd64 Packages [1,077 B] Hit:8 https://apt.boltops.com stable InRelease Get:9 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1,746 kB] Get:10 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB] Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2,121 kB] Get:12 http://security.ubuntu.com/ubuntu focal-security/main i386 Packages [497 kB] Get:13 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [727 kB] ..................................................
If any of the packages needs to be upgraded then run
sudo apt upgrade command as well.
cyberithub@ubuntu:~$ sudo apt upgrade Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following packages will be upgraded: google-chrome-stable 1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 92.1 MB of archives. After this operation, 2,305 kB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 https://dl.google.com/linux/chrome/deb stable/main amd64 google-chrome-stable amd64 106.0.5249.61-1 [92.1 MB] Fetched 92.1 MB in 16s (5,815 kB/s) (Reading database ... 258675 files and directories currently installed.) Preparing to unpack .../google-chrome-stable_106.0.5249.61-1_amd64.deb ... Unpacking google-chrome-stable (106.0.5249.61-1) over (105.0.5195.125-1) ... Setting up google-chrome-stable (106.0.5249.61-1) ... Processing triggers for mime-support (3.64ubuntu1) ... Processing triggers for gnome-menus (3.36.0-1ubuntu1) ... Processing triggers for man-db (2.9.1-1) ... Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
Step 3: Install Lua
There are multiple ways to install Lua Interpreter on Ubuntu 20.04 LTS system. You can choose any of the below method depending on the lua version you require in your System.
a) Using apt
You can choose to install lua interpreter by using
apt package manager from default Ubuntu repo. But it might be possible that Ubuntu repo does not always have the latest version available. So if you are not worried about the lua version, then you can simply install the package from default Ubuntu repo by using
sudo apt install lua5.3 command as shown below.
cyberithub@ubuntu:~$ sudo apt install lua5.3 Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: lua5.3 0 upgraded, 1 newly installed, 0 to remove and 9 not upgraded. Need to get 110 kB of archives. After this operation, 414 kB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu focal/universe amd64 lua5.3 amd64 5.3.3-1.1ubuntu2 [110 kB] Fetched 110 kB in 1s (95.4 kB/s) Selecting previously unselected package lua5.3. (Reading database ... 258675 files and directories currently installed.) Preparing to unpack .../lua5.3_5.3.3-1.1ubuntu2_amd64.deb ... Unpacking lua5.3 (5.3.3-1.1ubuntu2) ... Setting up lua5.3 (5.3.3-1.1ubuntu2) ... update-alternatives: using /usr/bin/lua5.3 to provide /usr/bin/lua (lua-interpreter) in auto mode update-alternatives: using /usr/bin/luac5.3 to provide /usr/bin/luac (lua-compiler) in auto mode Processing triggers for man-db (2.9.1-1) ...
b) Using Source code
If you are looking to install the latest available version, then you can visit the official website and download the latest version source code using
curl utility as shown below.
cyberithub@ubuntu:~$ curl -R -O http://www.lua.org/ftp/lua-5.4.4.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 352k 100 352k 0 0 196k 0 0:00:01 0:00:01 --:--:-- 196k
After downloading the tar file, you need to extract it by using
tar zxf lua-5.4.4.tar.gz command as shown below.
cyberithub@ubuntu:~$ tar zxf lua-5.4.4.tar.gz
Switch to directory
cd lua-5.4.4 command as shown below.
cyberithub@ubuntu:~$ cd lua-5.4.4
Then build and compile the code by using
make all test command as shown below.
cyberithub@ubuntu:~/lua-5.4.4$ make all test make: Entering directory '/home/cyberithub/lua-5.4.4/src' Guessing Linux make: Entering directory '/home/cyberithub/lua-5.4.4/src' make all SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl" make: Entering directory '/home/cyberithub/lua-5.4.4/src' gcc -std=gnu99 -O2 -Wall -Wextra -DLUA_COMPAT_5_3 -DLUA_USE_LINUX -c -o lapi.o lapi.c ........................ gcc -std=gnu99 -o luac luac.o liblua.a -lm -Wl,-E -ldl make: Leaving directory '/home/cyberithub/lua-5.4.4/src' make: Leaving directory '/home/cyberithub/lua-5.4.4/src' make: Leaving directory '/home/cyberithub/lua-5.4.4/src' make: Entering directory '/home/cyberithub/lua-5.4.4/src' ./lua -v Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio make: Leaving directory '/home/cyberithub/lua-5.4.4/src'
Finally install the binary by using
sudo make install command as shown below.
cyberithub@ubuntu:~/lua-5.4.4$ sudo make install [sudo] password for cyberithub: cd src && mkdir -p /usr/local/bin /usr/local/include /usr/local/lib /usr/local/man/man1 /usr/local/share/lua/5.4 /usr/local/lib/lua/5.4 cd src && install -p -m 0755 lua luac /usr/local/bin cd src && install -p -m 0644 lua.h luaconf.h lualib.h lauxlib.h lua.hpp /usr/local/include cd src && install -p -m 0644 liblua.a /usr/local/lib cd doc && install -p -m 0644 lua.1 luac.1 /usr/local/man/man1
Step 4: Check Version
You can test the installation by running
lua -v command as shown below.
cyberithub@ubuntu:~$ lua -v Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
Step 5: Write a Sample Program
Now that Lua is successfully installed in your System, it is time to test the tool by writing a sample program. You can write a simple print statement to display
"Hi, This is from CyberITHub" on the output as shown below.
cyberithub@ubuntu:~$ nano hello.lua print("Hi, This is from CyberITHub")
To run the above program, you need to use
sudo lua hello.lua as shown below.
cyberithub@ubuntu:~$ sudo lua hello.lua Hi, This is from CyberITHub
Step 6: Check the Man Page
You can check all the options available with Lua Interpreter from its Man page. You need to use
man lua command to open its Man Page as shown below.
cyberithub@ubuntu:~$ man lua LUA(1) General Commands Manual LUA(1) NAME lua - Lua interpreter SYNOPSIS lua [ options ] [ script [ args ] ] DESCRIPTION lua is the standalone Lua interpreter. It loads and executes Lua programs, either in textual source form or in precompiled binary form. (Precom‐ piled binaries are output by luac, the Lua compiler.) lua can be used as a batch interpreter and also interactively. After handling the options, the Lua program in file script is loaded and executed. The args are available to script as strings in a global table named arg and also as arguments to its main function. When called without arguments, lua behaves as lua -v -i if the standard input is a termi‐ nal, and as lua - otherwise. .............................................