Skip to content

Instantly share code, notes, and snippets.

@blacktm
Last active November 28, 2024 20:35
Show Gist options
  • Select an option

  • Save blacktm/8302741 to your computer and use it in GitHub Desktop.

Select an option

Save blacktm/8302741 to your computer and use it in GitHub Desktop.
A Bash script to install Ruby on the Raspberry Pi
#!/bin/bash
# --------------------------------------------------------------------------------------------
# Installs Ruby using rbenv/ruby-build on the Raspberry Pi (Raspbian)
#
# Run from the web:
# bash <(curl -s https://gist.githubusercontent.com/blacktm/8302741/raw/install_ruby_rpi.sh)
# --------------------------------------------------------------------------------------------
# Set the Ruby version you want to install
RUBY_VERSION=3.3.0
# Welcome message
echo -e "
This will install Ruby using rbenv/ruby-build.
It will take about 2 hours to compile on the original Raspberry Pi,
35 minutes on the second generation, and 16 minutes on the third.\n"
# Prompt to continue
read -p " Continue? (y/n) " ans
if [[ $ans != "y" ]]; then
echo -e "\nQuitting...\n"
exit
fi
echo
# Time the install process
START_TIME=$SECONDS
# Check out rbenv into ~/.rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
# Add ~/.rbenv/bin to $PATH, enable shims and autocompletion
read -d '' String <<"EOF"
# rbenv
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
EOF
# Save to ~/.bashrc
echo -e "\n${String}" >> ~/.bashrc
# Enable rbenv for current shell
eval "${String}"
# Install ruby-build as an rbenv plugin, adds `rbenv install` command
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
# Install dependencies
# See: https://github.com/rbenv/ruby-build/wiki#suggested-build-environment
sudo apt update
sudo apt install -y autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev
# Install Ruby, don't generate RDoc to save lots of time
CONFIGURE_OPTS="--disable-install-doc --enable-shared" rbenv install $RUBY_VERSION --verbose
# Set Ruby as the global default
rbenv global $RUBY_VERSION
# Don't install docs for gems (saves lots of time)
echo "gem: --no-document" > ~/.gemrc
# Reminder to reload the shell
echo -e "\nReload the current shell to get access to rbenv using:"
echo " source ~/.bashrc"
# Print the time elapsed
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo -e "\nFinished in $(($ELAPSED_TIME/60/60)) hr, $(($ELAPSED_TIME/60%60)) min, and $(($ELAPSED_TIME%60)) sec\n"
@facastagnini
Copy link
Copy Markdown

Why to take all that work when you can simply do:

  cd /usr/src/
  wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.2.tar.gz
  tar -xvzf ruby-2.2.2.tar.gz
  cd ruby-2.2.2
  ./configure --enable-shared --disable-install-doc --disable-install-rdoc --disable-install-capi
  make install

Reference: https://github.com/facastagnini/raspberry-bitcoin/blob/master/bootstrap.sh

@Donavan
Copy link
Copy Markdown

Donavan commented Jun 27, 2015

@blacktm you should really add "--enable-shared" to the configure opts.
@facastagnini what you posted doesn't install of the dependencies not rbenv

@delneet
Copy link
Copy Markdown

delneet commented Oct 30, 2015

👍

@causztic
Copy link
Copy Markdown

thanks for this!

@LimeBlast
Copy link
Copy Markdown

This is excellent, thank you. It took about 35 minutes on my rPi 2.

It didn't load rbenv for the current shell, but re-launching terminal fixed that right up.

@daz
Copy link
Copy Markdown

daz commented Mar 31, 2016

16 minutes on RPI 3

@medright
Copy link
Copy Markdown

medright commented Aug 4, 2016

Thanks! Just got done running on my second pi, 19min 9sec on this one, 33min on the other, this was very smooth.

@davecozz
Copy link
Copy Markdown

davecozz commented Sep 9, 2016

Great script, thanks. One suggestion: make sure git is installed before running git commands.
sudo apt-get update && sudo apt-get install -y git-core

Other than that it worked great on my Rpi 3
Finished in 0 hr, 15 min, and 56 sec

@MitchDresdner
Copy link
Copy Markdown

Worked great for getting Ruby running on my Pi3

But ...

I had to come here to get rails working:

http://railsapps.github.io/installrubyonrails-ubuntu.html

Copy link
Copy Markdown

ghost commented Nov 20, 2016

"Oh no! Something has gone wrong."

My Output:

pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.4.32-v7+ #924 SMP Tue Nov 15 18:11:28 GMT 2016 armv7l GNU/Linux

pi@raspberrypi:~ $ sudo sh install_ruby_rpi.sh
Building dependency tree
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree
Reading state information... Done

.....
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
install_ruby_rpi.sh: 53: install_ruby_rpi.sh: rbenv: not found
install_ruby_rpi.sh: 56: install_ruby_rpi.sh: rbenv: not found
-e
Reload the current shell to get access to rbenv using:
source ~/.bashrc
install_ruby_rpi.sh: 66: install_ruby_rpi.sh: arithmetic expression: expecting primary: " - "

Copy link
Copy Markdown

ghost commented Nov 20, 2016

Note:

pi@raspberrypi: sudo apt-get install ruby-full

pi@raspberrypi:~ $ ruby -v
ruby 2.1.5p273 (2014-11-13) [arm-linux-gnueabihf]
🤘
Source: Openshift

@ministryman
Copy link
Copy Markdown

@Robertthcan: It's too late, but I ran into the same problem (after trying to install ruby and upgrading it to 2.4, and borking my system)

sudo apt-get install rbenv curl

I added curl just in case. The error output showed that the script could not find rbenv. I assumed it wasn't installed for some reason, so I did it.

Then Re run the script.

Copy link
Copy Markdown

ghost commented Jun 10, 2017

took 44 minutes on a RPi 3B but helped a lot!! thanks

@racecarparts
Copy link
Copy Markdown

racecarparts commented Aug 5, 2017

Great gist! Thanks!
Raspberry Pi Zero W:
Finished in 1 hr, 26 min, and 24 sec

@anon489
Copy link
Copy Markdown

anon489 commented Aug 25, 2017

Thanks, very easy to install the latest version of Ruby onto my Raspberry Pi. Took 2 hr, 21 min, and 42 sec on an Model B+ with Raspbian Stretch.

@pjha1994
Copy link
Copy Markdown

My version of ruby is still 2.1:

pi@raspberrypi:~ $ ruby -v
ruby 2.4.2p198 (2017-09-14 revision 59899) [armv7l-linux-eabihf]

It installed successfully, without any errors. I had ruby installed earlier via apt-get. How do I get access tot he new version to work? Do I need to add some path?

@DeafScribe
Copy link
Copy Markdown

Pi3, 35 minutes and 28 seconds. My setup reported version 2.1 immediately after the install, but reports 2.5.0 after a reboot. Thank you, sir!

@DirtyScience
Copy link
Copy Markdown

Pi3, Raspbian Lite (Stretch), 14min/11sec. No reported errors. Nice job!

@yjchieng
Copy link
Copy Markdown

yjchieng commented Jun 4, 2018

pi3b+, Raspbian, Ruby 2.4.4, 17minutes.

@erikyuzwa
Copy link
Copy Markdown

This gist is unreal. Amazing work, thanks a lot @blacktm

Pi3 on Jesse (I think - I'm a n00b). No errors. This just worked. Roughly 30 minutes (didn't really time it), but I wouldn't care if it took 4 hours.

@alxprogrmz
Copy link
Copy Markdown

I don't care how long it takes as long as it worked. I bet he took his precious time writing this wonderful script. Thank you so much for your contribution. I wish I could write a script like you.

@johhnry
Copy link
Copy Markdown

johhnry commented Jun 4, 2019

I have the following warning messages :

  • install_ruby_rpi.sh: 19: install_ruby_rpi.sh: [[: not found
  • The installation stopped and it says :
Installed ruby-2.6.3 to /home/pi/.rbenv/versions/2.6.3

~
-e 
Reload the current shell to get access to rbenv using:
  source ~/.bashrc
install_ruby_rpi.sh: 66: install_ruby_rpi.sh: arithmetic expression: expecting primary: " - "`

@osullii
Copy link
Copy Markdown

osullii commented Jul 6, 2019

Thanks a million @blacktm for creating this bash script. Ruby 2.6 downloaded for me in 22 minutes. No stress. :-)

Wonderful script. Thank you again for posting.

@donrestarone
Copy link
Copy Markdown

donrestarone commented Mar 17, 2020

Hey @blacktm! First of all amazing work. This is a solid script. But I'm having an issue getting it to work on a Raspberry Pi 4 running Raspbian Buster 2020-2-13 version. Everything gets installed correctly, but when I try to invoke Rails s (or sidekiq or any gem) I get a massive error 1000+ lines long that ends with "You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome."
Any ideas?

@FrangaL
Copy link
Copy Markdown

FrangaL commented Nov 30, 2021

Calculate total time compilation.

total_time() {
  local T=$1
  local H=$((T/60/60%24))
  local M=$((T/60%60))
  local S=$((T%60))
  printf '\nFinished in '
  [[ $H -gt 0 ]] && printf '%d hours ' $H
  [[ $M -gt 0 ]] && printf '%d minutes ' $M
  [[ $D -gt 0 || $H -gt 0 || $M -gt 0 ]] && printf 'and '
  printf '%d seconds\n' $S
}

total_time $SECONDS

@fgirolami29
Copy link
Copy Markdown

fgirolami29 commented May 26, 2024

tnk u
SET RUBY VIA ARGV OR FALLBACK TO DEFAULT

Set the default Ruby version

DEFAULT_RUBY_VERSION=3.3.0

Set the Ruby version from command-line argument or fallback to default

RUBY_VERSION=${1:-$DEFAULT_RUBY_VERSION}

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