Convert Figma logo to code with AI

postmodern logochruby

Changes the current Ruby

2,892
193
2,892
98

Top Related Projects

16,470

Manage your app's Ruby environment

5,175

Ruby enVironment Manager (RVM)

23,996

Extendable version manager with support for Ruby, Node.js, Elixir, Erlang & more

A tool to download, compile, and install Ruby on Unix-like systems.

Quick Overview

Chruby is a lightweight Ruby version manager that allows users to easily switch between different Ruby versions. It provides a simple and minimalistic approach to managing Ruby environments, focusing on speed and ease of use.

Pros

  • Lightweight and fast, with minimal impact on shell startup time
  • Easy to install and configure, with no dependencies
  • Supports both system-wide and per-user installations
  • Integrates well with other Ruby tools and version managers

Cons

  • Doesn't automatically install Ruby versions (requires manual installation)
  • Limited features compared to more comprehensive Ruby version managers
  • May require additional setup for some advanced use cases
  • Not as widely adopted as some other Ruby version managers

Code Examples

  1. Switching Ruby versions:
chruby 2.7.2
  1. Listing available Ruby versions:
chruby
  1. Using a specific Ruby version in a script:
#!/usr/bin/env bash
source /usr/local/share/chruby/chruby.sh
chruby 3.0.0
ruby my_script.rb

Getting Started

  1. Install chruby (on macOS with Homebrew):
brew install chruby
  1. Add the following to your ~/.bashrc or ~/.zshrc:
source /usr/local/opt/chruby/share/chruby/chruby.sh
source /usr/local/opt/chruby/share/chruby/auto.sh
  1. Install Ruby versions (example using ruby-install):
ruby-install ruby 3.0.0
  1. Switch to the installed Ruby version:
chruby 3.0.0

Competitor Comparisons

16,470

Manage your app's Ruby environment

Pros of rbenv

  • More extensive ecosystem with plugins for additional functionality
  • Automatic version switching based on project-specific .ruby-version files
  • Seamless integration with popular Ruby version managers like ruby-build

Cons of rbenv

  • More complex setup and configuration process
  • Requires shell integration, which can potentially interfere with other tools
  • Slightly slower performance due to its shim-based approach

Code Comparison

rbenv:

# Install rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
# Add to PATH and initialize
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
# Install a Ruby version
rbenv install 3.0.0
# Set global Ruby version
rbenv global 3.0.0

chruby:

# Install chruby
wget -O chruby-0.3.9.tar.gz https://github.com/postmodern/chruby/archive/v0.3.9.tar.gz
tar -xzvf chruby-0.3.9.tar.gz
cd chruby-0.3.9/
sudo make install
# Add to shell configuration
echo 'source /usr/local/share/chruby/chruby.sh' >> ~/.bashrc
# Switch Ruby version
chruby 3.0.0

Both rbenv and chruby are popular Ruby version managers with their own strengths. rbenv offers a more feature-rich environment with automatic version switching, while chruby provides a simpler, lightweight solution with faster performance. The choice between them often depends on personal preference and specific project requirements.

5,175

Ruby enVironment Manager (RVM)

Pros of RVM

  • More feature-rich, offering gemset management and project-specific configurations
  • Larger community and longer history, potentially leading to better support
  • Provides automated installation of Ruby dependencies

Cons of RVM

  • More complex and can modify shell environment extensively
  • Slower to load and use due to its comprehensive feature set
  • Can sometimes conflict with other system-wide Ruby installations

Code Comparison

RVM:

rvm install 3.0.0
rvm use 3.0.0
rvm gemset create myproject
rvm use 3.0.0@myproject

chruby:

chruby 3.0.0

Key Differences

  • chruby is lightweight and focuses solely on switching Ruby versions
  • RVM offers a more comprehensive Ruby management solution
  • chruby requires manual Ruby installation, while RVM can automate this process
  • chruby has minimal impact on the shell environment compared to RVM
  • RVM includes gemset management, which chruby does not provide natively

Use Cases

  • Choose chruby for a minimalist approach and faster performance
  • Opt for RVM if you need extensive Ruby management features and don't mind the additional complexity
  • chruby is ideal for developers who prefer manual control over their Ruby environment
  • RVM is better suited for projects requiring isolated gemsets and automated setup
23,996

Extendable version manager with support for Ruby, Node.js, Elixir, Erlang & more

Pros of asdf

  • Supports multiple programming languages and tools, not just Ruby
  • Allows managing different versions of tools within the same project
  • Extensible plugin system for adding support for new languages/tools

Cons of asdf

  • More complex setup and configuration compared to chruby
  • Larger footprint and potentially slower performance
  • Steeper learning curve for users familiar with simpler version managers

Code Comparison

chruby:

source /usr/local/share/chruby/chruby.sh
chruby ruby-3.1.2

asdf:

asdf plugin add ruby
asdf install ruby 3.1.2
asdf global ruby 3.1.2

Summary

chruby is a lightweight Ruby version manager that focuses on simplicity and speed. It's easy to set up and use, making it ideal for developers who primarily work with Ruby.

asdf is a more versatile tool that can manage versions of multiple programming languages and tools. It offers greater flexibility and extensibility but comes with a slightly more complex setup and usage.

The choice between the two depends on your specific needs. If you work exclusively with Ruby, chruby might be sufficient. However, if you frequently switch between different languages and tools, asdf's broader support could be more beneficial.

A tool to download, compile, and install Ruby on Unix-like systems.

Pros of ruby-build

  • Offers a wider range of Ruby versions and implementations for installation
  • Integrates seamlessly with rbenv for a complete Ruby version management solution
  • Provides a plugin system for extending functionality

Cons of ruby-build

  • More complex setup and configuration compared to chruby
  • Requires additional steps to update Ruby versions

Code Comparison

chruby:

source /usr/local/share/chruby/chruby.sh
chruby ruby-3.0.0

ruby-build (with rbenv):

rbenv install 3.0.0
rbenv global 3.0.0
eval "$(rbenv init -)"

Key Differences

  • chruby is lightweight and focuses on switching between Ruby versions
  • ruby-build is part of a more comprehensive Ruby management ecosystem (rbenv)
  • chruby relies on system-wide Ruby installations, while ruby-build can install Rubies for specific users
  • ruby-build offers more granular control over Ruby versions and installations
  • chruby has a simpler learning curve and setup process

Both tools are popular choices for Ruby version management, with the choice often depending on specific project requirements and personal preferences. chruby is ideal for those seeking simplicity, while ruby-build (with rbenv) offers more advanced features for complex Ruby environments.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

chruby

CI

Changes the current Ruby.

Features

  • Updates $PATH.
    • Also adds RubyGems bin/ directories to $PATH.
  • Correctly sets $GEM_HOME and $GEM_PATH.
    • Users: gems are installed into ~/.gem/$ruby/$version.
    • Root: gems are installed directly into /path/to/$ruby/$gemdir.
  • Additionally sets $RUBY_ROOT, $RUBY_ENGINE, $RUBY_VERSION and $GEM_ROOT.
  • Optionally sets $RUBYOPT if second argument is given.
  • Calls hash -r to clear the command-lookup hash-table.
  • Fuzzy matching of Rubies by name.
  • Defaults to the system Ruby.
  • Optionally supports auto-switching and the .ruby-version file.
  • Supports bash and zsh.
  • Small (~100 LOC).
  • Has tests.

Anti-Features

  • Does not hook cd.
  • Does not install executable shims or require regenerating them after installing gems.
  • Does not require Rubies be installed into your home directory.
  • Does not automatically switch Rubies by default.
  • Does not require write-access to the Ruby directory in order to install gems.

Requirements

Install

wget https://github.com/postmodern/chruby/releases/download/v0.3.9/chruby-0.3.9.tar.gz
tar -xzvf chruby-0.3.9.tar.gz
cd chruby-0.3.9/
sudo make install

PGP

All releases are PGP signed for security. Instructions on how to import my PGP key can be found on my blog. To verify that a release was not tampered with:

wget https://github.com/postmodern/chruby/releases/download/v0.3.9/chruby-0.3.9.tar.gz.asc
gpg --verify chruby-0.3.9.tar.gz.asc chruby-0.3.9.tar.gz

setup.sh

chruby also includes a setup.sh script, which installs chruby. Simply run the script as root or via sudo:

sudo ./scripts/setup.sh

Homebrew

chruby can also be installed with homebrew:

brew install chruby

Or the absolute latest chruby can be installed from source:

brew install chruby --HEAD

Arch Linux

chruby is already included in the AUR:

yaourt -S chruby

Fedora Linux

chruby is available as an rpm on Fedora Copr.

FreeBSD

chruby is included in the official FreeBSD ports collection:

cd /usr/ports/devel/chruby/ && make install clean

Rubies

Manually

Chruby provides detailed instructions for installing additional Rubies:

ruby-install

You can also use ruby-install to install additional Rubies:

Installing to /opt/rubies or ~/.rubies:

ruby-install ruby
ruby-install jruby
ruby-install rubinius
ruby-install maglev

ruby-build

You can also use ruby-build to install additional Rubies:

Installing to /opt/rubies:

ruby-build 1.9.3-p392 /opt/rubies/ruby-1.9.3-p392
ruby-build jruby-1.7.3 /opt/rubies/jruby-1.7.3
ruby-build rbx-2.0.0-rc1 /opt/rubies/rubinius-2.0.0-rc1
ruby-build maglev-1.0.0 /opt/rubies/maglev-1.0.0

Configuration

Add the following to the ~/.bashrc or ~/.zshrc file:

source /usr/local/share/chruby/chruby.sh

macOS

source $HOMEBREW_PREFIX/opt/chruby/share/chruby/chruby.sh # Or run `brew info chruby` to find out installed directory

Note: macOS does not automatically execute ~/.bashrc, instead try adding to /etc/bashrc.

System Wide

If you wish to enable chruby system-wide, add the following to /etc/profile.d/chruby.sh:

if [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ]; then
  source /usr/local/share/chruby/chruby.sh
  ...
fi

This will prevent chruby from accidentally being loaded by /bin/sh, which is not always the same as /bin/bash.

Rubies

When chruby is first loaded by the shell, it will auto-detect Rubies installed in /opt/rubies/ and ~/.rubies/. After installing new Rubies, you must restart the shell before chruby can recognize them.

For Rubies installed in non-standard locations, simply append their paths to the RUBIES variable:

source /usr/local/share/chruby/chruby.sh

RUBIES+=(
  /opt/jruby-1.7.0
  "$HOME/src/rubinius"
)

Migrating

If you are migrating from another Ruby manager, set RUBIES accordingly:

RVM

RUBIES+=(~/.rvm/rubies/*)

rbenv

RUBIES+=(~/.rbenv/versions/*)

rbfu

RUBIES+=(~/.rbfu/rubies/*)

Auto-Switching

If you want chruby to auto-switch the current version of Ruby when you cd between your different projects, simply load auto.sh in ~/.bashrc or ~/.zshrc:

source /usr/local/share/chruby/chruby.sh
source /usr/local/share/chruby/auto.sh

Note: macOS does not automatically source ~/.bashrc. Bash users should create the file ~/.bash_profile with following content:

if [[ -f ~/.profile ]]; then
    source ~/.profile
fi

if [[ $- == *i* ]] && [[ -f ~/.bashrc ]]; then
    source ~/.bashrc
fi

so that the terminal loads ~/.bashrc and ~/.profile (the POSIX one). If ~/.bash_profile exists, Bash users should append the above lines to ~/.bash_profile as needed.

chruby will check the current and parent directories for a .ruby-version file. Other Ruby switchers also understand this file: https://gist.github.com/1912050

If you want to automatically run the version of a gem executable specified in your project's Gemfile, try rubygems-bundler.

Default Ruby

Once you have loaded chruby.sh and/or auto.sh in your shell configuration, you can also set a default Ruby. Simply call the chruby function in ~/.bash_profile or ~/.zprofile:

chruby ruby-1.9

If you have enabled auto-switching, simply create a .ruby-version file:

echo "ruby-1.9" > ~/.ruby-version

RubyGems

Gems installed as a non-root user via gem install will be installed into ~/.gem/$ruby/X.Y.Z. By default, RubyGems will use the absolute path to the currently selected ruby for the shebang of any binstubs it generates. In some cases, this path may contain extra version information (e.g. ruby-2.0.0-p451). To mitigate potential problems when removing rubies, you can force RubyGems to generate binstubs with shebangs that will search for ruby in your $PATH by using gem install --env-shebang (or the equivalent short option -E). This parameter can also be added to your gemrc file.

Integration

For instructions on using chruby with other tools, please see the wiki:

Examples

List available Rubies:

$ chruby
   ruby-1.9.3-p392
   jruby-1.7.0
   rubinius-2.0.0-rc1

Select a Ruby:

$ chruby 1.9.3
$ chruby
 * ruby-1.9.3-p392
   jruby-1.7.0
   rubinius-2.0.0-rc1
$ echo $PATH
/home/hal/.gem/ruby/1.9.3/bin:/opt/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/bin:/opt/rubies/ruby-1.9.3-p392/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/hal/bin:/home/hal/bin
$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.8.23
  - RUBY VERSION: 1.9.3 (2013-02-22 patchlevel 392) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/hal/.gem/ruby/1.9.3
  - RUBY EXECUTABLE: /opt/rubies/ruby-1.9.3-p392/bin/ruby
  - EXECUTABLE DIRECTORY: /home/hal/.gem/ruby/1.9.3/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /home/hal/.gem/ruby/1.9.3
     - /opt/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gem" => "--no-rdoc"
  - REMOTE SOURCES:
     - http://rubygems.org/

Switch to JRuby in 1.9 mode:

$ chruby jruby --1.9
$ ruby -v
jruby 1.7.0 (1.9.3p203) 2012-10-22 ff1ebbe on OpenJDK 64-Bit Server VM 1.7.0_09-icedtea-mockbuild_2012_10_17_15_53-b00 [linux-amd64]

Switch back to system Ruby:

$ chruby system
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/hal/bin

Run a command under a Ruby with chruby-exec:

$ chruby-exec jruby -- gem update

Switch to an arbitrary Ruby on the fly:

$ chruby_use /path/to/ruby

Uninstall

  1. Remove or comment out chruby from your shell configuration.
  2. Restart your shell (ex: exec $SHELL).
  3. sudo make uninstall

Alternatives

* Deprecated in favor of chruby.

Endorsements

yeah chruby is nice, does the limited thing of switching really good, the only hope it never grows

-- Michal Papis of RVM

I just looooove chruby For the first time I'm in total control of all aspects of my Ruby installation.

-- Marius Mathiesen

Written by Postmodern, it's basically the simplest possible thing that can work.

-- Steve Klabnik

So far, I'm a huge fan. The tool does what it advertises exactly and simply. The small feature-set is also exactly and only the features I need.

-- Patrick Brisbin

I wrote ruby-version; however, chruby is already what ruby-version wanted to be. I've deprecated ruby-version in favor of chruby.

-- Wil Moore III

Credits

  • mpapis for reviewing the code.
  • havenwood for handling the homebrew formula.
  • zendeavor for style fixes.
  • #bash, #zsh, #machomebrew for answering all my questions.