{"id":11908,"date":"2025-01-29T10:50:52","date_gmt":"2025-01-29T10:50:52","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11908"},"modified":"2025-01-29T10:50:52","modified_gmt":"2025-01-29T10:50:52","slug":"install-bootstrap-in-rails-app","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/install-bootstrap-in-rails-app","title":{"rendered":"What is the Best Way to Install Bootstrap with Rails App?"},"content":{"rendered":"<p>The best way to install and manage Bootstrap in a Rails application is to integrate it using a modern, maintainable, and flexible approach that leverages the Rails asset pipeline and facilitates future updates and customizations. Here are two widely-used methods:<\/p>\n<h3>1. Using the bootstrap Ruby gem:<\/h3>\n<p>This method allows you to easily integrate Bootstrap with the Rails asset pipeline and manage updates through Bundler. It\u2019s simple and Rails-friendly, as it integrates with the asset pipeline and easy to update via bundle update, but it may lag behind in supporting the latest Bootstrap features or updates compared to npm packages.<\/p>\n<p><strong>Steps:<\/strong><\/p>\n<ol>\n<li>Add the gem to your Gemfile:\n<p><span style=\"color: #99cc00;\">gem &#8216;bootstrap&#8217;, &#8216;~&gt; 5.3.0&#8217;<\/span><br \/>\n<span style=\"color: #99cc00;\">gem &#8216;sassc-rails&#8217;, &#8216;~&gt; 2.1&#8217; # For SASS support<\/span><\/li>\n<li>Run bundle install.<\/li>\n<li>Import Bootstrap in your application\u2019s stylesheets:<br \/>\n(app\/assets\/stylesheets\/application.scss):<\/p>\n<p><span style=\"color: #99cc00;\">@import &#8220;bootstrap&#8221;;<\/span><\/p>\n<p>Make sure the file has .scss extension (or .sass for Sass syntax). If you have just generated a new Rails app, it may come with a .css file instead. If this file exists, it will be served instead of Sass, so rename it.<\/li>\n<li>Add Bootstrap and Popper.js in your app\/javascript\/application.js:\n<p><span style=\"color: #99cc00;\">\/\/= require popper<\/span><br \/>\n<span style=\"color: #99cc00;\">\/\/= require bootstrap<\/span><\/p>\n<p>Popper.js is necessary because Bootstrap relies on it to position interactive elements such as dropdowns, tooltips, and popovers. Without it, these components may not function correctly.<\/li>\n<\/ol>\n<h3>2. Using yarn or npm to manage Bootstrap:<\/h3>\n<p>This approach gives you more flexibility and control, especially if you need to customize Bootstrap or keep up with the latest updates. This approach works with Rails 5.1+, Rails 6 and Rails 7. For Rails 7, If you&#8217;re using esbuild or import maps instead of Webpacker, you&#8217;ll need to adjust the way dependencies are managed and imported, as these tools handle JavaScript bundling and asset management differently. It\u2019s easier to stay up-to-date with the latest Bootstrap releases using this approach, and it offers greater control over customization, as well as the ability to eliminate unused parts of Bootstrap through tree-shaking. However, the setup is slightly more complex compared to the gem-based approach.<\/p>\n<p><strong>Steps:<\/strong><\/p>\n<ol>\n<li>Add Bootstrap via Yarn or npm:\n<p><span style=\"color: #99cc00;\">yarn add bootstrap jquery @popperjs\/core<\/span><br \/>\n<span style=\"color: #99cc00;\"># OR<\/span><br \/>\n<span style=\"color: #99cc00;\">npm install bootstrap jquery @popperjs\/core<\/span>&nbsp;<\/li>\n<li>Add following in your environment.js:\n<p><span style=\"color: #99cc00;\">const { environment } = require(&#8216;@rails\/webpacker&#8217;)<\/span><br \/>\n<span style=\"color: #99cc00;\">const webpack = require(&#8220;webpack&#8221;)<\/span><br \/>\n<span style=\"color: #99cc00;\">environment.plugins.append(&#8220;Provide&#8221;, new webpack.ProvidePlugin({<\/span><br \/>\n<span style=\"color: #99cc00;\">$: &#8216;jquery&#8217;,<\/span><br \/>\n<span style=\"color: #99cc00;\">jQuery: &#8216;jquery&#8217;,<\/span><br \/>\n<span style=\"color: #99cc00;\">Popper: [&#8216;popper.js&#8217;, &#8216;default&#8217;]<\/span><br \/>\n<span style=\"color: #99cc00;\">}))<\/span><br \/>\n<span style=\"color: #99cc00;\">module.exports = environment<\/span>&nbsp;<\/li>\n<li>Import Bootstrap styles in app\/assets\/stylesheets\/application.scss:\n<p><span style=\"color: #99cc00;\">@import &#8220;bootstrap\/scss\/bootstrap&#8221;;<\/span>&nbsp;<\/li>\n<li>Import JavaScript in app\/javascript\/packs\/application.js:\n<p><span style=\"color: #99cc00;\">import &#8216;bootstrap&#8217;;<\/span>&nbsp;<\/li>\n<li>Ensure your application.html.erb includes the necessary packs:\n<p><span style=\"color: #99cc00;\">&lt;%= stylesheet_link_tag &#8216;application&#8217;, media: &#8216;all&#8217;, &#8216;data-turbolinks-track&#8217;: &#8216;reload&#8217; %&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;%= javascript_pack_tag &#8216;application&#8217;, &#8216;data-turbolinks-track&#8217;: &#8216;reload&#8217; %&gt;<\/span><\/li>\n<\/ol>\n<p>If you prioritize simplicity and Rails integration, use the gem-based approach. For flexibility, modern JavaScript workflows, and easier upgrades, opt for the npm\/Yarn method. Both approaches can handle customizations efficiently, but npm\/Yarn offers more control and future-proofing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The best way to install and manage Bootstrap in a Rails application is to integrate it using a modern, maintainable, and flexible approach that leverages the Rails asset pipeline and facilitates future updates and customizations. Here are two widely-used methods: 1. Using the bootstrap Ruby gem: This method allows you to easily integrate Bootstrap with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11909,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-11908","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11908"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11908"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11908\/revisions"}],"predecessor-version":[{"id":11910,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11908\/revisions\/11910"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11909"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}