Install Ruby on Rails on Debian 12 Bookworm with Easy Steps

Posted on

Install Ruby on Rails on Debian 12 Bookworm with Easy Steps

This tutorial aims to guide you through the Steps To Install Ruby on Rails on Debian 12 Bookworm. Ruby is an open-source, dynamic programming language known for its elegant syntax and focus on developer productivity. Rails, often referred to as Ruby on Rails, is a powerful web application framework written in Ruby. It provides a structure for building web applications by offering default structures for databases, web services, and web pages. In essence, Rails is a RubyGem, a library package that extends Ruby’s capabilities.

This guide, brought to you by Orcacore, will walk you through the process to Install Ruby on Rails on Debian 12 Bookworm using a command-line utility called rbenv.

Before you begin the process to Install Ruby on Rails on Debian 12 Bookworm, ensure you have the following prerequisites:

  1. Sudo Privileges: You must be logged in to your Debian 12 server as a non-root user with sudo privileges. If you need assistance setting this up, refer to Orcacore’s guide on Initial Server Setup with Debian 12 Bookworm.
  2. Node.js: Node.js must be installed and running on your server. For instructions, consult Orcacore’s guide on Setting up Node.js LTS on Debian 12 Bookworm.

Once these requirements are met, follow the steps outlined below to Install Ruby on Rails on Debian 12 Bookworm.

Ruby on Rails Setup with rbenv tool on Debian 12
Install Ruby on Rails on Debian 12 Bookworm

Step 1 – Download and Install rbenv on Debian 12

rbenv is a command-line tool that simplifies the process to Install Ruby on Rails on Debian 12 Bookworm, and it helps manage and switch between multiple Ruby environments. Here’s how to install it:

First, update your system’s package list:

sudo apt update

Next, install the necessary packages required for building Ruby and its dependencies:

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 -y

Now, use curl to download and execute the rbenv installer script:

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

Add ~/.rbenv/bin to your $PATH environment variable:

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

Configure rbenv to automatically load when you open a new terminal:

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

Apply the changes to your current shell session:

source ~/.bashrc

Verify that rbenv is correctly installed:

type rbenv
**<mark>Output</mark>**
rbenv is a function
rbenv ()
{
    local command;
    command="${1:-}";
    if [ "$#" -gt 0 ]; then
        shift;
    fi;
    case "$command" in
        rehash | shell)
            eval "$(rbenv "sh-$command" "$@")"
        ;;
        *)
            command rbenv "$command" "$@"
        ;;
    esac
}

Step 2 – Install Ruby with rbenv on Debian 12 Bookworm

With rbenv installed, you can now use it to Install Ruby on Rails on Debian 12 Bookworm. First, list the available Ruby versions:

rbenv install -l
**<mark>Output</mark>**
3.0.6
3.1.4
3.2.2
jruby-9.4.3.0
mruby-3.2.0
picoruby-3.0.0
truffleruby-23.0.0
truffleruby+graalvm-23.0.0

Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all / -L' to show all local versions.

Install the desired Ruby version (e.g., 3.2.2):

rbenv install 3.2.2

This process may take a while.

Set the installed Ruby version as the global default:

rbenv global 3.2.2

Confirm the Ruby installation:

ruby -v
**<mark>Output</mark>**
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]

Step 3 – Install Rails on Debian 12 Bookworm

Gems are Ruby’s package manager, used for distributing libraries. You can use the gem command to manage Ruby gems and Install Ruby on Rails on Debian 12 Bookworm. Also, install Bundler, which is a tool for managing gem dependencies for your projects.

Execute the following commands:

# echo "gem: --no-document" > ~/.gemrc
# gem install bundler
**<mark>Output</mark>**
Fetching bundler-2.4.19.gem
Successfully installed bundler-2.4.19
1 gem installed

Verify the gem installation directory:

gem env home
**<mark>Output</mark>**
/root/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0

Install the latest version of Rails:

gem install rails
**<mark>Output</mark>**
...
Successfully installed rails-7.0.8
36 gems installed

Note: To install a specific version of Rails, use the following commands:

# gem search '^rails$' --all
# gem install rails -v your-desired-version

After installing a new Ruby version or a gem that provides commands (like Rails), run:

rbenv rehash

Verify the Rails installation:

rails -v
**<mark>Output</mark>**
Rails 7.0.8

Step 4 – Update rbenv Tool on Debian 12

Update rbenv using git pull. Navigate to the ~/.rbenv directory and run:

# cd ~/.rbenv
# git pull

Step 5 – Uninstall Ruby Versions on Debian 12

To uninstall a specific Ruby version:

rbenv uninstall 3.2.1

Step 6 – Uninstall rbenv from Debian 12

To remove rbenv:

  1. Open ~/.bashrc with your preferred text editor (e.g., vi ~/.bashrc).
  2. Remove the following lines:

    ...
    export PATH="$HOME/.rbenv/bin:$PATH"
    eval "$(rbenv init -)"
  3. Save and close the file.
  4. Remove rbenv and all installed Ruby versions:

     rm -rf `rbenv root`

For further details, refer to the official Rails Guides: https://guides.rubyonrails.org/

Conclusion

You have successfully learned how to use rbenv to manage Ruby environments and Install Ruby on Rails on Debian 12 Bookworm.

We hope you found this guide on Install Ruby on Rails on Debian 12 Bookworm helpful. You may also be interested in these articles:

PHP Composer Installation Guide on Debian 12 Bookworm

Froxlor Server Management Software on Debian 12 Bookworm

Alternative Solutions for Installing Ruby on Rails on Debian 12 Bookworm

While rbenv is a popular and effective method, here are two alternative approaches to installing Ruby on Rails on Debian 12:

1. Using RVM (Ruby Version Manager):

RVM is another popular Ruby version manager similar to rbenv. It offers a comprehensive set of features for managing Ruby installations and gemsets.

  • Explanation: RVM allows you to install multiple Ruby versions and switch between them easily. It also supports gemsets, which are isolated environments for managing dependencies for different projects. This can be useful if you have projects that require different versions of gems.

  • Installation:

    First, install the required dependencies:

    sudo apt update
    sudo apt install curl gpg2 -y

    Then, install RVM:

    curl -sSL https://rvm.io/pkuczynski.asc | gpg --import -
    curl -sSL https://get.rvm.io | bash -s stable

    Follow the instructions provided by the installer. You’ll likely need to open a new terminal window or source your .bashrc or .zshrc file.

    source ~/.rvm/scripts/rvm

    Now, install a Ruby version:

    rvm install 3.2.2 # Or your desired version
    rvm use 3.2.2 --default

    Install Bundler and Rails:

    gem install bundler
    gem install rails

    Like with rbenv, you’ll likely need to rehash after installing gems that provide commands:

    rvm rehash

    Finally, verify the Rails installation:

    rails -v

2. Using asdf Version Manager:

asdf is a versatile version manager that can handle multiple languages and tools, including Ruby. This approach offers a unified way to manage dependencies for various projects.

  • Explanation: asdf provides a single tool for managing versions of Ruby, Node.js, Python, and many other languages and tools. This can simplify your development workflow and reduce the number of tools you need to learn and manage.

  • Installation:

    First, install the necessary dependencies:

    sudo apt update
    sudo apt install git curl autoconf libtool make -y

    Download and install asdf:

    git clone https://github.com/asdf-vm/asdf.git ~/.asdf
    
    echo '. ~/.asdf/asdf.sh' >> ~/.bashrc
    echo 'source ~/.asdf/completions/asdf.bash' >> ~/.bashrc
    source ~/.bashrc

    Add the Ruby plugin:

    asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git

    Install Ruby:

    asdf install ruby 3.2.2 # Or your desired version
    asdf global ruby 3.2.2

    Install Bundler and Rails:

    gem install bundler
    gem install rails

    You may need to reshim asdf after installing gems:

    asdf reshim ruby

    Verify the Rails installation:

    rails -v

These alternative methods, RVM and asdf, offer different approaches to managing Ruby versions and dependencies. Choose the one that best suits your development workflow and project requirements. RVM is solely focused on Ruby management, while asdf can handle multiple languages, providing a unified version management solution.