By Philip Knerr
As of this writing, the Amazon Linux AMI installs version 2.0.0p648 of the Ruby language by default. This is problematic because recent versions of the Rails framework typically require recent versions of Ruby. Worse, all maintenance has ended for Ruby 2.0.
Therefore, you likely want to install a newer version of Ruby on your EC2 instance. You probably also want to use this newer version by default.
Fortunately, defaulting to a later version of Ruby does not force you to use this version for all Ruby code on your system. If you have Ruby code which relies on a specific version of Ruby, you can still use tools like
rbenv to force that version to be used for that code.
It should be noted that these instructions should still work if, for some reason, there is no version of Ruby installed on an EC2 instance.
The context and caveats in the Foreword apply to this article.
This information is current as of July 9, 2017.
This article assumes you have already instantiated an Amazon EC2 instance on which you would like to upgrade the Ruby language.
Even following the steps in this article perfectly will still not cause the latest stable version of the Ruby language to be installed. This is because there is typically a delay between the release of a new version of Ruby and the release of an Amazon Linux package which installs that version. As of this writing, the latest stable version of Ruby is version 2.4.1, but the latest version available as an Amazon Linux package is version 2.3.0p0. Nonetheless, upgrading to the latest version of Ruby available as an Amazon Linux package currently upgrades Ruby by three minor versions. More important, it upgrades from an unsupported version to a fully supported version. This is a significant improvement. The resulting version is also adequate for many use cases.
Nonetheless, if you need a version of Ruby which is newer than the latest version available as an Amazon Linux package, you will need to install this version in some other way. Other ways include:
- Compiling from source code. This is optimal. However, it requires more effort as well as a minimal understanding of software development.
- Managing Ruby versions using
rbenvor a similar tool. This is relatively easy. However, I’ve had difficulty getting the Passenger application server to use a version of Ruby installed via
Techniques for the above are beyond the scope of this article.
Upgrading the Ruby language
Install the latest patch version of Ruby 2.3 by executing the following command:
sudo yum install -y ruby23 ruby23-devel ruby23-doc
This command will also install upgraded versions of key Ruby tools, including RubyGems and
irb. This works because the specified packages depend on the packages which install these tools.
In the above command, the following packages are optional, but recommended:
ruby23-develpackage installs tools which are useful for Ruby development. If you are only executing Ruby code and not actually developing in Ruby, you may be able to get away with omitting this package. If in doubt, you should include this package.
ruby23-docpackage installs the Ruby 2.3 documentation.
Note that if a newer minor version of Ruby is available via Amazon Linux packages by the time you read this, you may install those packages instead. For example, Ruby 2.4 should be available as a package named
ruby24 at some future date. When that happens, you can replace
ruby24 throughout these instructions. One way to check whether a newer minor version of Ruby is available is to execute the following command:
yum list | grep ruby
Similarly, if you are using an application which requires a specific minor version of Ruby, you can use the same technique to install the packages for that version instead.
Defaulting to the upgraded version
To determine the default version of Ruby, you can execute the following command:
Output similar to the following will be displayed:
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
Let’s fix this by causing the version of Ruby you just installed to be used by default. This can be achieved by redirecting the symbolic links in the
/etc/alternatives directory to the most current versions. To do so, execute the following commands:
sudo rm /etc/alternatives/erb
sudo ln -s /usr/bin/erb2.3 /etc/alternatives/erb
sudo rm /etc/alternatives/gem
sudo ln -s /usr/bin/gem2.3 /etc/alternatives/gem
sudo rm /etc/alternatives/irb
sudo ln -s /usr/bin/irb2.3 /etc/alternatives/irb
sudo rm /etc/alternatives/rake
sudo ln -s /usr/bin/rake2.3 /etc/alternatives/rake
sudo rm /etc/alternatives/rdoc
sudo ln -s /usr/bin/rdoc2.3 /etc/alternatives/rdoc
sudo rm /etc/alternatives/ri
sudo ln -s /usr/bin/ri2.3 /etc/alternatives/ri
sudo rm /etc/alternatives/ruby
sudo ln -s /usr/bin/ruby2.3 /etc/alternatives/ruby
sudo rm /etc/alternatives/testrb
sudo ln -s /usr/bin/testrb2.3 /etc/alternatives/testrb
sudo rm /etc/alternatives/ruby.pc
sudo ln -s /usr/lib64/pkgconfig/ruby-2.3.pc /etc/alternatives/ruby.pc
sudo rm /etc/alternatives/erb.1
sudo ln -s /usr/share/man/man1/erb2.3.1.gz /etc/alternatives/erb.1
sudo rm /etc/alternatives/irb.1
sudo ln -s /usr/share/man/man1/irb2.3.1.gz /etc/alternatives/irb.1
sudo rm /etc/alternatives/rake.1
sudo ln -s /usr/share/man/man1/rake2.3.1.gz /etc/alternatives/rake.1
sudo rm /etc/alternatives/ri.1
sudo ln -s /usr/share/man/man1/ri2.3.1.gz /etc/alternatives/ri.1
sudo rm /etc/alternatives/ruby.1
sudo ln -s /usr/share/man/man1/ruby2.3.1.gz /etc/alternatives/ruby.1
Important: These commands assume that version 2.3 of Ruby was installed (regardless of the patch level). If a different major or minor version was installed, adjust the commands above by replacing all instances of
2.3 above with the actual major and minor version numbers. Note that the
1 in the names of files for man pages is the section of the manual, not a patch level of Ruby.
Some of these symlinks don’t actually point anywhere yet. This is actually OK, because the missing files are for optional Ruby packages. If you upgrade the optional package in question, the symlink will point to it and caused the upgraded version to be used.
To confirm that the upgraded version of Ruby is now the default, execute the following command:
If Ruby was upgraded correctly, output similar to the following will be displayed to indicate a relatively recent version:
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux-gnu]
--version command line switch also works with other key Ruby tools like
Your EC2 instance now has, and defaults to, relatively recent versions of the Ruby language and of key tools such as RubyGems and