This chapter covers what Lua is and how to set up a Lua environment on any operating system. Lua is not tied to any operating system, so this chapter covers installing Lua for Windows, macOS, and Linux. By the end of this chapter, you will have a fully functional Lua development environment set up, regardless of what operating system you are using. This will leave you ready to start learning the Lua language.
This is what you will learn in this chapter:
- What Lua is
- How to install Lua
- Available Lua tools
- How to install Visual Studio Code
- How to use Visual Studio Code
- Write and run a Hello World Lua application
The code files of this chapter can be found on GitHub:
Check out the following video to see the code in action:
Lua is a powerful, fast, lightweight, embeddable scripting language. The Lua virtual machine and interpreter are written in C. As a language, Lua is easy to learn. It contains 21 keywords, which makes the language rather small. Lua is also easy to read and understand, as its syntax makes it similar to English. For example, consider the following code snippet:
if not hero:IsAlive() then
This code is easy to read, and I bet you can take an intuitive guess at what it does. Lua is not only easy to read, it is also very powerful. The real power of Lua comes from its extensible nature. Programming constructs such as object-oriented programming (OOP) can be implemented in Lua, even though the language has no native support for objects.
At the time of writing, Lua has 14 versions; this book will focus on Lua 5.2.4. The latest version is 5.3; the main difference between 5.2 and 5.3 is that 5.3 contains support for explicit integers and bitwise operation.
Lua is open source software published under the MIT License. You can browse Lua's source code at https://www.lua.org/source/. Additionally, you can download both the source code and reference manuals for Lua from https://www.lua.org/ftp/.
At the time of writing, no pre-built binaries are downloadable from the lua.org website. Pre-built binaries can be found on SourceForge at https://sourceforge.net/projects/luabinaries/. In this chapter, we will be using SourceForge to download binaries for Lua.
Follow these steps to install Lua 5.2.4 on Windows 10. These instructions are written for Windows 10, but the steps needed to install should be similar on older (and future) versions of Windows as well:
- To download Lua 5.2.4, visit https://sourceforge.net/projects/luabinaries/files/5.2.4/.
- Click on the Tools and Executables link.
- On a 32-bit version of Windows, click the lua-5.2.4_Win32_bin.zip link to start downloading Lua. On a 64-bit version of Windows, click the lua-5.2.4_Win64_bin.zip link to start downloading.
- Once the file is downloaded, unzip the file. Unzipping the downloaded file should create four new files: lua52.dll, lua52.exe, luac52.exe, and wlua52.exe.
- Create a new folder inside C:\Program Files, and call this new folder LUA. Copy the four files you just unzipped into this directory.
- Rename lua52.exe to lua.exe. If your Windows installation is set up to hide file extensions, rename lua52 to lua:
- The path to Lua needs to be set up as an environment variable in Windows.
- Right-click on the Start/Windows menu button and select the System option.
- From the System window, select the Advanced Settings option.
- Having clicked the Advanced Settings option, you should now see the System Properties dialog. In this dialog, click on the Environment Variables... button.
- In the Environment Variables window, with the Path variable selected, click the Edit... button:
- Inside the Edit environment Variable window, click the New button and add C:\Program Files\LUA as a new path. Click the OK button to save changes and close this window. You can close all the windows we have opened up to this point.
- Lua should now be successfully installed on your computer. To verify the installation, you need to launch a new Command Prompt. You can launch Command Prompt by right-clicking the Windows Start/Windows button and selecting the Command Prompt item.
- In the newly opened Command Prompt, type lua -v. If everything is set up correctly, the command should print out the installed version of Lua (5.2.4):
Follow these steps to install Lua 5.2.4 on macOS. These instructions are written for macOS High Sierra, but the steps are the same on previous (and future) versions of macOS as well:
- To download Lua 5.2.4, visit https://sourceforge.net/projects/luabinaries/files/5.2.4/.
- Click on the Tools and Executables link.
- Click on the lua-4.2.4_MacOS1011_bin.tar.gz link to start downloading Lua.
- Once the zip file has downloaded, unzip it. The archive should contain two files, lua52 and luac52:
- Create a new folder in your ~/Documents directory, and name this folder LUA. Move both lua52 and luac52 into this new directory:
- Rename lua52 to just lua.
- Launch a Terminal window. The Terminal app is located at /Applications/Utilities/Terminal.app. You can also simply type Terminal into the universal search on macOS.
- With the new Terminal window open, type sudo nano /etc/paths and hit Enter. You will be asked for your password; this is the password for your user account. The password will not show up as you type it. After the password is entered, nano will open; nano is a Terminal-based text editor. You should see something similar to the following window:
- You can navigate the type cursor with the arrow keys. Don't worry if your paths file (the file we are editing) already has text in it. We will be adding a new entry into this file; where in the file you add the new entry does not matter. On a new line, type ~Documents/LUA:
- Press Ctrl + X to exit nano. The program will ask you if you want to save the changes you have made to the file. Press Y to save changes.
- Nano will ask you to confirm the filename. Just hit Enter to accept the default path.
- In order for the changes made in the paths to take effect, you must restart the Terminal app. To do this, right-click on the Terminal icon in your macOS dock and select Quit. Then, launch a new Terminal window.
- In the new Terminal window, type lua -v. If everything is set up correctly, the Terminal should print out the installed version of Lua (5.2.4):
Follow these steps to install Lua 5.2.4 on Linux. These instructions are written for Ubuntu Linux 16.04. The steps needed to install Lua are the same on Ubuntu Linux 12.04 and higher:
- The entire installation of Lua can be done using the command line, with the apt package manager.
- Open up a new Terminal and type sudo apt-get install lua5.2:
- Provide your password when prompted and wait for the installation to finish:
- Type lua -v. If everything is set up correctly, the Terminal should print out the installed version of Lua (5.2.4):
A programming language relies heavily on the tools that support it. Lua files are plain text files. This means you can write Lua in any text editor you want, whether it is emacs, vi, Sublime Text, TextWrangler, or just the OS-provided basic text editor.
Because of the popularity of Lua, several IDEs, such as ZeroBrane Studio, Decoda, and LuaEdit, have been created for the language. An IDE is an integrated development environment. An IDE comes with everything you need to write, compile, and execute code. There are a number of advanced text editors that have varying levels of support for Lua:
Throughout this book, we will be using Visual Studio Code. VS Code is a free text editor, which supports the Lua syntax with its default installation and works across multiple platforms. The version of VS Code used is 1.9.1, but future versions should work more or less the same way.
Setting up VS Code for Windows is very straightforward. The installer takes care of everything for you. These instructions are written for Windows 10, but the process should be the same on all versions of Windows:
- Go to http://code.visualstudio.com/ and download the VS Code Code installer for Windows.
- Once downloaded, launch the installer exe file. The default options are all valid, so you can hit Next all the way through the installer.
- Wait for the installer to finish the setup and exit the installer. There are no further actions to take.
These instructions are written for OSX High Sierra, but the installation steps should be the same on all supported versions of OSX:
- Go to http://code.visualstudio.com/ and download VS Code for macOS. This will download a zipped file named VSCode-darwin-stable.zip.
- Double-click the zip file to extract its contents. Drag the resulting Visual Studio Code.app file into your Applications directory.
- Once Visual Studio Code is in the Applications directory, it is installed, and there are no further actions to take.
These instructions are written for Ubuntu Linux 16.04. The steps needed to install Lua are the same for Ubuntu Linux 12.04 and higher.
- Go to http://code.visualstudio.com/ and download the Visual Studio Code installer .deb file. Take note of the name of the downloaded file; the version I am using is named code_1.9.1-1486597190_amd64.deb.
- Once downloaded, launch a new Terminal window. Navigate the Terminal to the downloads folder with the following command: cd ~/Downloads.
- Next, install the .deb file with the following command: sudo dpkg -i ./code_1.9.1-1486597190_amd64.deb. The filename might be different based on the version of VS Code you downloaded.
- Fix any missing or broken dependencies with the following command: sudo apt-get install -f.
- Visual Studio Code is now installed, and there are no further actions to take.
It's important to be familiar with the tools you use. While Visual Studio Code is primarily a text editor, it does boast a rather large set of IDE-like features. VS Code can easily become overwhelming if you are not familiar with using it.
Follow these steps to gain some familiarity and intuition with Visual Studio Code:
- When you open up Visual Studio Code, you are greeted with either the last open documents, the welcome page, or if you have no documents open and the welcome page is disabled, the default window.
- The icons on the left side of the screen make up what is called the View bar. Clicking any of the items on the View bar will cause a side bar to become visible:
- The first item on the View bar is the Explorer. You can use the Explorer to open a folder and View all of the files in that folder in one convenient list. We will use this feature of the editor throughout the next few chapters:
- The search item on the bar will let you search for and replace text in either open documents, or documents inside of the currently open folder.
- The GIT item on the View bar will only be available if the currently open folder is a git repository. VS Code has excellent git integration! While source control solutions such as git are outside the scope of this book, using some kind of source control is highly recommended:
- The DEBUG sidebar gives VS Code IDE features such as break points and a watch window:
- Finally, the EXTENSIONS item will show you a side bar that can be used to View, manage, and install new extensions in Visual Studio Code:
- To make a new file, simply select File > New File.
- This opens a new file, in a new tab. This file has no syntax highlighting yet. To assign a syntax, click on the Plain Text label in the bottom-right of the code tab, then select Lua (lua) from the drop-down menu that appears:
- If at any point you open a file and it does not have proper syntax highlighting, you can follow the previous step to force the file to have Lua syntax highlighting. In the previous step, we set the syntax of the file manually. If you save a file with a .lua extension, the next time the file is opened, it will automatically use Lua syntax highlighting.
It is common practice when first learning a new programming language to create a Hello World program. This is a simple program that prints the words Hello World to the screen. The goal of this exercise is to write, compile (or interpret), and run a simple piece of code to prove that you can execute it.
The program will be written using Visual Studio Code, but how will it be executed? Visual Studio Code provides an Integrated Terminal. This is a Terminal that should work the same way regardless of what operating system you are using. It's important to note that whatever code gets executed through this Terminal can also be executed through the operating system Terminal/shell.
Being able to perform the same steps regardless of operating system can save time and reduce errors. For this reason, future chapters will assume code will be executed in VS Code instead of the native Terminal of each operating system.
Follow these steps to create a Hello World program, save it, and execute it on any platform (macOS, Windows 10, or Linux):
- Open Visual Studio Code and make a new document with File > New.
- Set the syntax highlighting of this file to the Lua syntax. Click on the Plain Text label in the bottom right of the code tab, then select Lua (lua) from the drop-down menu that appears:
- In this new file, type print ('hello, world'):
- Save the file to your desktop and name it hello.lua.
- From the top menu of Visual Studio Code, select View > Integrated Terminal.
- On all platforms, if you did not have a folder open, the editor starts out in your home directory. If you did have a folder open, the editor starts out in the folder. Navigate to your desktop with the following command: cd ~/Desktop; the ~/ part of the path is shorthand for home directory:
- Now that the Terminal has the desktop directory open (which is where hello.lua should be saved), you can execute the Lua file with the following command: lua hello.lua. You should see hello, world printed to the Terminal.
The last step invoked the Lua binary from the Terminal of Visual Studio with the hello.lua file as an argument. This, in turn, launched the Lua runtime, which executed the file that was provided as an argument. If the Lua runtime did not launch, you may want to review how to set the runtime up in the Tools for Lua section.
This chapter covered how to install Lua and VS Code. VS Code is the development environment that will be used throughout this book to write and execute Lua code. The integrated Terminal in Visual Studio Code allows us to execute code the same way on all three major platforms: Windows, Linux, and macOS.
In the next chapter, we will start to write code. The basics of Lua, such as variables, loops, if statements, and functions, will be covered. The topics covered in the next chapter are the basics of programming; it is going to be one of the most important chapters in this book.