Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Egor-Skriptunoff/cb952f7eaf39b7b1bf739b818ece87cd to your computer and use it in GitHub Desktop.
Save Egor-Skriptunoff/cb952f7eaf39b7b1bf739b818ece87cd 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.
@Ismoh
Copy link

Ismoh commented Dec 22, 2021

I really appreciate your work and that you are sharing this! 🥇
Please do not remove this gist!

@cleverpig
Copy link

coool!

@Corical
Copy link

Corical commented Mar 24, 2022

Thank you, very concise, thorough, and to the point, worked like a charm.

@GrayFace
Copy link

GrayFace commented Sep 1, 2022

How can different C runtime library versions cause problems (on Windows)? They're DLL's, they're containing their C libraries and have completely separate heap management. You aren't gonna call free() on Lua state or any of its objects etc. I guess naming of different compilers might be different (not sure though) if compiled in C++ mode, but Lua defaults to C, especially when it comes to building as a DLL...

@Ismoh
Copy link

Ismoh commented Sep 1, 2022

How can different C runtime library versions cause problems (on Windows)? They're DLL's, they're containing their C libraries and have completely separate heap management. You aren't gonna call free() on Lua state or any of its objects etc. I guess naming of different compilers might be different (not sure though) if compiled in C++ mode, but Lua defaults to C, especially when it comes to building as a DLL...

Dont see any benefit of this comment. Thanks for that! :)

@GrayFace
Copy link

GrayFace commented Sep 1, 2022

Dont see any benefit of this comment. Thanks for that! :)

Trolls on Github, that's funny.

@miosad
Copy link

miosad commented Jan 9, 2023

epic

@groovyghoul
Copy link

Worked perfectly! Thank you so much for your work!
For anybody that gets "/var/cache/pacman/pkg/mingw-w64-x86_64-mpc-1.2.1-1-any.pkg.tar.zst fails with signature from "David Macek <...>" is unknown trust" (or something like that...can't quite remember now) when running pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-make, I ran the following two commands and then everything was fine:

pacman -Syyu
pacman -Su

Now pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-make again should be good to go.

Again, thank you for this gist...perfect!

@BivoZeou
Copy link

very thanks! god job!

@alpharetzy
Copy link

Can you add peparser as an optional feature? especially unpack

@ThatsNasu
Copy link

git certainly needs a "Thanks!" button for gists like this, amazing work c:

@insinfo
Copy link

insinfo commented Nov 13, 2023

@Egor-Skriptunoff
Can you use this process to compile wrk for windows?

@leoncrf
Copy link

leoncrf commented Jul 10, 2024

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.

Running this code returns various warnings and errors.

error: failed retrieving file 'mingw-w64-x86_64-expat-2.4. 8-1-any. pkg. tar. zst' from ftp. cc. uoc.gr :
The requested URL returned error: 404
warning: too many errors from ftp. cc.uoc.gr, skipping for the remainder of this transaction
error: failed retrieving file 'mingw-w64-x86_64-expat-2.4. 8-1-any. pkg. tar. zst' from mirror. jmu. edu :
The requested URL returned error: 404
warning: too many errors from mirror. jmu. edu, skipping for the remainder of this transaction
warning: failed to retrieve some files
error: failed to commit transaction (unexpected error)
Errors occurred, no packages were upgraded.

Fix:
At https://repo.msys2.org/distrib/x86_64/msys2 CAUTION, there are two .exe, the sfx.exe and the normal .exe download the most recent normal .exe version, but CAUTION, because I downloaded the most recent msys2-x86_64-20220904.exe and the installation process gave errors on the console), so on this site I clicked on ../ and downloaded msys2-x86_64-latest.exe, which oddly enough is said to be the most current version but the release date is a few months older than the most recent in relation to the previous page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment