Install Ruby On Rails on
Ubuntu 24.04 Noble Numbat
A guide to setting up a Ruby on Rails development environment
Operating System
Version
Overview
This will take about 30 minutes.
We will be setting up a Ruby on Rails development environment on Ubuntu 24.04 Noble Numbat.
The reason we're going to be using Ubuntu is because the majority of code you write will run on a Linux server. Ubuntu is one of the easiest Linux distributions to use with lots of documentation so it's a great one to start with.
You'll want to download the latest Desktop version here: http://releases.ubuntu.com/24.04/
Some of you may choose to develop on Ubuntu Server so that your development environment matches your production server. You can find it on the same download link above.
Installing Ruby
The first step is to install dependencies for compiling Ruby.
Open your Terminal and run the following commands to install them.
sudo apt-get update
sudo apt install build-essential rustc libssl-dev libyaml-dev zlib1g-dev libgmp-dev
Next, we'll install Ruby using a version manager called Mise. This allows you to easily update Ruby and switch between versions anytime.
curl https://mise.run | sh
echo 'eval "$(~/.local/bin/mise activate)"' >> ~/.bashrc
source ~/.bashrc
Then install Ruby with Mise:
mise use --global ruby@3
Confirm that Ruby is installed and works:
ruby --version
#=> 3.3.5
You also want to ensure you're using the latest version of Rubygems.
gem update --system
Optionally, if you plan to use Node.js for handling assets, you can use Mise to install Node as well.
mise use --global node@22.11.0
node -v
#=> 22.11.0
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 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 "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
ssh-keygen -t ed25519 -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_ed25519.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 excid3! You've successfully authenticated, but GitHub does not provide shell access.
Installing Rails
Choose the version of Rails you want to install:
gem install rails -v 8.0.0
Now that you've installed Rails, you can run the rails -v
command to make sure you have everything installed correctly:
rails -v
# Rails 8.0.0
If you get a different result for some reason, it means your environment may not be setup properly.
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 is a lot of documentation on both, so you can 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.
Setting Up MySQL
You can install MySQL server and client from the packages in the Ubuntu repository. As part of the installation process, you'll set the password for the root user. This information will go into your Rails app's database.yml
file in the future.
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
The libmysqlclient-dev
package gives you the necessary files to compile the mysql2
gem which is what Rails will use to connect to MySQL when you setup your Rails app.
When you're finished, you can skip to the Final Steps.
Setting Up PostgreSQL
For PostgreSQL, we're going to add a new repository to easily install a recent version of Postgres.
sudo apt install postgresql 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 chris
with your username.
sudo -u postgres createuser chris -s
# If you would like to set a password for the user, you can do the following
sudo -u postgres psql
postgres=# \password chris
Final Steps
And now for the moment of truth. Let's create your first Rails application:
#### If you want to use SQLite (not recommended)
rails new myapp
#### If you want to use MySQL
rails new myapp -d mysql
#### If you want to use Postgres
# 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
# 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
# Create the database
rake db:create
rails server
You can now visit http://localhost:3000 to view your new website!
Now that you've got your machine setup, it's time to start building some Rails applications.
Getting an "Access denied" error?
If you received an error that said Access denied for user 'root'@'localhost' (using password: NO)
then you need to update your config/database.yml file to match the database username and password.
Editing Code
Install VS Code.
Then run Ctrl+Shift+P and select "Install 'code' command in PATH". This will add the code
command to your shell so you can open up VS Code from your terminal.
Lastly, run this command to configure VS Code as your editor. This allows you to edit Rails credentials in your terminal.
echo 'export EDITOR="code --wait"' >> ~/.bashrc
exec $SHELL
That's it! Let us know in the comments below if you run into any issues or have any other protips to share!