Windows
Ruby supports a few native build platforms for Windows.
-
mswin: Build using Microsoft Visual C++ compiler with vcruntimeXXX.dll
-
mingw-msvcrt: Build using compiler for Mingw with msvcrtXX.dll
-
mingw-ucrt: Build using compiler for Mingw with Windows Universal CRT
Building Ruby using Mingw with UCRT
The easiest build environment is just a standard RubyInstaller-Devkit installation and git-for-windows. You might like to use VSCode as an editor.
Build examples
Ruby core development can be done either in Windows cmd like:
ridk install ridk enable ucrt64 pacman -S --needed %MINGW_PACKAGE_PREFIX%-openssl %MINGW_PACKAGE_PREFIX%-libyaml %MINGW_PACKAGE_PREFIX%-libffi mkdir c:\work\ruby cd /d c:\work\ruby git clone https://github.com/ruby/ruby src sh ./src/autogen.sh mkdir build cd build sh ../src/configure -C --disable-install-doc make
or in MSYS2 bash like:
ridk install ridk enable ucrt64 bash pacman -S --needed $MINGW_PACKAGE_PREFIX-openssl $MINGW_PACKAGE_PREFIX-libyaml $MINGW_PACKAGE_PREFIX-libffi mkdir /c/work/ruby cd /c/work/ruby git clone https://github.com/ruby/ruby src ./src/autogen.sh cd build ../src/configure -C --disable-install-doc make
If you have other MSYS2 environment via other package manager like scoop, you need to specify $MINGW_PACKAGE_PREFIX is mingw-w64-ucrt-x86_64. And you need to add --with-opt-dir option to configure command like:
sh ../../ruby/configure -C --disable-install-doc --with-opt-dir=C:\Users\username\scoop\apps\msys2\current\ucrt64
Building Ruby using Visual C++
Requirement
-
Windows 10 version 1809 (build 17763)/Windows Server 2019 or later.
-
Visual C++ 14.15 (Visual Studio 2017 version 15.8) or later.
Note if you want to build x64 version, use native compiler for x64.
The minimum requirement is here: * VC++/MSVC build tools on VS 2017 version 15.8 or later. * Windows 10/11 SDK
You can install Visual Studio Build Tools with
winget.win32\install-buildtools.cmdis a batch file to install the minimum requirements excluding the IDE etc. -
Please set environment variable
INCLUDE,LIB,PATHto run required commands properly from the command line. These are set properly byvsdevcmd.batorvcvarall*.batusually. You can run the following command to set them in your command line.To native build:
cmd /k win32\vssetup.cmd
To cross build arm64 binary:
cmd /k win32\vssetup.cmd -arch=arm64
To cross build x64 binary:
cmd /k win32\vssetup.cmd -arch=x64
This batch file is a wrapper of
vsdevcmd.batand options are passed to it as-is.win32\vssetup.cmd -helpfor other command line options.Note building ruby requires following commands.
-
nmake -
cl -
ml -
lib -
dumpbin
-
-
If you want to build from GIT source, following commands are required.
-
git -
ruby3.1 or later
You can use scoop to install them like:
scoop install git ruby
The windows version of
gitconfigured withautocrlfistrue. The Ruby test suite may fail withautocrlfset totrue. You can set it tofalselike:git config --global core.autocrlf false
-
-
You need to install required libraries using vcpkg on directory of ruby repository like:
vcpkg --triplet x64-windows install
-
Enable Command Extension of your command line. It's the default behavior of
cmd.exe. If you want to enable it explicitly, runcmd.exewith/E:ONoption. -
Some tests in
nmake checkcreate symbolic links. Enable Developer Mode (Settings > System > For developers) so that they do not fail withPermission denied @ rb_file_s_symlink. -
If you want to build YJIT,
rustcwith thex86_64-pc-windows-msvctarget is required. YJIT is enabled with--enable-yjitofwin32\configure.bat, and is supported only on x64 and only in YJIT's release mode, without the dev and stats builds.--enable-yjitaccepts onlyyesorno. A YJIT build does not install the static library. -
If you want to build with clang-cl instead of
cl, clang-cl 15 or later is required. It is the "C++ Clang Compiler for Windows" component (Microsoft.VisualStudio.Component.VC.Llvm.Clang) of Visual Studio 2022 version 17.4 or later.win32\vssetup.cmdaddsclang-cltoPATHwhen the component is installed. You can add it to the installed Build Tools with Visual Studio Installer, or from the command line like:"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\setup.exe" modify --installPath "%ProgramFiles(x86)%\Microsoft Visual Studio\18\BuildTools" --add Microsoft.VisualStudio.Component.VC.Llvm.Clang --passive
How to compile and install
-
Execute
win32\configure.baton your build directory. You can specify the target platform as an argument. For example, runconfigure --target=i686-mswin32. You can also specify the install directory. For example, runconfigure --prefix=<install_directory>. Default of the install directory is/usr. -
If you want to append to the executable and DLL file names, specify
--program-prefixand--program-suffix, likewin32\configure.bat --program-suffix=-$(MAJOR)$(MINOR).Also, the
--install-nameand--so-nameoptions specify the exact base names of the executable and DLL files, respectively, likewin32\configure.bat --install-name=$(RUBY_BASE_NAME)-$(MAJOR)$(MINOR).By default, the name for the executable without a console window is generated from the RUBY_INSTALL_NAME specified as above by replacing
rubywithrubyw. If you want to make it different more, modify RUBYW_INSTALL_NAME directly in the Makefile. -
You need specify vcpkg directory to use
--with-opt-diroption likewin32\configure.bat --with-opt-dir=C:/vcpkg_installed/x64-windows -
If you want to build with clang-cl, specify
CC=clang-cllikewin32\configure.bat CC=clang-cl. The installed ruby also uses clang-cl to build extension libraries, so installing gems with C extensions requires clang-cl too. -
Run
nmake upif you are building from GIT source. -
Run
nmake -
Run
nmake prepare-vcpkgwith administrator privilege if you need to copy vcpkg installed libraries likelibssl-3-x64.dllto the build directory. -
Run
nmake check -
Run
nmake install
Build examples
-
Build on the ruby source directory.
ruby source directory: C:\ruby build directory: C:\ruby install directory: C:\usr\local
C: cd \ruby win32\configure --prefix=/usr/local nmake nmake check nmake install
-
Build on the relative directory from the ruby source directory.
ruby source directory: C:\ruby build directory: C:\ruby\mswin32 install directory: C:\usr\local
C: cd \ruby mkdir mswin32 cd mswin32 ..\win32\configure --prefix=/usr/local nmake nmake check nmake install
-
Build on the different drive.
ruby source directory: C:\src\ruby build directory: D:\build\ruby install directory: C:\usr\local
D: cd D:\build\ruby C:\src\ruby\win32\configure --prefix=/usr/local nmake nmake check nmake install DESTDIR=C:
-
Build x64 version (requires native x64 VC++ compiler)
ruby source directory: C:\ruby build directory: C:\ruby install directory: C:\usr\local
C: cd \ruby win32\configure --prefix=/usr/local --target=x64-mswin64 nmake nmake check nmake install
-
Build x64 version with clang-cl
ruby source directory: C:\ruby build directory: C:\ruby install directory: C:\usr\local
C: cd \ruby win32\configure --prefix=/usr/local CC=clang-cl nmake nmake check nmake install
Building with GNU make
nmake cannot run jobs in parallel. win32\configure.bat --with-gnu-make writes GNUmakefile instead of Makefile, so that you can build with make -j. This is experimental, and nmake stays the default.
GNU make 4.4 or later built for native Windows is required. win32\install-gnumake.cmd downloads the GNU make release, verifies its SHA256, builds it with cl, and puts make.exe in .gnumake under the ruby source directory, or in the directory given as its argument.
C: cd \ruby win32\install-gnumake.cmd set PATH=C:\ruby\.gnumake;%PATH% win32\configure --prefix=/usr/local --with-opt-dir=C:/ruby/vcpkg_installed/x64-windows --with-gnu-make make -j16 make check make install
Use make in place of nmake for the other targets in this document too, such as make up, make prepare-vcpkg and make update-vcpkg.
The installed ruby also builds extension libraries with make found in PATH, not with nmake. Before installing gems with C extensions, copy make.exe from .gnumake to a directory in PATH, such as the bin directory of the install directory, since make install does not install it.
Bugs
You can NOT use a path name that contains any white space characters as the ruby source directory, this restriction comes from the behavior of !INCLUDE directives of NMAKE.
You can build ruby in any directory including the source directory, except win32 directory in the source directory. This is restriction originating in the path search method of NMAKE.
Dependency management
Ruby uses vcpkg to manage dependencies on mswin platform.
You can update and install it under the build directory like:
nmake update-vcpkg # Update baseline version of vcpkg nmake install-vcpkg # Install vcpkg from build directory
Windows Defender Firewall
Some tests listen on all interfaces, and Windows Defender Firewall shows a security alert dialog when an executable at a path it has no rule for does so. If you keep the same build directory, run win32\firewall.cmd there once, and the dialog no longer interrupts the tests. It adds rules that block inbound connections to ruby.exe and rubyw.exe in that directory, so the tests are not exposed to other machines, while connections from the same machine still work.
cd /d D:\build\ruby C:\src\ruby\win32\firewall.cmd
It restarts itself as administrator when not elevated. It also takes the build directory as an argument. When you no longer need the rules, delete them with -delete.
C:\src\ruby\win32\firewall.cmd -delete D:\build\ruby
If you build in a new worktree for each task, as coding agents often do, every worktree meets the dialog. A post-checkout hook in .git/hooks registers each worktree when git worktree add creates it, before its first build, and skips worktrees that already have the rules. The script elevates itself, so the hook runs without stopping when User Account Control is set to never notify.
#!/bin/sh top=$(git rev-parse --show-toplevel) || exit 0 fw="$top/win32/firewall.cmd" [ -f "$fw" ] || exit 0 dir=$(cygpath -w "$top") netsh advfirewall firewall show rule name="Ruby build directory $dir" >/dev/null 2>&1 && exit 0 cmd //c "$(cygpath -w "$fw")" "$dir" >/dev/null 2>&1 exit 0
Otherwise each new worktree asks for administrator privilege. To avoid that, register once from an elevated prompt a task that runs the script for every worktree with the highest privileges, and replace the cmd line of the hook with schtasks //run //tn ruby-firewall >/dev/null 2>&1. The task runs without asking.
schtasks /create /tn ruby-firewall /sc once /st 00:00 /rl highest /tr "cmd /c for /d %D in (C:\src\worktrees\*) do C:\src\ruby\win32\firewall.cmd %D"
Icons
Any icon files(*.ico) in the build directory, directories specified with icondirs make variable and win32 directory under the ruby source directory will be included in DLL or executable files, according to their base names.
$(RUBY_INSTALL_NAME).ico or ruby.ico --> $(RUBY_INSTALL_NAME).exe $(RUBYW_INSTALL_NAME).ico or rubyw.ico --> $(RUBYW_INSTALL_NAME).exe the others --> $(RUBY_SO_NAME).dll
Although no icons are distributed with the ruby source, you can use anything you like. You will be able to find many images by search engines. For example, the following are made from Ruby logo kit:
-
Small favicon in the official site