Skip to content

Instantly share code, notes, and snippets.

@MichaelCurrin
Last active January 4, 2024 18:30
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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

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 ...

If the install step 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 want to keep Ruby 2 installed, just make sure Ruby 3 is your system 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 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.

@DevBaccon
Copy link

for ruby it only gives version 2.7.4 is that normal

@MichaelCurrin
Copy link
Author

MichaelCurrin commented Jul 23, 2023

That's a few years old but it's still valid.

If you want the features or performance of Ruby 3, then make sure to upgrade Ruby as per steps above (I extended for Linux and not just macOS to handle both versions). Then continue with the guide.

Also please provide more info like what OS you are on, if you are still stuck.

@parth721
Copy link

parth721 commented Aug 5, 2023

Hi, I tried to install Ruby 3 so that I can install Jekyll & bundler in zorin 16.3.
But unable to use ruby version 2.5 but Jekyll doesn't support that. Is there any way to fix it

@MichaelCurrin
Copy link
Author

@parth721 I don't fully understand but you can install the latest Ruby 2.X or 3.X on the major OS's and both will work with Jekyll. You'll have to research options for Zorin specifically.

@parth721
Copy link

parth721 commented Sep 2, 2023

Hi @MichaelCurrin I am facing this issue. Looking for resouces how to update from from 2.7.0.0 to 3.0.0 on ubuntu 20.04.

ERROR:  Error installing jekyll:
	The last version of sass-embedded (~> 1.54) to support your Ruby & RubyGems was 1.63.6. Try installing it with `gem install sass-embedded -v 1.63.6` and then running the current command again
	sass-embedded requires Ruby version >= 3.0.0. The current ruby version is 2.7.0.0.

@parth721
Copy link

parth721 commented Sep 2, 2023

The issue resolved 😃.

@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.

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