Install Ruby on Rails on Ubuntu

We will be setting up a Ruby on Rails development environment on Ubuntu 22.04 LTS.

Table of contents

Installing Ruby
Configuring Git
Installing Rails
Setting up Database

Installing Ruby

The first step is to install some dependencies …

We will be setting up a Ruby on Rails development environment on Ubuntu 22.04 LTS.

Table of contents

  1. Installing Ruby
  2. Configuring Git
  3. Installing Rails
  4. Setting up Database

Installing Ruby

The first step is to install some dependencies for Ruby and Rails.

We’re going to start by adding the Node.js and Yarn repositories to our system before installing them.

sudo apt install curl
curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn

Next we’re going to be installing Ruby using a version manager called Rbenv.

Installing with rbenv is a simple two step process. First you install rbenv, and then ruby-build:

cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

To install Ruby and set the default version, we’ll run the following commands:

rbenv install 3.2.1 # Change it with the latest version
rbenv global 3.2.1

Confirm the default Ruby version matches the version you just installed.

ruby -v

The last step is to install Bundler

gem install bundler

After bundler is installed, run this command:

rbenv rehash

Want to know more about ruby and rails?

Configuring Git

We’ll be using Git for our version control system so we’re going to set it up to match our Github account. If you don’t already have a Github account, make sure to register. It will come in handy for the future.

Replace name and email address in the following steps, with the ones you used for your Github account.

git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
ssh-keygen -t rsa -b 4096 -C "YOUR@EMAIL.com"

The next step is to take the newly generated SSH key and add it to your Github account. You want to copy and paste the output of the following command and paste it here.

cat ~/.ssh/id_rsa.pub

Once you’ve done this, you can check and see if it worked:

ssh -T git@github.com

You should get a message like this:

Hi AhmadR-Coder-max! You've successfully authenticated, but GitHub does not provide shell access.

Installing Rails

gem install rails -v 7.0.4.2 # Change it with the latest version

Now after installing rails, you need to run the following command to make the rails executable available:

rbenv rehash

Now that you’ve installed Rails, you can run the rails -v command to make sure you have everything installed correctly:

rails -v
# Rails 7.0.4.2

If you get a different result for some reason, quit/close your terminal and restart it. If it still doesn’t work, it means your environment may not be setup properly.

Want to start you rails journey?

Setting Up A Database

Rails ships with sqlite3 as the default database. Chances are you won’t want to use it because it’s stored as a simple file on disk. You’ll probably want something more robust like MySQL or PostgreSQL.

To install MySQL, run the below command:

sudo apt-get install mysql-server mysql-client libmysqlclient-dev

To install PostgreSQL, run the below command:

sudo apt install postgresql-14 libpq-dev

The postgres installation doesn’t setup a user for you, so you’ll need to follow these steps to create a user with permission to create databases. Feel free to replace ahmad with your username.

sudo -u postgres createuser ahmad -s

# If you would like to set a password for the user, you can do the following

sudo -u postgres psql
postgres=# \password ahmad

Congratulations 🎉, your setup is done!

If you get any errors, don’t hesitate to ask in comments.

Thanks for reading!


Print Share Comment Cite Upload Translate
APA
Ahmad Raza | Sciencx (2024-03-29T08:17:37+00:00) » Install Ruby on Rails on Ubuntu. Retrieved from https://www.scien.cx/2023/03/03/install-ruby-on-rails-on-ubuntu/.
MLA
" » Install Ruby on Rails on Ubuntu." Ahmad Raza | Sciencx - Friday March 3, 2023, https://www.scien.cx/2023/03/03/install-ruby-on-rails-on-ubuntu/
HARVARD
Ahmad Raza | Sciencx Friday March 3, 2023 » Install Ruby on Rails on Ubuntu., viewed 2024-03-29T08:17:37+00:00,<https://www.scien.cx/2023/03/03/install-ruby-on-rails-on-ubuntu/>
VANCOUVER
Ahmad Raza | Sciencx - » Install Ruby on Rails on Ubuntu. [Internet]. [Accessed 2024-03-29T08:17:37+00:00]. Available from: https://www.scien.cx/2023/03/03/install-ruby-on-rails-on-ubuntu/
CHICAGO
" » Install Ruby on Rails on Ubuntu." Ahmad Raza | Sciencx - Accessed 2024-03-29T08:17:37+00:00. https://www.scien.cx/2023/03/03/install-ruby-on-rails-on-ubuntu/
IEEE
" » Install Ruby on Rails on Ubuntu." Ahmad Raza | Sciencx [Online]. Available: https://www.scien.cx/2023/03/03/install-ruby-on-rails-on-ubuntu/. [Accessed: 2024-03-29T08:17:37+00:00]
rf:citation
» Install Ruby on Rails on Ubuntu | Ahmad Raza | Sciencx | https://www.scien.cx/2023/03/03/install-ruby-on-rails-on-ubuntu/ | 2024-03-29T08:17:37+00:00
https://github.com/addpipe/simple-recorderjs-demo