# Building Ruby

Lots of good resources into how we can build Ruby in Koichi’s Ruby Hack challenge.

Basic gist for building is that it’s best to separate the source tree from the build artifacts. Ruby uses a two phase build where it builds a binary called miniruby and then uses that in the second stage build to build the actual ruby binary (and others). And this process creates a bunch of build artifacts that make clean doesn’t appear to clean up, hence keeping this separate from your actual code checkout.

This causes a couple challenges with setting up editors: see [setting-up-vscode-for-mri], where some of the include files are in your build dir which the editor doesn’t know about yet.

## Getting the build structure ready

• Set up your build directory structure like this:
  ruby
├── ruby
│   └── <git checkout here>
└── build
│   └── <Makefile and other build root artifacts>
└── install
└── <make install results get put here>

• cd into ruby/ruby and run autoconf
• cd into ruby/build and run ../ruby/configure --prefix=$PWD/../install --enable-shared --with-openssl-dir="$(brew --prefix openssl)" --with-readline-dir="\$(brew --prefix readline)" --disable-libedit

### Alternative configure

Aaron recommended doing:

begin
set -lx debugflags '-g'
set -lx optflags '-O0'
set -lx RUBY_DEVEL 'yes'
../ruby/configure --prefix=~../install --disable-install-doc --with-openssl-dir=(brew --prefix)/opt/openssl
end


This will make sure that every time Ruby builds either itself or any of the included gems it always builds with:

• -g include debug symbols - needed for C function labelling and decent debugger context (otherwise lldb just shows you assembly)
• -O0 minimal optimisations - this is needed to make sure that cmake doesn’t optimise out any local variables or useful information that you can use while debugging.
• RUBY_DEVEL enabled - I don’t actually know what this does yet.

It’s also possible to compile without this and then edit the generated Makefile, but this doesn’t apply these to any gems that are built - just the main miniruby and ruby binaries. Which can be a pain.

## Building and installing

• make -j8 && make install

This will build and install ruby, irb, gem, bundler et al and dump them in your configured install directory. This takes time (rougly 5 minutes on my work MacBook Pro, longer on my personal Surface Pro).

## Building for development

This obviously depends on what your working on, but for my purposes, where I’m predominantly in the GC or the VM, just building the initial miniruby binary is enough. Do this with

make miniruby


Once Ruby/miniruby is built you should be good to start development. See