Ruby on Rails, often simply referred to as Rails, is a powerful web application framework written in Ruby. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. If you’re looking to harness the power of Rails on your Ubuntu 24.04 LTS system, you’re in the right place. This guide will walk you through the step-by-step process of installing Ruby on Rails on Ubuntu 24.04 LTS.
Prerequisites
Before you begin, make sure you have:
- A system running Ubuntu 24.04 LTS.
- Sudo privileges.
Step 1: Update Your System
First, ensure your system is up-to-date. Open a terminal and run the following commands:
$ sudo apt update
$ sudo apt upgrade -y
Step 2: Install Ruby
Ruby is the programming language behind Rails. We’ll use rbenv
to manage Ruby versions.
- Install dependencies:
$ sudo apt install -y git curl libssl-dev libreadline-dev zlib1g-dev
- Install rbenv and ruby-build:
$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-installer | bash
- Add rbenv to your path:
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ source ~/.bashrc
- Install Ruby using rbenv:
$ rbenv install 3.2.2 # Check for the latest stable version at https://www.ruby-lang.org/en/downloads/
$ rbenv global 3.2.2
- Verify the Ruby installation:
$ ruby -v
Step 3: Install Node.js and Yarn
Rails requires a JavaScript runtime. We will use Node.js and Yarn:
- Install Node.js:
$ sudo apt install -y nodejs
- Install Yarn:
$ curl -sL 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 update && sudo apt install -y yarn
Step 4: Install Rails
With Ruby installed, you can now install Rails:
$ gem install rails -v 7.0.4 # Check for the latest stable version at https://rubyonrails.org/
$ rbenv rehash
Step 5: Verify the Rails Installation
Verify that Rails is installed correctly by checking the version:
$ rails -v
Step 6: Install and Configure PostgreSQL (Optional)
While Rails can work with SQLite, using PostgreSQL is recommended for production applications:
- Install PostgreSQL:
$ sudo apt install -y postgresql postgresql-contrib libpq-dev
- Start and enable PostgreSQL service:
$ sudo systemctl start postgresql
$ sudo systemctl enable postgresql
- Create a new PostgreSQL user:
$ sudo -i -u postgres
> createuser --interactive
# Follow the prompts to create a new user
> exit
- Create a new PostgreSQL database:
$ sudo -i -u postgres
> createdb your_database_name
> exit
- Configure Rails to use PostgreSQL:
When creating a new Rails application, specify PostgreSQL as the database:
$ coderails new myapp -d postgresql
$ cd myapp
Update the config/database.yml
file with your PostgreSQL database details.
Step 7: Create and Migrate the Database
Run the following commands to set up the database:
$ coderails db:create
$ rails db:migrate
Conclusion
Congratulations! You have successfully installed Ruby on Rails on your Ubuntu 24.04 LTS system. You are now ready to start developing powerful web applications with Rails. For more information on using Rails, check out the official documentation.
Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.