2011-08-11 22:40:36 +02:00
# ruby-build
2022-09-25 13:06:19 +02:00
ruby-build is a command-line tool that simplifies installation of any Ruby version from source on Unix-like systems.
2014-04-07 22:17:46 +02:00
2022-09-25 13:06:19 +02:00
It is available as a plugin for [rbenv][] as the `rbenv install` command, or as a standalone program as the `ruby-build` command.
2012-05-24 20:33:38 +02:00
## Installation
2022-09-25 13:06:19 +02:00
### Homebrew package manager
2017-08-23 10:07:25 +02:00
```sh
2022-09-25 13:06:19 +02:00
brew install ruby-build
```
2011-08-11 22:40:36 +02:00
2022-09-25 13:06:19 +02:00
Upgrade with:
```sh
2023-02-20 16:00:59 +01:00
brew upgrade ruby-build
2017-08-23 10:07:25 +02:00
```
2011-08-11 22:40:36 +02:00
2022-09-25 13:06:19 +02:00
### Clone as rbenv plugin using git
```sh
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
```
2012-02-07 21:23:05 +01:00
2022-09-25 13:06:19 +02:00
Upgrade with:
2017-08-23 10:07:25 +02:00
```sh
2022-09-25 13:06:19 +02:00
git -C "$(rbenv root)"/plugins/ruby-build pull
```
2012-02-07 21:23:05 +01:00
2022-09-25 13:06:19 +02:00
### Install manually as a standalone program
First, download a tarball from https://github.com/rbenv/ruby-build/releases/latest. Then:
```sh
tar -xzf ruby-build-*.tar.gz
PREFIX=/usr/local ./ruby-build-*/install.sh
2017-08-23 10:07:25 +02:00
```
2012-05-24 20:33:38 +02:00
2017-07-10 08:49:08 +02:00
## Usage
2012-04-21 01:46:02 +02:00
2017-07-10 08:49:08 +02:00
### Basic Usage
2012-04-21 01:46:02 +02:00
2017-08-23 10:07:25 +02:00
```sh
# As a standalone program
2024-11-06 10:26:00 +01:00
$ ruby-build --list # lists latest stable releases for each Ruby
$ ruby-build --definitions # lists all definitions, including outdated ones
2024-11-06 10:51:54 +01:00
$ ruby-build 3.2.2 ~/.rubies/ruby-3.2.2 # installs Ruby 3.2.2
$ ruby-build -d ruby-3.2.2 ~/.rubies # alternate form for the previous example
2023-11-03 18:00:45 +01:00
# As an rbenv plugin
$ rbenv install 3.2.2 # installs Ruby 3.2.2 to ~/.rbenv/versions/3.2.2
2017-08-23 10:07:25 +02:00
```
2012-04-21 01:46:02 +02:00
2023-11-21 17:25:51 +01:00
> [!WARNING]
2022-09-25 13:06:19 +02:00
> ruby-build mostly does not verify that system dependencies are present before downloading and attempting to compile Ruby from source. Please ensure that [all requisite libraries][build-env] such as build tools and development headers are already present on your system.
Basically, what ruby-build does when installing a Ruby version is this:
- Downloads an official tarball of Ruby source code;
- Extracts the archive into a temporary directory on your system;
- Executes `./configure --prefix=/path/to/destination` in the source code;
- Runs `make install` to compile Ruby;
- Verifies that the installed Ruby is functional.
Depending on the context, ruby-build does a little bit more than the above: for example, it will try to link Ruby to the appropriate OpenSSL version, even if that means downloading and compiling OpenSSL itself; it will discover and link to Homebrew-installed instances of some libraries like libyaml and readline, etc.
2012-05-24 20:33:38 +02:00
2017-07-10 08:49:08 +02:00
### Advanced Usage
2012-05-24 20:33:38 +02:00
2017-07-10 08:49:08 +02:00
#### Custom Build Definitions
2012-05-24 20:33:38 +02:00
2022-09-25 13:06:19 +02:00
To install a version of Ruby that is not recognized by ruby-build, you can specify the path to a custom build definition file in place of a Ruby version number.
2012-05-24 20:33:38 +02:00
2022-09-25 13:06:19 +02:00
Check out [default build definitions][definitions] as examples on how to write definition files.
2012-05-24 20:33:38 +02:00
2017-07-10 08:49:08 +02:00
#### Custom Build Configuration
2012-05-24 20:33:38 +02:00
2017-07-10 08:49:08 +02:00
The build process may be configured through the following environment variables:
2012-05-24 20:33:38 +02:00
2020-07-17 18:36:59 +02:00
| Variable | Function |
| ------------------------------- | ------------------------------------------------------------------------------------------------ |
| `TMPDIR` | Where temporary files are stored. |
| `RUBY_BUILD_BUILD_PATH` | Where sources are downloaded and built. (Default: a timestamped subdirectory of `TMPDIR` ) |
| `RUBY_BUILD_CACHE_PATH` | Where to cache downloaded package files. (Default: `~/.rbenv/cache` if invoked as rbenv plugin) |
| `RUBY_BUILD_HTTP_CLIENT` | One of `aria2c` , `curl` , or `wget` to use for downloading. (Default: first one found in PATH) |
| `RUBY_BUILD_ARIA2_OPTS` | Additional options to pass to `aria2c` for downloading. |
| `RUBY_BUILD_CURL_OPTS` | Additional options to pass to `curl` for downloading. |
| `RUBY_BUILD_WGET_OPTS` | Additional options to pass to `wget` for downloading. |
| `RUBY_BUILD_MIRROR_URL` | Custom mirror URL root. |
2023-09-21 16:23:35 +02:00
| `RUBY_BUILD_MIRROR_PACKAGE_URL` | Custom complete mirror URL (e.g. http://mirror.example.com/package-1.0.0.tar.gz). |
2020-07-17 18:36:59 +02:00
| `RUBY_BUILD_SKIP_MIRROR` | Bypass the download mirror and fetch all package files from their original URLs. |
2023-09-21 16:23:35 +02:00
| `RUBY_BUILD_TARBALL_OVERRIDE` | Override the URL to fetch the ruby tarball from, optionally followed by `#checksum` . |
2024-05-30 16:34:31 +02:00
| `RUBY_BUILD_DEFINITIONS` | Colon-separated list of paths to search for build definition files. |
| `RUBY_BUILD_ROOT` | The path prefix to search for build definitions files. *Deprecated:* use `RUBY_BUILD_DEFINITIONS` |
2024-11-09 17:21:11 +01:00
| `RUBY_BUILD_VENDOR_OPENSSL` | Build and vendor openssl even if the system openssl is compatible |
2020-07-17 18:36:59 +02:00
| `CC` | Path to the C compiler. |
| `RUBY_CFLAGS` | Additional `CFLAGS` options (_e.g.,_ to override `-O3` ). |
| `CONFIGURE_OPTS` | Additional `./configure` options. |
| `MAKE` | Custom `make` command (_e.g.,_ `gmake` ). |
| `MAKE_OPTS` / `MAKEOPTS` | Additional `make` options. |
| `MAKE_INSTALL_OPTS` | Additional `make install` options. |
| `RUBY_CONFIGURE_OPTS` | Additional `./configure` options (applies only to Ruby source). |
| `RUBY_MAKE_OPTS` | Additional `make` options (applies only to Ruby source). |
| `RUBY_MAKE_INSTALL_OPTS` | Additional `make install` options (applies only to Ruby source). |
2023-11-07 16:05:27 +01:00
| `NO_COLOR` | Disable ANSI colors in output. The default is to use colors for output connected to a terminal. |
| `CLICOLOR_FORCE` | Use ANSI colors in output even when not connected to a terminal. |
2013-09-01 14:26:03 +02:00
2017-07-10 08:49:08 +02:00
#### Applying Patches
2012-05-24 20:33:38 +02:00
2022-09-25 13:06:19 +02:00
Both `rbenv install` and `ruby-build` commands support the `-p/--patch` flag to apply a patch to the Ruby source code before building. Patches are read from standard input:
2013-12-20 19:06:50 +01:00
```sh
# applying a single patch
$ rbenv install --patch 1.9.3-p429 < /path/to/ruby.patch
# applying a patch from HTTP
$ rbenv install --patch 1.9.3-p429 < < (curl -sSL http://git.io/ruby.patch)
# applying multiple patches
$ cat fix1.patch fix2.patch | rbenv install --patch 1.9.3-p429
```
2017-07-10 08:49:08 +02:00
#### Checksum Verification
2012-11-15 23:51:58 +01:00
2022-09-25 13:06:19 +02:00
All Ruby definition files bundled with ruby-build include checksums for packages, meaning that all externally downloaded packages are automatically checked for integrity after fetching.
2012-11-15 23:51:58 +01:00
2022-09-25 13:06:19 +02:00
See the next section for more information on how to author checksums.
2012-11-15 23:51:58 +01:00
2017-07-10 08:49:08 +02:00
#### Package Mirrors
2012-11-15 23:51:58 +01:00
2019-10-09 14:08:21 +02:00
To speed up downloads, ruby-build fetches package files from a mirror hosted on
Amazon CloudFront. To benefit from this, the packages must specify their checksum:
2012-11-15 23:51:58 +01:00
2019-10-09 14:08:21 +02:00
```sh
# example:
install_package "ruby-2.6.5" "https://ruby-lang.org/ruby-2.6.5.tgz#< SHA2 > "
```
ruby-build will first try to fetch this package from `$RUBY_BUILD_MIRROR_URL/<SHA2>`
2019-10-09 14:11:12 +02:00
(note: this is the complete URL), where `<SHA2>` is the checksum for the file. It
will fall back to downloading the package from the original location if:
2019-10-09 14:08:21 +02:00
- the package was not found on the mirror;
- the mirror is down;
- the download is corrupt, i.e. the file's checksum doesn't match;
- no tool is available to calculate the checksum; or
- `RUBY_BUILD_SKIP_MIRROR` is enabled.
2012-11-15 23:51:58 +01:00
2019-10-09 14:08:21 +02:00
You may specify a custom mirror by setting `RUBY_BUILD_MIRROR_URL` .
2012-11-15 23:51:58 +01:00
2020-07-17 18:36:59 +02:00
If a mirror site doesn't conform to the above URL format, you can specify the
complete URL by setting `RUBY_BUILD_MIRROR_PACKAGE_URL` . It behaves the same as
`RUBY_BUILD_MIRROR_URL` except being a complete URL.
2019-10-09 14:08:21 +02:00
The default ruby-build download mirror is sponsored by
2015-07-18 08:47:45 +02:00
[Basecamp ](https://basecamp.com/ ).
2012-12-12 20:48:10 +01:00
2017-07-10 08:49:08 +02:00
#### Keeping the build directory after installation
2011-08-11 22:40:36 +02:00
2013-09-01 14:26:03 +02:00
Both `ruby-build` and `rbenv install` accept the `-k` or `--keep` flag, which
tells ruby-build to keep the downloaded source after installation. This can be
useful if you need to use `gdb` and `memprof` with Ruby.
2011-08-11 22:40:36 +02:00
2013-09-01 14:26:03 +02:00
Source code will be kept in a parallel directory tree `~/.rbenv/sources` when
using `--keep` with the `rbenv install` command. You should specify the
location of the source code with the `RUBY_BUILD_BUILD_PATH` environment
variable when using `--keep` with `ruby-build` .
2011-08-16 11:31:58 +02:00
2012-05-24 20:33:38 +02:00
## Getting Help
2011-09-14 19:38:34 +02:00
2013-09-01 14:26:03 +02:00
Please see the [ruby-build wiki][wiki] for solutions to common problems.
2017-08-23 10:07:25 +02:00
If you can't find an answer on the wiki, open an issue on the [issue tracker][].
Be sure to include the full build log for build failures.
2011-09-14 19:38:34 +02:00
2022-09-25 13:06:19 +02:00
[rbenv]: https://github.com/rbenv/rbenv#readme
2017-08-23 10:07:25 +02:00
[definitions]: https://github.com/rbenv/ruby-build/tree/master/share/ruby-build
[wiki]: https://github.com/rbenv/ruby-build/wiki
[build-env]: https://github.com/rbenv/ruby-build/wiki#suggested-build-environment
[issue tracker]: https://github.com/rbenv/ruby-build/issues