Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosme12/5893e7b89dd224e7b1adc69fc6d53545 to your computer and use it in GitHub Desktop.
Save cosme12/5893e7b89dd224e7b1adc69fc6d53545 to your computer and use it in GitHub Desktop.
How to install Lua and LuaJIT on Windows

How to install Lua and LuaJIT on 64-bit Windows

  1. Download the latest Lua and LuaJIT sources

    • Create temporary folder for Lua sources.
      I assume you would use C:\Temp\ folder.

    • Visit Lua FTP webpage and download the latest Lua source archive, currently it is lua-5.4.3.tar.gz

    • Use suitable software (7-Zip, WinRar, WinZip or TotalCommander) to unpack the archive.
      Please note that with 7-Zip you have to unpack it twice: lua-5.4.3.tar.gz -> lua-5.4.3.tar -> lua-5.4.3\

    • Move unpacked Lua sources to C:\Temp\lua-5.4.3\

    • Visit "Git for Windows" latest release page and download the latest portable installer, currently it is PortableGit-2.31.1-64-bit.7z.exe

    • Run PortableGit installer.

    • Double-click the installed file git-cmd.exe, a console window will open.

    • Execute the following commands in this console window:
      cd /d C:\Temp\
      git clone https://luajit.org/git/luajit.git
      cd luajit
      git checkout v2.1

    • Close the console window.
      Now you have LuaJIT 2.1 sources in the folder C:\Temp\luajit\

    • You can uninstall "Git for Windows" application now.
      It's portable, so just remove its folder.

  2. Build Lua and LuaJIT executables using MinGW64

    • Visit MSYS2 download page and download the latest MSYS2 installer, currently it is msys2-x86_64-20210419.exe

    • Run MSYS2 installer.
      It is recommended to install MSYS2 in the default destination folder C:\msys64\,
      but you may choose another path consisting of English letters without spaces

    • After installation is complete, a MSYS2 console window will open.
      Execute the following command in this MSYS2 console window:
      pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-make
      When asked Proceed with installation? [Y/n], answer Y and press Enter.

    • Close this MSYS2 console window and open a new one by clicking the MSYS2 MinGW 64-bit menu item in Windows Start menu.
      Execute the following commands in the new MSYS2 window:

      • Go to the location of Lua 5.4 sources:
        cd /c/Temp/lua-5.4.3/

      • Build Lua 5.4:
        mingw32-make mingw

      • Go to the location of LuaJIT sources:
        cd /c/Temp/luajit/

      • Build LuaJIT:
        mingw32-make CFLAGS=-DLUAJIT_ENABLE_LUA52COMPAT

    • Close the MSYS2 console window.

    • You can uninstall "MSYS2" application now at Control Panel -> Programs and Features.

  3. Install Lua and LuaJIT executables

    • Create folder for Lua executables.
      I assume you would use C:\Lua\ folder.

    • Install executable files:

      • Move 3 files: lua.exe, luac.exe and lua54.dll
        (sort files by "Date Modified" - these files are among the most recently modified)
        from C:\Temp\lua-5.4.3\src\ to C:\Lua\

      • Move 2 files: luajit.exe and lua51.dll
        from C:\Temp\luajit\src\ to C:\Lua\

    • Install jit.* modules (optional)
      Move the folder C:\Temp\luajit\src\jit\ to C:\Lua\lua\jit\

    • Install documentation (optional)
      Move and rename the folder C:\Temp\luajit\doc\ to C:\Lua\doc\LuaJIT\
      Move and rename the folder C:\Temp\lua-5.4.3\doc\ to C:\Lua\doc\Lua54\

    • You can remove Lua and LuaJIT sources now by deleting their temporary folders:
      C:\Temp\lua-5.4.3\
      C:\Temp\luajit\

    • Add Lua folder to PATH

      • Go to
        Control Panel -> System -> Advanced system settings -> Environment Variables

      • Edit variable Path and append ;C:\Lua to the end of its content.
        After that every new console window will understand luajit and lua commands.
        (Already opened console windows still have old copy of PATH without Lua folder in it)

  4. Make sure Lua works now.

    • Open new console window.
    • Type lua to run interactive Lua interpreter.
    • Press Ctrl+Z and Enter to exit.
    • Type luajit to run interactive LuaJIT interpreter.
    • Press Ctrl+Z and Enter to exit.

The directory tree looks like this:

C:\
  +-- Lua\
        +-- lua\             folder for Lua modules
        |     +-- jit\       subfolder for jit.* modules
        +-- doc\             folder for documentation
        |     +-- Lua54\     open manual.html with your browser
        |     +-- LuaJIT\    open running.html with your browser
        +-- lua.exe          Lua 5.4 executable
        +-- lua54.dll        Lua 5.4 DLL
        +-- luajit.exe       LuaJIT executable
        +-- lua51.dll        LuaJIT DLL

FAQ

How to open Windows console?

  • Press Win+R, type cmd, press Enter
    Win is the key between Left Ctrl and Left Alt.

How to open Control Panel?

  • Press Win+R, type control, press Enter

How do I run my Lua program?

  • Use your favorite text editor (or just Notepad) to create text file yourfile.lua with your Lua program inside.
    Open Windows console and type lua yourfile.lua to run the program.
    If you are in a different directory, you should provide full path: lua C:\path\to\yourfile.lua

How to make my Lua module available for my Lua program?

  • Copy your module file yourmodule.lua to the modules folder C:\Lua\lua\
    Now you can load your module by require("yourmodule") in any Lua program.

What does mingw32-make CFLAGS=-DLUAJIT_ENABLE_LUA52COMPAT mean?

  • This flag enables some Lua 5.2 features in LuaJIT,
    see file:///C:/Lua/doc/LuaJIT/extensions.html#lua52 in your browser for details.
    You can replace this command with simple mingw32-make to build strictly 5.1-compatible LuaJIT instead.

Every time I run Lua script an annoying console window pops up.
How to run a Lua script silently in the background?

  • You need windowless version of Lua binaries.
    Here are instructions on how to build wlua.exe and wluajit.exe

Lua crashes when I try to use a binary module (a Lua library written in C and provided as DLL file).
How do I determine whether some binary module is compatible with my Lua executable?

  • The following three conditions must be met:

    • Binary module must be written for the same Lua version.
      Lua 5.1, 5.2, 5.3 and 5.4 are four incompatible versions, and a binary module is designed to work only with one of them.
      For example, a module created for Lua 5.2 will not work with Lua 5.4.
      LuaJIT can work only with binary modules written for Lua 5.1.

    • Binary module DLL must have the same bitness (32 or 64) as the Lua/LuaJIT executable;
      otherwise module DLL will not be able to load.

    • Binary module DLL must depend on the same C runtime as the Lua/LuaJIT DLL;
      otherwise Lua may crash or behave incorrectly due to each C runtime library has its own heap memory and stdin/stdout handlers.
      In other words, both Lua executable and binary library must be built with the same tool.
      For example, if your module DLL was built with MSVC, it will be incompatible with Lua built with MinGW64.

    You can view bitness and C runtime of any DLL file with "Dependency Walker" application.
    So, before using a binary module you should do the following:

    1. read binary module's documentation to learn which Lua version the module was written for;
    2. compare bitness and C runtime of the two DLL files: binary module DLL and Lua DLL.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment