DEV Community

Cover image for How to Install Ruby on AWS CloudShell
Peter Cooper
Peter Cooper

Posted on

How to Install Ruby on AWS CloudShell

If you use AWS and like to interact with AWS services from the shell/terminal, there's a new option from today: AWS CloudShell. CloudShell provides a terminal emulator in the browser complete with the tools and authentication credentials needed to work with your AWS resources.

Python, Node, and numerous other tools are preinstalled on the Amazon Linux 2 based instance out of the box, but.. there's no Ruby! Boo!

While installing Ruby on Amazon Linux 2 is no big deal, only the /home folder is persisted between AWS CloudShell runs, so you'll need to take extra care to install it under your home directory.

Without further ado, here's how to get Ruby 2.7.2 running on AWS CloudShell using rbenv and ruby-build:

# Install basic developer tools and dependencies
sudo yum -y groupinstall "Development Tools"
sudo yum -y install git-core zlib zlib-devel gcc-c++ patch readline readline-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison curl sqlite-devel

# Install libyaml to $HOME
wget https://github.com/yaml/libyaml/releases/download/0.2.5/yaml-0.2.5.tar.gz
tar xzf yaml-0.2.5.tar.gz 
cd yaml-0.2.5
./configure --prefix=$HOME
make && make install
cd ..
rm -rf yaml-0.2.5
rm yaml-0.2.5.tar.gz

# Install rbenv and ruby-build
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
~/.rbenv/bin/rbenv init
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
mkdir -p "$(rbenv root)"/plugins
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build

# Install Ruby itself
RUBY_CONFIGURE_OPTS=--with-libyaml-dir=$HOME rbenv install 2.7.2
rbenv global 2.7.2
gem env home
Enter fullscreen mode Exit fullscreen mode

There's a LOT to digest here, but here's basically what happens:

  • Install basic development dependencies (gcc, etc.)
  • Install libyaml manually to get it into /home
  • Install rbenv and ruby-build
  • Get ruby-build to build Ruby 2.7.2
  • Set Ruby 2.7.2 as our default, global Ruby
  • Run gem env home just to see everything is working

Note that due to only the /home directory sticking around long term, you might find that some gems with native dependencies fail to install. If the dependencies are binary packages, you'll need to install them prior to installing or upgrading those gems.

To install GCC and a big pile of common native dependencies, you can do this:

sudo yum groupinstall "Development Tools"
sudo yum install git-core zlib zlib-devel gcc-c++ patch readline readline-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison curl sqlite-devel libyaml
Enter fullscreen mode Exit fullscreen mode

Notes on an earlier libyaml problem..

In an earlier version of this article, I used the normal libyaml packages and Ruby installed fine.. but once the instance had been deleted due to inactivity, gem list failed to work due to libyaml being dynamically loaded from the system.

The error looked like this:

in 'require': libyaml-0.so.2: cannot open shared object file: No such file or directory - /home/cloudshell-user/.rbenv/versions/2.7.2/lib/ruby/2.7.0/x86_64-linux/psych.so (LoadError)
Enter fullscreen mode Exit fullscreen mode

If you really want to use the system libyaml, this can be resolved by reinstalling libyaml each time using sudo yum -y install libyaml and this could be added to ~/.bash_profile but if you install libyaml to $HOME as in the new instructions up top, this problem goes away.

Top comments (0)