Skip to content

Instantly share code, notes, and snippets.

@MichaelCurrin
Last active September 15, 2024 21:17
Show Gist options
  • Save MichaelCurrin/fb758aea4d35e03b9ed093afddf4e7ec to your computer and use it in GitHub Desktop.
Save MichaelCurrin/fb758aea4d35e03b9ed093afddf4e7ec to your computer and use it in GitHub Desktop.
Install Ruby 3 and Bundler

Install Ruby 3 and Bundler

Resources:

1. Install Ruby

Linux - Ubuntu/Debian

Install Ruby and dev headers.

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

Test Ruby:

$ ruby --version
ruby 3.0.1p64 ...

If the install step with apt-get install ruby tries to install Ruby 2 and finds it installed already, then uninstall Ruby 2 and install Ruby 3:

$ sudo apt-get remove ruby2.0 ruby2.0-dev
$ sudo apt-get install ruby3.0 ruby3.0-dev

Or, if you wish to keep Ruby 2 installed, just make sure Ruby 3 is the default. Your symlinks should look like this:

/usr/bin/ruby3.0
/usr/bin/ruby -> ruby3.0
/bin/ruby -> ruby3.0

macOS

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

See the ruby (for @3.0) and [email protected] formulae on the Homebrew site.

Install Ruby:

$ brew install [email protected]
$ # OR
$ brew install [email protected]

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 for 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 stop you from breaking your OS.

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

See two options below for installing.

User-level 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-wide 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, 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 such as with a trusted package. 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.

@weiqingzhangmkt
Copy link

The issue resolved 😃.

How? Could you elaborate? Thank you

@MichaelCurrin
Copy link
Author

My guide already covers how to uninstall Ruby 2 and install Ruby 3 for Linux/APT and macOS/Homebrew. If there is something missing then check Ruby articles or documentation.

@parth721
Copy link

How? Could you elaborate? Thank you

Hi @weiqingzhangmkt , sorry for the delay. The issue I was faced while installing ruby is because the command I was running, it install the version 2.7.0.0 (maybe it's a default version of ruby).
simply just remove the old version : sudo apt purge ruby
update & ugrade : sudo apt update && apt upgrade -y
install RVM

check the ruby version ruby -v. if it not showing anything just run rvm list to check the which is your default ruby version. That means ruby3.0.0 is installed. Just customize the bash shell for loading RVM into a shell session before setting the PATH
vim ~/.bashrc then [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" save the changes & quit.

Screenshot from 2023-11-19 15-48-04

I hope it works for you. Happy learning :)

@MichaelCurrin
Copy link
Author

If you just need the latest version of Ruby, just using the plain ruby3.0 with apt as in my guide and that will be sufficient. If you want to have multiple versions of Ruby installed such as for some projects that need 3.1 and 3.1 and 2.7, then using RVM (Ruby Version Manager) makes sense. But it also needs additional setup as above.

@MichaelCurrin
Copy link
Author

Note that my guide already covered how to remove with sudo apt-get remove ruby2.0 (or maybe just ruby?). Using purge as suggested above does the same thing but also removes configuration files.

@parth721
Copy link

I tried it out, but for me it install v2.7.0. And with specifying the version doesn't gave me the desired output. So I go with RVM. 🙂 I am grateful for your guide & active support towards contributors.

@MichaelCurrin
Copy link
Author

Sometimes there are system limitations, like Debian doesn't let you install a certain version of Python unless you update the entire OS, or go and find the URL for the debian repo and configure that.

So it may be an outdated OS you have that refuses to get new Ruby.

@sandipghosal
Copy link

Try the following:
sudo snap install ruby --classic

This should install ruby version >= 3.3.1.

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