DEV Community

florianfelsing
florianfelsing

Posted on • Updated on

How To Set up a Ruby on Rails 7 Development Environment on Ubuntu 22.04 LTS

In this post I will go through the steps required to set up a development environment for Ruby on Rails 7 on Ubuntu 22.04 LTS.

I will be running Ubuntu on a virtual machine using VirtualBox, however, the steps are basically the same when Ubuntu is your main OS.

Before You Start

Before you get started, run these commands to update the list of available packages and to upgrade them, if an upgrade is available.

sudo apt update
sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode

Google Chrome

Feel free to go with any browser you are comfortable working with in your development environment. Personally, I like working with Google Chrome.

If you also like using Chrome, just download the .deb package via this page and install.

VSCode

My go-to code-editor is Microsoft Visual Studio Code. The fastest way to install it goes via terminal. Simply execute the following command and you should be good to go:

sudo snap install --classic code
Enter fullscreen mode Exit fullscreen mode

In case you run VSCode on multiple machines and would like to automatically sync your settings between them, don't forget to turn on Settings Sync via the head icon on the bottom left of the screen.

Image description

Git

Any developer needs Git, so let's install it.

The quickest option here is install with default packages, which is fine unless you explicitly need to run the latest version of Git with all the newest available features.

sudo apt install git
Enter fullscreen mode Exit fullscreen mode

By running git --version you can now check, which Git version is installed on your machine.

There are also some standard configurations that I like to apply right away.

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global init.defaultBranch main
git config --global alias.co checkout
git config --global credential.helper "cache --timeout=86400"
Enter fullscreen mode Exit fullscreen mode

This is what the above commands will do (in this exact order):

  • Configure your username
  • Configure your email address
  • Set the name of the default branch to "main" (as opposed to "master")
  • Create "co" as an alias for "checkout" - that way you won't need to write "git checkout main" anymore and go with "git co main" instead
  • Make Git remember credentials for 24 hours

Don't forget to replace the "Your Name" and "your.email@example.com" values, before you execute those commands.

By running git config --list you should now be able to confirm that all of the above settings have been applied successfully.

rbenv and Ruby

For managing my apps' Ruby environments, I like to roll with rbenv (short for RuBy ENVironment).

Install rbenv by executing the following commands:

# Install dependencies required to install Ruby
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

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

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

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

# Apply changes to current Shell sessions
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Now run type rbenv. If the output is as in the screenshot below, you have done everything correctly up to this point.

Image description

Next, you'll want to run the following commands:

# Download Ruby build plugin via GitHub
git clone https://github.com/rbenv/ruby-build.git

# Install Ruby build plugin
cat ruby-build/install.sh
PREFIX=/usr/local sudo ./ruby-build/install.sh
Enter fullscreen mode Exit fullscreen mode

Now everything is set up and ready to install Ruby. The next command will show you the latest stable releases for each Ruby implementation:

rbenv install -l
Enter fullscreen mode Exit fullscreen mode

I'll go with the (at the time of writing) latest Ruby version 3.1.2.

These commands will install it via rbenv and make it the global (= standard) Ruby version to run on your system:

rbenv install 3.1.2
rbenv global 3.1.2
rbenv rehash
Enter fullscreen mode Exit fullscreen mode

To conclude this segment, let's also install some gems that you will definitely need in your Rails development environment:

gem install bundler
gem install rails
rbenv rehash
Enter fullscreen mode Exit fullscreen mode

Feel free to install any other gems at this point, that you know you'll need later on.

SQLite

When you are developing with Rails, there is a high chance that at some point you will run into an SQLite database, especially with small projects that have been mostly set up for testing and experimenting.

Install SQLite3 by running the following command:

sudo apt install sqlite3
Enter fullscreen mode Exit fullscreen mode

Confirm that running sqlite3 –version works and shows you the SQLite version installed on your system now.

PostgreSQL

Run the following command to install PostgreSQL on your machine:

sudo apt-get install postgresql postgresql-contrib
Enter fullscreen mode Exit fullscreen mode

Since we are not intending to connect to PostgreSQL databases from other machines, we can follow the [simple PostgreSQL setup] guide (https://help.ubuntu.com/community/PostgreSQL) as described in the linked article.

"Since the only user who can connect to a fresh install is the postgres user, here is how to create yourself a database account (which is in this case also a database superuser) with the same name as your login name and then create a password for the user:"

sudo -u postgres createuser --superuser $USER
sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

You will also want to create a password for your user:

postgres=# \password <username>
Enter fullscreen mode Exit fullscreen mode

Running the above command will trigger a dialogue that will allow you to set it.

Lastly, also pay attention to this advice given on ubuntu.com:

Client programs, by default, connect to the local host using your Ubuntu login name and expect to find a database with that name too. So to make things REALLY easy, use your new superuser privileges granted above to create a database with the same name as your login name:

So let's do just this:

sudo -u postgres createdb $USER
Enter fullscreen mode Exit fullscreen mode

You should be able to run psql without any error messages now.

nvm and Node.js

Just like with Ruby, we will want to use a version manager with Node to help us manage any conflicts between versions and dependencies.

The version manager will be nvm and you can install it using this command:

# Download and run nvm install script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

Apply these changes to current Shell session using source ~/.bashrc before you continue so that the nvm command becomes available.

Now run the following command to install and use the latest stable version of Node:

nvm install --lts
Enter fullscreen mode Exit fullscreen mode

If everything went well, node -v should now return you the latest stable Node version.

I hope this guide was helpful to you. And please let me know in case you have any questions, suggestions or comments.

Top comments (0)