Adding Bootstrap 5 to Your Rails 6 Application: A Step-by-Step Guide

Ashvin Choudhary
2 min readJul 7, 2023

Bootstrap is a popular front-end framework that helps developers build responsive and visually appealing web applications quickly. In this article, we’ll walk you through the process of integrating Bootstrap 5 into your Rails 6 application. We’ll provide you with the necessary commands and code snippets to make the process as straightforward as possible.

Step 1: Create a New Rails Application
To begin, let’s assume you have Rails 6 installed on your machine. Open your terminal and run the following command to generate a new Rails application:

rails new your_app_name

Replace `your_app_name` with the desired name for your application.

Step 2: Add the Bootstrap Gem
In your terminal, navigate to the root directory of your Rails application by running:

cd your_app_name

Once you’re in the application’s directory, open the `Gemfile` using a text editor and add the following line:

gem ‘bootstrap’, ‘~> 5.0’

Save the file and run the following command to install the Bootstrap gem:

bundle install

Step 3: Import Bootstrap Stylesheets and JavaScript
Now, let’s import the Bootstrap stylesheets and JavaScript files into our Rails application. Open the `app/assets/stylesheets/application.css` file and replace its contents with the following:

/* app/assets/stylesheets/application.css */

@import ‘bootstrap’;

Next, open the `app/assets/javascripts/application.js` file and replace its contents with the following:

/* app/assets/javascripts/application.js */

//= require jquery3
//= require popper
//= require bootstrap

Step 4: Generate a Custom SCSS File
To make customizations to Bootstrap styles, let’s generate a custom SCSS file. In your terminal, run the following command:

rails generate scss:stylesheet custom

This command will generate a new SCSS file named `custom.scss` under the `app/assets/stylesheets` directory.

Step 5: Customize Bootstrap
Open the `app/assets/stylesheets/custom.scss` file in a text editor and add your custom styles. For example, you can override Bootstrap variables or add new CSS rules to modify the appearance of your application.

Step 6: Include Bootstrap in Your Layout
To include Bootstrap styles in your application layout, open the `app/views/layouts/application.html.erb` file and add the following line within the `<head>` section:

<%= stylesheet_link_tag ‘application’, media: ‘all’, ‘data-turbolinks-track’: ‘reload’ %>

Step 7: Restart Your Rails Server
Lastly, restart your Rails server by stopping it (if it’s running) and running the following command:

rails server

That’s it! You have successfully added Bootstrap 5 to your Rails 6 application. You can now start using Bootstrap classes and components in your views to create responsive and visually appealing web pages.

Conclusion:
Integrating Bootstrap 5 into your Rails 6 application is a straightforward process. By following the steps outlined in this article, you can quickly set up Bootstrap, import its stylesheets and JavaScript files, customize its appearance, and include it in your application layout. Bootstrap provides a powerful foundation for building modern and responsive web applications, saving you time and effort in the development process. Happy coding!

--

--

Ashvin Choudhary

Ashvin Choudhary is an AWS Certified Senior Software Engineer with 8+ years of experience. He's enthusiastic about tech and believes in lifelong learning.