Install Ruby and Rails on Ubuntu 22.04 LTS13 Jan 2023 - Tobias Erdle
Today I wanted to install Ruby 3 and Rails on an Ubuntu 22.04 LTS installation and forgot (again), which libs are required to get this done successfully. Because of that, I'll describe the correct steps in the following sections.
Step 1: Install rbenv and ruby-build
To be able to control my Ruby installation I prefer to use rbenv. With
rbenv you can install different versions
on Ruby and select the version to use globally or on a local base. You can install
rbenv as package or from Git, which is my preferred way since the packages on Ubuntu are outdated most of the time. To install
rbenv via Git and integrate it in your terminal (using ZSH in my case) run
git clone https://github.com/rbenv/rbenv.git ~/.rbenv echo 'eval "$(~/.rbenv/bin/rbenv init - zsh)"' >> ~/.zshrc
in your terminal. To have access to the very helpful
rbenv install command you also need to install the ruby-build plugin. It can be installed via Git by running
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
in the terminal.
Step 2: Install required libs
To be able to use
rbenv install you need these libs for Ubuntu etc. which are suggested
Step 3: Install Ruby
Now you can list the latest Ruby versions by running
rbenv install -l and select one, e.g.
3.0.2. This version can be installed by running
rbenv install 3.0.2.
Step 4: Set global Ruby version
After the successful installation of a Ruby version you can set it as global by running
rbenv global 3.0.2 (or whatever version you've selected).
Step 5: Install Rails
The next step is to install the
rails gem by running
gem install rails in your terminal. This gem is installed for the global Ruby installation.
Step 6: Install PostgreSQL libs
In case you want to use PostgreSQL as database, you need to have at least
libpq-dev installed on your system. Run
sudo apt-get install libpq-dev for
starting the installation. Afterwards the
pg gem can be installed with
gem install pg.