Install Ruby With rbenv
I attempted to install Ruby with rbenv on a Ubuntu Linux 22.04 AWS EC2 VM, having used only rvm in the past, and this was my experience…
First, I see from the rbenv official docs that I have to use the OS package manager - this gives me a version (rbenv v.1.1.2 - released March 25, 2019) older than the latest (rbenv v.1.2.0 - released September 29, 2021), which was released before CoVID became a pandemic.
sudo apt update
sudo apt install -y rbenv
Sounds like we then need a simple rbenv install 3.2.1
, right? Not quite… it turns out that out-of-the-box, rbenv 1.1.2 has some really old versions of Ruby available. So, the next thing I do is rbenv init
to create a ~/.rbenv
directory, add the .bashrc
entry eval "$(rbenv init -)"
and then source ~/.bashrc
. Then, I install ruby-build:
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
rbenv install --list
now shows the newer versions of Ruby so I do a rbenv install 3.2.1
.
…and it complains with:
tmp/ruby-build.20230223101000.3977.vvJcC0/ruby-3.2.1/lib/yaml.rb:3: warning: It seems your ruby installation is missing psych (for YAML output)
A Google search for the error takes me to a StackOverflow thread for a similar problem with rvm (we are using rbenv) and the solution for Ubuntu users is to install the libtool package with sudo apt install -y libtool
. I still get the above error so I try setting the GEM_HOME environment variable to an empty path so it doesn’t try to pick up psych from the system ruby installation:
mkdir ~/.rbenv/gems
export GEM_HOME=/home/ubuntu/.rbenv/gems
export PATH=$GEM_HOME/bin:$PATH
At this point, the AWS web console stopped responding while waiting for some output so I don’t know if it would have worked at that point or not. However, I see that on StackOverflow somebody did a sudo apt install libyaml-dev
. Also, I see the ruby-build docs on GitHub state that I have to install the prerequisites manually, which for Ubuntu is:
apt install -y autoconf bison patch build-essential rustc libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libgmp-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev uuid-dev
After installing the prerequisites, I proceed to set the GEM_HOME and PATH variables, and then run a rbenv install 3.1.2
… and SUCCESS!
I now have a reproducible set of steps to install Ruby with rbenv:
sudo apt install -y rbenv autoconf bison patch build-essential rustc libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libgmp-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev uuid-dev
rbenv init
mkdir ~/.rbenv/gems
export GEM_HOME=/home/ubuntu/.rbenv/gems
export PATH=$GEM_HOME/bin:$PATH
eval "$(rbenv init -)" # Add this to the .bashrc file
nohup rbenv install 3.1.2 > rbenv1.log 2>&1 &
Also see: Install Ruby with rbenv | rvm | asdf