Installing Ruby on Ubuntu

Ruby Install guide stage 1:

To install Ruby on Ubuntu and configure it with Apache, you’ll first need to install Ruby and its dependencies, then configure Apache to use Ruby’s mod_ruby or a suitable alternative like mod_passenger. You can achieve this by using the APT package manager or a version manager like Rbenv.

  1. Install Ruby:

Using APT.

Code

sudo apt update

sudo apt install ruby-full

This installs Ruby, RubyGems, and other essential dependencies.

Using Rbenv:
Install dependencies:
Code

    sudo apt update

    sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev

Install Rbenv:

Code

     curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash

Configure Rbenv:

Code

    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc

    echo 'eval "$(rbenv init -)"' >> ~/.bashrc

    source ~/.bashrc

Install a Ruby version:

Code

    rbenv install 3.2.3

    rbenv global 3.2.3



    rbenv install 3.4.2

    rbenv global 3.4.2

(Replace 3.2.3 with the desired Ruby version).

  1. Configure Apache:

Install Apache.

Code

sudo apt install apache2

Install mod_ruby (if using that method).

Code

sudo apt install libapache2-mod-ruby2.7

Configure Apache (example with mod_ruby):

Create a virtual host configuration file (e.g., /etc/apache2/sites-available/my-ruby-site.conf):

Code

     <VirtualHost *:80>

        ServerName your_domain.com

        DocumentRoot /var/www/my-ruby-site



        <Directory /var/www/my-ruby-site>

            Options Indexes FollowSymLinks MultiViews

            AllowOverride None

            Require all granted

            RubyInit /usr/local/bin/ruby # or the path to your ruby

         </Directory>



        RewriteEngine On

        RewriteRule ^/app/(.*) /index.rb?path=$1 [L]

    </VirtualHost>

Enable the virtual host:

Code

    sudo a2ensite my-ruby-site.conf

    sudo apache2ctl restart

STAGE 2:

To configure Passenger with Apache, you need to add specific configuration snippets to your Apache configuration file, typically httpd.conf or apache2.conf. You can achieve this by using the
command, which will guide you through the process and provide the necessary snippets.

Steps:
Install Passenger: Make sure you have Passenger installed.

Run the installer: Execute the passenger-install-apache2-modcd /etc/apache2
command in your terminal.

Follow the prompts: The installer will guide you through the process, including asking you to copy and paste the generated configuration snippets into your Apache configuration file.

Identify the configuration file: Determine the location of your Apache configuration file (e.g., /etc/httpd/httpd.conf or /etc/apache2/apache2.conf).

Add the snippets: Open the configuration file and paste the LoadModule directive and the other Passenger configuration options provided by the installer.

Enable the module: Use the command sudo a2enmod passenger (or similar, depending on your system) to enable the Passenger module, says the Phusion Passenger documentation .

Restart Apache: Restart your Apache server to apply the changes.

Example snippets:

The passenger-install-apache2-module command will generate snippets similar to these:

Code
LoadModule passenger_module /path/to/passenger/lib/httpd/mod_passenger.so PassengerRoot /path/to/passenger PassengerRuby /path/to/ruby/bin/ruby PassengerAppRoot /path/to/your/rails/app # Other Passenger settings

Notes:
Replace /path/to/passenger, /path/to/ruby/bin/ruby , and /path/to/your/rails/app with the actual paths to your Passenger installation, Ruby interpreter, and Rails application, respectively.

The exact snippets and commands may vary slightly depending on your operating system, Apache version, and Passenger version.

LoadModule passenger_module /root/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/passenger-6.0.27/buildout/apache2/mod_passenger.so
PassengerRoot /root/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/passenger-6.0.27 PassengerDefaultRuby /root/.rbenv/versions/3.4.2/bin/ruby

Stage 3:

I have come across a cleaner solution today. This might help future users. The command –

passenger-install-apache2-module
tells me to put these three lines in apache configuration file.

LoadModule passenger_module /root/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/passenger-6.0.27/buildout/apache2/mod_passenger.so
PassengerRoot /root/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/passenger-6.0.27 PassengerDefaultRuby /root/.rbenv/versions/3.4.2/bin/ruby

But, where is that configuration file? The answer is the configuration files are seperated into many pieces and they reside in /etc/apache2/mods-available.

So you should do three things –

Create a file ending with .load in /etc/apache2/mods-available folder. I used passenger.load.

Paste the three lines in that file and save the file.

Now in terminal use sudo a2enmod to enable the module. In my case, the file was, passenger.load. So, I used

sudo a2enmod passenger
Now, restart the server and use the command apache2ctl -M to find that passenger module is enabled.

Stage 4:

to apache2.conf:

AddHandler application/x-ruby .rb .rbx

Options Indexes FollowSymLinks ExecCGI

LoadModule mime_module libexec/apache2/mod_mime.so

systemctl restart apache2

Comments

Leave a Reply