Code with me.

{ JamesCode }

How to Set Up Ruby on Rails on Ubuntu 20.04

Cover Image for How to Set Up Ruby on Rails on Ubuntu 20.04
James Hoggard
James Hoggard

In this post, I will discuss how to set up Ruby on Rails on Ubuntu in under an hour.

Not an all-important step because we will run this command later but just to keep it clean early, first type  sudo apt-get update into your terminal. This will download an updated package list from all the source repositories. 

We are now going to install some dependencies for Ruby and Rails in Node.js and Yarn. First type sudo apt install curl into your terminal.

When this finishes we can add the two repositories using:

curl -sL https://deb.nodesource.com/setup_12.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

curl

Then enter: 

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 

Stop

Type in your password if prompted. Now we can set up Ruby 3.0.2 using rbenv in this guide so enter these code blocks into the terminal:

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

rbenv

Second:

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

rbenv

Stop

If you get something similar to "fatal: destination path '/x/y/.rbenv' already exists and is not an empty.." , then navigate up one level up from /.rbenv. To see any available hidden files type: ls -a. Now delete the .rbenv directory. Run the command pictured above again.

Now run these commands: 

rbenv install 3.0.2

rbenv global 3.0.2

ruby -v

Stop

Type gem install bundler. After it completes, type rbenv rehash into the terminal.

You can generate an SSH key for authentication and add it to your GitHub if you don't already have one. Configure Git by replacing the next commands with your GitHub account's listed name and email address:

rbenv

The returned key should begin with all lowercase letters. Copy from the beginning likely starting with "ssh.." to the end of your email. Paste this into the space given for new SSH keys on GitHub. You can choose whichever Rails version you would like to use but if you are still following along we are going to use version 6 with the command:

gem install rails -v 6.1.4.1

After that completes, run rbenv rehash again, then lastly rails -v to have the Rails version returned to you. After this completes, run rbenv rehash again, then lastly rails -v to have the Rails version returned to you. Great job installing Ruby on Rails on Linux, thank you for joining me.

Image by: Ithot

Citations: Go Rails