- 
Download the latest Lua and LuaJIT sources
- 
Create temporary folder for Lua sources.
I assume you would useC:\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 folderC:\Temp\luajit\ - 
You can uninstall "Git for Windows" application now.
It's portable, so just remove its folder. 
 - 
 - 
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 folderC:\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 askedProceed with installation? [Y/n], answerYand pressEnter. - 
Close this MSYS2 console window and open a new one by clicking the
MSYS2 MinGW 64-bitmenu 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. 
 - 
 - 
Install Lua and LuaJIT executables
- 
Create folder for Lua executables.
I assume you would useC:\Lua\folder. - 
Install executable files:
- 
Move 3 files:
lua.exe,luac.exeandlua54.dll
(sort files by "Date Modified" - these files are among the most recently modified)
fromC:\Temp\lua-5.4.3\src\toC:\Lua\ - 
Move 2 files:
luajit.exeandlua51.dll
fromC:\Temp\luajit\src\toC:\Lua\ 
 - 
 - 
Install
jit.*modules (optional)
Move the folderC:\Temp\luajit\src\jit\toC:\Lua\lua\jit\ - 
Install documentation (optional)
Move and rename the folderC:\Temp\luajit\doc\toC:\Lua\doc\LuaJIT\
Move and rename the folderC:\Temp\lua-5.4.3\doc\toC:\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
Pathand append;C:\Luato the end of its content.
After that every new console window will understandluajitandluacommands.
(Already opened console windows still have old copy of PATH without Lua folder in it) 
 - 
 
 - 
 - 
Make sure Lua works now.
- Open new console window.
 - Type 
luato run interactive Lua interpreter. - Press 
Ctrl+ZandEnterto exit. - Type 
luajitto run interactive LuaJIT interpreter. - Press 
Ctrl+ZandEnterto 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
How to open Windows console?
- Press 
Win+R, typecmd, pressEnter
Winis the key between Left Ctrl and Left Alt. 
How to open Control Panel?
- Press 
Win+R, typecontrol, pressEnter 
How do I run my Lua program?
- Use your favorite text editor (or just Notepad) to create text file 
yourfile.luawith your Lua program inside.
Open Windows console and typelua yourfile.luato 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.luato the modules folderC:\Lua\lua\
Now you can load your module byrequire("yourmodule")in any Lua program. 
What does mingw32-make CFLAGS=-DLUAJIT_ENABLE_LUA52COMPAT mean?
- This flag enables some Lua 5.2 features in LuaJIT,
seefile:///C:/Lua/doc/LuaJIT/extensions.html#lua52in your browser for details.
You can replace this command with simplemingw32-maketo 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 buildwlua.exeandwluajit.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:- read binary module's documentation to learn which Lua version the module was written for;
 - compare bitness and C runtime of the two DLL files: binary module DLL and Lua DLL.
 
 - 
 
I really appreciate your work and that you are sharing this! 🥇
Please do not remove this gist!