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

2. Install the FPM gem:

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

3. Get the Ruby source code:

cd /tmp
wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gz
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/add_gem_home.sh'. This script will be executed upon installing the RPM.

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

#!/bin/bash

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

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

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

#!/bin/bash

file="/etc/profile.d/ruby.sh"
if [ -f "$file" ]
then
echo "$file found - deleting.."
/bin/rm -f $file
if [ $? == 0 ]; then
printf "OK!\n"
else
printf "FAIL!\n"
fi
else
echo "$file not found"
fi
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/add_gem_home.sh \
--after-remove=/usr/local/bin/remove_gem_home.sh \
--url="http://lampros.chaidas.com" \
-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/ruby.sh not found - Adding.. OK!
Make sure to run: . /etc/profile.d/ruby.sh

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

. /etc/profile.d/ruby.sh

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

echo $GEM_HOME 
/usr/local/share/gems

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!

Sources: