• Stars
    star
    2,832
  • Rank 15,394 (Top 0.4 %)
  • Language
    Shell
  • License
    MIT License
  • Created almost 12 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Changes the current Ruby

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.

More Repositories

1

ruby-install

Installs Ruby, JRuby, Rubinius, TruffleRuby or MRuby
Shell
1,854
star
2

spidr

A versatile Ruby web spidering library that can spider a site, multiple domains, certain links or infinitely. Spidr is designed to be fast and easy to use.
Ruby
763
star
3

ruby-nmap

A Ruby interface to nmap, the exploration tool and security / port scanner. Allows automating nmap and parsing nmap XML files.
Ruby
295
star
4

nokogiri-diff

Calculate the differences between two XML/HTML documents.
Ruby
129
star
5

command_kit.rb

A Ruby toolkit for building complete and robust CLI commands.
Ruby
119
star
6

gem_home

A tool for changing your $GEM_HOME
Shell
118
star
7

digest-crc

A Cyclic Redundancy Check (CRC) library for Ruby.
C
89
star
8

chgems

DEPRECATED: use gem_home instead!
Shell
87
star
9

kramdown-man

Allows you to write man pages in pure markdown.
Ruby
87
star
10

hexdump.rb

Fully Featured and Fast hexdumping for Ruby.
Ruby
77
star
11

rubygems-tasks

Simple Rake tasks for managing and releasing Ruby projects.
Ruby
77
star
12

raingrams

A flexible and general-purpose ngrams library written in Ruby. Raingrams supports ngram sizes greater than 1, text/non-text grams, multiple parsing styles and open/closed vocabulary models.
Ruby
70
star
13

ruby-versions

A common repository of ruby version metadata
Shell
69
star
14

net-http-server

A pure Ruby HTTP Server.
Ruby
68
star
15

sketches

A Ruby library for live programming and code reloading.
Ruby
65
star
16

deployml

DeploYML is a simple deployment solution that uses a single YAML file.
Ruby
58
star
17

ruby-cli-boilerplate

Zero-dependency Ruby CLI boilerplate code
Ruby
58
star
18

command_mapper.rb

Maps external commands and options to Ruby classes
Ruby
54
star
19

ffi-hunspell

Ruby FFI bindings for Hunspell.
Ruby
48
star
20

combinatorics.rb

Bringing (more) Combinatorics to Ruby.
Ruby
46
star
21

wordlist.rb

A Ruby library and CLI for generating and working with wordlists.
Ruby
45
star
22

ffi-pcap

Ruby FFI bindings for libpcap.
Ruby
43
star
23

flv-dl

Downloads Flash Video (flv) files from web-pages.
Ruby
33
star
24

gscraper

A web-scraping interface to various Google Services.
Ruby
33
star
25

rprogram

A library for creating wrappers around command-line programs.
Ruby
32
star
26

ffi-libc

Useful Ruby FFI bindings for libc
Ruby
31
star
27

libBERT

A C library for encoding and decoding BERT (Binary ERlang Term)
C
31
star
28

scm

Ruby interface to common SCMs
Ruby
30
star
29

tdiff

Calculates the differences between two tree-like structures.
Ruby
29
star
30

wsoc

The Web Spider Obstacle Course
Ruby
24
star
31

cparser

A pure Ruby ANSI C Parser.
Ruby
23
star
32

npm_scan

Scans npmjs.org for npm packages that can be taken over
Crystal
19
star
33

multi_markdown

multi_markdown allows projects to use a wide variety of Markdown Processors, without having to depend on a specific one.
Ruby
19
star
34

dm-visualizer

Visualizes the Models, Properties and Relationships defined in a DataMapper based Ruby project.
Ruby
18
star
35

ffi-msgpack

Ruby FFI bindings to msgpack
Ruby
16
star
36

uri-query_params

Access the query parameters of a URI, just like $_GET in PHP.
Ruby
16
star
37

mruby-embed

Example project that embeds mruby.
Ruby
15
star
38

ffi-extractor

Ruby FFI bindings for libextractor
Ruby
15
star
39

yard-dm

A YARD plugin for parsing DataMapper syntax.
Ruby
14
star
40

dotfiles

Development Environment Configuration
Vim Script
13
star
41

chars.rb

A Ruby library for working with various character sets, recognizing text and generating random text from specific character sets.
Ruby
13
star
42

example-activerecord-lib

Example usage of ActiveRecord in a Ruby library (not a Rails app or Rails engine)
Ruby
13
star
43

test

A collection of boilerplate code and projects for testing ideas
Ruby
12
star
44

ffi-http-parser

Ruby FFI bindings to the http-parser library.
Ruby
12
star
45

dm-chunked_query

Allows performing chunked queries with DataMapper.
Ruby
12
star
46

yard-spellcheck

Spellcheck your YARD documentation.
Ruby
12
star
47

dm-is-authenticatable

A DataMapper plugin for adding authentication and encrypted passwords to your DataMapper models.
Ruby
12
star
48

object_loader

Load Objects from Ruby files without having to use YAML or define classes named like the file.
Ruby
11
star
49

parameters

Parameters allows you to add annoted variables to your classes which may have configurable default values.
Ruby
10
star
50

shodan-ruby

A Ruby interface to SHODAN, a computer search engine.
Ruby
10
star
51

vim-yard

A VIM plugin to add syntax highlighting to your YARD tags.
Vim Script
10
star
52

jekyll-static-import

[WIP] Import static HTML websites into Jekyll
Ruby
10
star
53

ruby-masscan

A Ruby interface to masscan, an Internet-scale port scanner.
Ruby
9
star
54

v4l2.cr

Crystal bindings for V4L2
Crystal
9
star
55

ffi-ptrace

Ruby FFI bindings for ptrace
Ruby
9
star
56

ffi-udis86

Ruby FFI bindings for udis86
Ruby
8
star
57

env_ext

Provides a Ruby interface to common environment variables
Ruby
8
star
58

pullr

A Ruby library for quickly pulling down or updating any Repository.
Ruby
8
star
59

ffi-bit_masks

FFI plugin which adds support for bit-wise fields (or flags) to FFI.
Ruby
8
star
60

python-pickle.rb

A modern Ruby implementation of the Python Pickle serialization format.
Ruby
8
star
61

ruby-amass

A Ruby interface to amass.
Ruby
8
star
62

pcap-ffi

Ruby FFI bindings for libpcap
Ruby
7
star
63

dm-is-read_only

A DataMapper plugin for making a Model absolutely read-only.
Ruby
7
star
64

gemspec.yml

Write your gemspecs in plain YAML
Ruby
7
star
65

crystal-crc

CRC (Cyclic Redundancy Check) algorithms for Crystal.
Crystal
7
star
66

ruby-ncrack

A Ruby interface to Ncrack.
Ruby
7
star
67

sinatra-json

sinatra/json extracted from sinatra-contrib.
Ruby
7
star
68

chars.cr

A Crystal library for working with various character sets, recognizing text and generating random text from specific character sets.
Crystal
7
star
69

nokogiri-ext

Useful extensions to nokogiri
Ruby
7
star
70

command_mapper-gen.rb

Generates command_mapper Ruby classes for a given command
Ruby
6
star
71

phishing-training-sigs

A crowd sourced list of phishing training simulators and their signatures
6
star
72

donatello

Highly configurable graphs ontop of Raphael.js.
JavaScript
6
star
73

ruby-yasm

A Ruby interface to YASM.
Ruby
5
star
74

fake_io.rb

A mixin module for creating fake IO-like classes.
Ruby
5
star
75

open_namespace

Allows namespaces to load constants on-demand
Ruby
5
star
76

zcow

Manages temporary zram backed COW (Copy On Write) devices.
Shell
5
star
77

postmodern.github.io

website | blog
HTML
5
star
78

dm-is-counter_cacheable

A DataMapper plugin for adding counter-cache properties to related models.
Ruby
5
star
79

ffi-libnet

Ruby FFI bindings for libnet
Ruby
4
star
80

dm-is-predefined

A DataMapper plugin for adding predefined resources to Models.
Ruby
4
star
81

ffi-libelf

Ruby FFI bindings for libelf
Ruby
4
star
82

ffi-bert

FFI bindings to libBERT, a library for encoding and decoding BERT data.
Ruby
4
star
83

badger

A fear-less, robust and portable systems management solution.
Ruby
4
star
84

ffi-cairo

Ruby FFI bindings for Cairo
Ruby
4
star
85

ffi-sundown

Ruby FFI bindings for the libupskirt markdown library.
Ruby
4
star
86

threaded_server

A generic TCP Server with a fixed-size Thread Pool.
Ruby
4
star
87

hunspell.cr

Crystal bindings for libhunspell. Crystal port of the Ruby ffi-hunspell gem.
Crystal
4
star
88

dm-types-legacy

A collection of legacy data types for DataMapper.
Ruby
4
star
89

command_kit-completion

Generate shell completions for command_kit commands
Ruby
4
star
90

netfilter-ffi

Ruby FFI bindings for netfilter libraries
Ruby
3
star
91

data_paths

DataPaths is a library to manage the paths to data/ directories within projects.
Ruby
3
star
92

ffi-llvm

Ruby FFI bindings for LLVM
Ruby
3
star
93

badger-ruby

A Ruby client for communicating with Badger servers.
Ruby
3
star
94

cvelist.rb

A Ruby library for parsing the CVE JSON in the cvelist git repository
Ruby
3
star
95

dm-is-checksumed

Adds checksum properties to DataMapper Models.
Ruby
3
star
96

yard-parameters

yard-parameters is a plugin for YARD for parsing parameters.
Ruby
3
star
97

rtoolchain

RToolchain is a Ruby interface to various toolchains and other compilers or assemblers. Currently RToolchain supports the GNU toolchain and NASM.
Ruby
3
star
98

hoe-yard

A Hoe plugin for generating YARD documentation
Ruby
3
star
99

rtranscoder

A Rubyful interface to various transcoding utilities
Ruby
3
star
100

cinch-commands

Allows defining multiple commands within a Cinch Plugin.
Ruby
3
star