Published on

How to install ruby on rails on ubuntu 23.04

Authors

Install Ruby on Rails On Ubuntu 23.04

Installation Stack

  • Rbenv
  • Ruby (latest)
  • Ruby on Rails (Latest)
  • Nodejs (Latest Stable)
  • Yarn Package Manager (Latest)
  • Git

In this tutorial we are going to use rbenv to install and ruby and ruby on raills.

Let's Start 🚀

The first step is to install some dependencies for Ruby and Rails. To make sure we have everything necessary to support Rails, we're first going to start by adding the Node.js and Yarn repositories to our system before installing them.

Step -1 (Updat Ubuntu)


At first lets update ubuntu.

sudo apt update

Step - 2 (Install CURL if not installed)


sudo apt install curl

Step - 3 (Install Nodejs if not installed)

sudo apt install nodejs

Step - 4 (Add Yarn Package Manager)

npm install --global yarn

Step - 5 (Install Required Dependencies)

Now install require 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

Step - 6 (Install Rbenv)

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

Step - 7 (Add Path To Bash)

Now we need to add the rbenv path to bash.

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

If you are using zsh then change bashrc to zshrc like as follows

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
source ~/.zshrc

Step - 8 (Install Ruby)

Lets check the version on Ruby.

rbenv install -l

This will give us the available ruby version that we can install in our machine. The output will be something like as follows.

2.7.7
3.0.5
3.1.3
3.2.1
jruby-9.4.1.0
mruby-3.2.0
picoruby-3.0.0
truffleruby-22.3.1
truffleruby+graalvm-22.3.1

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

Now lets pick up the latest version that is 3.2.1 as we can see from the list.

rbenv install 3.2.1

This is going to take some times, so don't worried.

After completion of installation, lets make ruby global.

rbenv global 3.2.1

Now lets check the installed ruby version in our machine. if everything goes well, we will get the version name.

ruby -v

The output will be something like as follows.

ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]

Now run the following command to work rbenv work properly.

rbenv rehash

Step - 9 (Install Bundler)

Ruby on rails use bundler to manage gem dependencies. so we need to install bundler.

gem install bundler

Step - 10 (Install Rails)

If everything goes in a correct way, we can install rails now.

gem install rails -v 7.0.5

or if you want you can istall the latest version with out any specific version.

gem install rails

The output will be as follows

Successfully installed rails-7.0.5
35 gems installed

we can check the installed rails version with the following command.

rails -v

The output will be as follows.

Rails 7.0.5

Now run the following command to work rbenv work properly.

rbenv rehash

Step - 11 (Configuring Git)

Now we are going to configure our github account. Replace my 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 "my name"
git config --global user.email "myname@email.com"
ssh-keygen -t rsa -b 4096 -C "myname@email.com"

The next step is to take the newly generated SSH key and add it to your Github account.

cat ~/.ssh/id_rsa.pub

Now we have to copy and paste the output and paste it to github settings. " The output will look like this.

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCKemFQLarQf/9CH0p/EYKRE+hhGyKkdjkajcuayH989kdgD8QUCkOEle3Re4D7Z4rmNbijk9jOvwGQIqsSuJ0vgN8Vpk3rRoWJcQ9tLSQh4bujBNFlzmJAakVEsen14raEluPCpH07SmhWspQguqaiADTPYxrdnQMEQrgKUWbkOfXUh/q5YHof68VB00SI3dF05deoisndcdi+GGZeywGbVBLMYsgnmSI8CSfYTsx2+9NSz147b8Lfk9h7mlA3hirYdKcvkqlimzNdaasdsadasdasdkjlkhuddjndwVf/XEuoktn9UEOiggVXH4BFcE9LaRwHTqwu1onVQMiigPNTGolwET13e2cH2CyLRkp/41MtuIoyKFAOyDp54PAbeN3W5FX8u0BjhyLgnNUL2IfTl6d58//enAjEnHSJo21kRRlZN7SZkMJbvNzkYciczuGoYubU8lpWf5uFbYKpNfHiGmaMQRmJhkjadlkYkmKMJ/4GTXsbqxF1jyQ6TgNQ2ZIeLzftZPaY1Qw6WZbz2UCvB9xh9jaCePQBu/JvANWGkO/IgGWJOgN/Lmk5J7mzuIghojwRF9JxNIQenQD7neCL+hL0CV0vuD+oBzDPn7b7VgJ2n7CUecfroriBDNR9NQZGyeFRhdqPs5G0YQ== myname@email.com

Now let's check and see if it worked. Type the following command in the terminal.

ssh -T git@github.com

You will get a message like this:

Hi excid3! You've successfully authenticated, but GitHub does not provide shell access.

Step - 12 (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. There are lots of documentation on both, so just pick one that seems like you'll be more comfortable with. If you're new to Ruby on Rails or databases in general, I strongly recommend setting up PostgreSQL. If you're coming from PHP, you may already be familiar with MySQL.

Install and Setup PostgreSQL latest version

Run the following command to install Postgres and dependencies:

sudo apt install postgresql libpq-dev

after completion of postgres installation we have to follow the next step. As 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 myname with your username.

sudo -u postgres createuser myname -s

If you would like to set a password for the user, you can do the following. this will open postgres in terminal.

sudo -u postgres psql

now change the password by replace \password with your own password

postgres=# \password myname

All Setup Done, lets create a new rails application now 😊😊😊.

Step - A (Create New Rails App)

rails new myapp

If you want to use MySQL run this command.

rails new myapp -d mysql

If you want to use Postgres run this command.

Note that this will expect a postgres user with the same username as your app, you may need to edit config/database.yml to match the user you created earlier
rails new myapp -d postgresql

Step - B (Move into the application directory)

cd myapp

If you setup MySQL or Postgres with a username/password, modify the config/database.yml file to contain the username/password that you specified.

Step - C (Create the database)

rails db:create

Step - D (Run the server.)

rails server

Hurray, Successfully Installed Rails and create your first ruby on rails app successfully! 🔥🔥🔥 🐧🐧🐧