DEV Community

Cover image for Gearing up for Lua
DR
DR

Posted on • Updated on

Gearing up for Lua

Introduction

From 2022 to 2023, Stack Overflow tracked changes in the popularity of a bunch of programming languages via their annual survey (excellent reading, if you haven't already). Many of them hopped up or down one or two spaces on the global leaderboard, but only one leaped up an impressive seven spaces. This language is commonly used to make games, is fast and cross-compatible, and has a simple syntax comparable to Python or Ruby.

Any guesses?

This month, we're talking about Lua. It's not always a first choice when it comes to programming, but I think there's a lot to enjoy about this little language. Heck, I'm a big game development fan myself - I would look into it even if that was the only reason to.

I'm not going to talk your ear off with the history - here's a short video that takes you through the basics, if you're curious. Focus on the big picture, we're talking syntax later.

And here's their official explanation that talks about where it came from and what it's used for. Overall, I'm super excited to work with this language and I totally lucked out getting this instead of something like Rust or C++ to start with.

Ecosystem

Lua doesn't have a super expansive community around it like other, more popular, languages, but it's got a couple of neat features that make it worth playing around with.

LuaRocks is a package manager for Lua modules, similar to npm in JavaScript or pip in Python. It's free software and allows you to "create and install Lua modules as self-contained packages called rocks" (downloadable on Unix and Windows systems). I'm not going to be using it heavily, but I'm hoping to be able to play around with it later this month.

Programming Gems is a collection of some articles detailing how to program well in Lua. It's also freely available on the Lua website, and though it's a bit random, you might find something fun to play around with.

If you're familiar with awesome-lists, you'll be happy to know that an awesome-lua repository does in fact exist. This list contains more interesting stuff about the language, along with going deeper into certain niches that I'm not even going to start to touch.

Lua is also useful in that it plays well with C (partially because much of it is built in ANSI C to begin with). I won't be touching on this much, but there look to be a bunch of interesting articles on the topic you could investigate deeper (here's one to get you started).

Probably the most important piece of software we'll be playing around with is a game engine called LÖVE. Lua is well known around developer circles as being a good scripting language when it comes to making games, and this engine is one of the more popular. I'll be going through installation at the end of this post.

System

I'm running code for the year on a WSL 2 setup (Windows 10) with VSCode. Terminal snippets are aimed at Unix, but it's easy to translate to Mac or Windows itself if you don't want that specific workflow.

Alternatively, you can always set up a target environment online that comes with the language preinstalled. This works well for me in particular when I'm on a machine that isn't mine (school laptop, etc.) that I don't want to mess with.

My top pick here would be Replit because of the expanse of their features, including a shell so you can run commands similarly to what I'll be doing. However, hardware limitations cap pretty quick on the free tier, so don't expect to run anything huge.

This month...

This month, I'm working with Lua. This post is the first of five that will take you through my learning process, over which I'll be talking about the language and building some cool side projects.

Here's our timeline, for anyone who's curious.

  • 1/1: Introduction
  • 1/5: Syntax exploration (technical knowledge) and cheatsheet
  • 1/12: Mini-projects and code released
  • 1/29: Final projects and code released
  • 1/31: Wrap-up and next month's language

Alright, let's get coding!

Installation

Lua

As per the official website, here's how to get the latest version of Lua installed on your machine.

curl -R -O http://www.lua.org/ftp/lua-5.4.6.tar.gz
tar zxf lua-5.4.6.tar.gz
cd lua-5.4.6
make all test
Enter fullscreen mode Exit fullscreen mode

From there, it should be as simple as running lua --version to see if you're up and running.

If this doesn't work for you, you might try this alternative version.

sudo apt update
sudo apt install lua5.4
Enter fullscreen mode Exit fullscreen mode

This can be tested the same way, or just use lua to start an interactive shell session (similar to Ruby or Python, if you're familiar with either).

If you're still having problems, you could head to their downloads page and see if you can find any more relevant information. Both of these worked for me so I'm not going into any more depth, but additional resources do exist. These will also come in handy if you're not on a Unix system.

LuaRocks

Specific instructions to download LuaRocks can be found on their website, but here's the quick start that should work for Unix systems.

$ wget https://luarocks.org/releases/luarocks-3.9.2.tar.gz
$ tar zxpf luarocks-3.9.2.tar.gz
$ cd luarocks-3.9.2
$ ./configure && make && sudo make install
$ sudo luarocks install luasocket
$ lua
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
> require "socket"
Enter fullscreen mode Exit fullscreen mode

This depends on Lua being installed, so make sure to do that first (instructions above!).

LÖVE2D

If you have Lua installed, you can easily install LOVE with a few shell commands.

sudo add-apt-repository ppa:bartbes/love-stable
sudo apt update
sudo apt install love
Enter fullscreen mode Exit fullscreen mode

Test with love --version.

Small note.
When I ran these and then tried to launch a window with the love command, it gave me an error similar to this. This is 100% because of WSL and so if you're not using it, feel free to ignore.

Unable to create OpenGL window 
This program requires a graphics card and video drivers which support OpenGL 2.1 or OpenGL ES 2. 
Enter fullscreen mode Exit fullscreen mode

A simple fix (thanks GPT!) is running this code.

sudo apt install libgl1-mesa-glx
Enter fullscreen mode Exit fullscreen mode

WSL can be tricky sometimes. Check the manual or search around if you're using it and come upon a problem, they're usually easier to fix than they look.

As always, if you're having issues, their site might be a good place to start.

Conclusion

So that's the Lua installation. A bit rough around the edges, but not super hard.

Join me on Friday (1/5), where I'll be talking about how to start your coding journey with your newfound materials and giving you a free cheatsheet to keep these ideas fresh in your mind.

Happy coding and have a great end to your holidays :)


Hey there! Thanks for reading my post. If you enjoyed, leave a like and share 💖

If you're new to the series, I'm breaking down twelve popular programming languages over the year of 2024 and exploring what they have to offer as opposed to others. This is, of course, a valiant attempt to revive my programming "career" and publish interesting content.

Want to keep up with my shenanigans? Check out the GitHub repository, which is where I'm stockpiling my projects over the year. You can also follow me here on DEV to get my latest posts straight to your notifications!


Top comments (0)