Lampros - Weird Bricks

Building an RPM for Ruby 2.4.1

04 September, 2017 | CentOS

These are my notes on how I used FPM to build an RPM for Ruby 2.4.1.

1. Install the dependencies that we need to build Ruby:

yum -y install openssl-devel libffi-devel readline-devel gdbm-devel libyaml-devel \
tcl-devel tk-devel ruby ruby-devel gcc rpm-build gcc-c++ gmp-devel doxygen make \
systemtap-sdt-devel graphviz

2. Install the FPM gem:

gem install fpm --no-ri --no-rdoc

3. Get the Ruby source code:

cd /tmp
tar -xzf ruby-2.4.1.tar.gz
cd ruby-2.4.1

4. Configure and compile Ruby:

./configure --prefix=/usr
time make -j `nproc`

Note: The above took about 6 minutes on a 4 core, 8GB RAM VPS

5. Install Ruby to our target directory '/ruby-compiled-from-source':

make install DESTDIR=/ruby-compiled-from-source
cd /ruby-compiled-from-source/usr/

6. Now add a script to add the gem home on every boot - the script will be called '/usr/local/bin/'. This script will be executed upon installing the RPM.

Note: You don't need to make this script executable. 


if [ -f "$file" ]
echo "$file found"
printf "$file not found - Adding.. "
echo "export GEM_HOME=/usr/local/share/gems" >> /etc/profile.d/
echo 'export PATH=/usr/local/share/gems/bin${PATH:+:${PATH}}' >> /etc/profile.d/
if [ $? == 0 ]; then
printf "OK!\n"
echo "Make sure to run: . /etc/profile.d/"
printf "FAIL!\n"

7. Add another script to remove the gem home when the package is removed - the script will be called '/usr/local/bin/':

Note: You don't need to make this script executable.


if [ -f "$file" ]
echo "$file found - deleting.."
/bin/rm -f $file
if [ $? == 0 ]; then
printf "OK!\n"
printf "FAIL!\n"
echo "$file not found"
unset GEM_HOME

8. Create the RPM:

cd /ruby-compiled-from-source/usr
fpm --verbose -v 2.4.1 -n ruby-lampros -d 'libyaml' \
--after-install=/usr/local/bin/ \
--after-remove=/usr/local/bin/ \
--url="" \
-s dir -t rpm .=/usr

9. Remove the old Ruby and Install the RPM:

yum -y remove ruby-\*
yum -y install ruby-lampros-2.4.1-1.x86_64.rpm

During installation you'll probably notice this message:

/etc/profile.d/ not found - Adding.. OK!
Make sure to run: . /etc/profile.d/

This is the output from the '/usr/local/bin/' script we added earlier. Make sure to actually run that command so that any environmental variables are loaded:

. /etc/profile.d/

The result of the above is that the environmental variable 'GEM_HOME' is now loaded - confirm with:

echo $GEM_HOME 

10. Check that Ruby works:

ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

11. Try installing a gem:

gem install fpm --no-ri --no-rdoc
Fetching: json-1.8.6.gem (100%)
Building native extensions. This could take a while...
Successfully installed json-1.8.6
Successfully installed fpm-1.9.2
2 gems installed

You're good to go!