Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install Ruby 3 and Bundler

Install Ruby 3 and Bundler

Resources:

1. Install Ruby

Ubuntu/Debian

Install Ruby and dev headers.

$ sudo apt-get update
$ sudo apt-get install ruby ruby-dev

Test it:

$ ruby --version
ruby 3.0.1p64 ...

macOS

For more info, also my gist instructions to set up Ruby, Bundler and a project-level Jekyll on macOS Catalina and higher.

See ruby (for @3.0) and ruby@2.7 formulae on the Homebrew site.

Install Ruby:

$ brew install ruby@2.7
$ # OR
$ brew install ruby@3.0

Add to your PATH in Bash config (.bashrc) or ZSH config (.zshrc) to make the Brew install of Ruby accessible.

export PATH="/usr/local/opt/ruby/bin:$PATH"

Start a new terminal tab.

Test Ruby:

$ ruby --version
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-darwin19]

Windows

As recommended on the Ruby homepage, download and run this installer:

2. Install Bundler

The instructions below are macOS/Linux.

Note that you might already have Bundler installed with your system Ruby.

$ /usr/bin/bundler --version
Bundler version 1.17.2

But you might prefer to use a user-level Bundler. Especially since from macOS Catalina, the system Ruby and gems (like Bundler) are locked, to protect you from yourself.

$ /usr/local/opt/ruby/bin/bundler --version
Bundler version 2.2.15

See two options below for installing.

User install

Install Bundler at the user level, so it can be used across projects.

$ gem install bundler --user-install

Or upgrade it.

$ gem install bundler --user-install --update

With the user install flag, you will install for your user in one of these locations:

  • Ruby 2
    ~/.gem/ruby/2.7.0/bin/bundler
    
  • Ruby 3
    ~/.local/share/gem/ruby/3.0.0/bin/bundler
    

System install

For Ruby 3, this part is unnecessary as Bundler gets installed for you.

To install system-wide, you can do this:

$ gem install bundler

If you lack permissions to install here, you'll get an error. Then you'll have to install with root access, which you should avoid in general unless you really know what you are doing. Anyway here it is:

$ sudo gem install bundler

Or set the permissions of the target directory owned by root to be 755, so that all users can edit it. Then install into it, without sudo.

$ sudo chmod o+w /usr/local/
$ gem install bundler

Omitting the --user-install flag will install to directory owned by root but that is shared i.e. intended for all users on the machine to read.

/usr/local/opt/ruby/bin/bundler

3. Add gems directory to PATH

This step is not needed for Windows or if you don't install gems at the user level

If you used the --user-install flag, then you installed to a user gem directory which is not in your PATH yet.

Therefore add this to your shell config. This will use Ruby itself to figure out what the appropriate path such as:

  • ~/.gem/ruby/2.7.0/bin, or
  • ~/.local/share/gem/ruby/3.0.0/bin

Use this to figure out that path for you and add it to your PATH.

if which ruby > /dev/null && which gem > /dev/null; then
  GEM_PATH="$(ruby -r rubygems -e 'puts Gem.user_dir')/bin"
  export PATH="$GEM_PATH:$PATH"
fi

Then start a new terminal tab.

4. Check

Check that Bundler is accessible.

$ which bundle
$ bundle --version

Now you can work in a Ruby or Jekyll project and install gems in the project using Bundler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment