twitter-bootstrap-rails
Integrates Bootstrap 5.3.9 into Rails on Propshaft, Sprockets, importmap or the CDN. Supports Rails 8 and 7.1+. Tested against Rails 8.1.4.1 and 8.0.5.2. Apps still on Bootstrap 4 can use HeroDevs Bootstrap NES 4.6.
Upgrade Rails. Rails 8.1.4.1 and 8.0.5.2 fix 21 security issues, and the Rails team strongly recommends upgrading as soon as possible. This gem requires Rails 7.1 or later but allows unpatched releases within that range, so installing it won't upgrade Rails for you. Run
bundle update railsin your app. Security support for Rails 8.0 ends on 7 November 2026, and older series are already unsupported.
Install
# Gemfile
gem "twitter-bootstrap-rails"bundle install
rails generate bootstrap:install staticGenerators
Every code snippet on the Bootstrap 5.3 Introduction page is reproducible from a command:
| Docs snippet | Command |
|---|---|
| Starter template | rails g bootstrap:starter |
CDN CSS <link>
|
rails g bootstrap:cdn --css |
CDN JS bundle <script>
|
rails g bootstrap:cdn --js |
Popper + bootstrap.js separately |
rails g bootstrap:cdn --separate-popper |
| Bootstrap 6 prerelease tags | rails g bootstrap:cdn --bootstrap=6 |
Important globals (doctype, lang, viewport) |
baked into every generated layout and the starter template |
# Install Bootstrap assets into the asset pipeline
rails g bootstrap:install static
# ...or load Bootstrap from the jsDelivr CDN instead
rails g bootstrap:install cdn
# ...or pin Bootstrap as an ES module in config/importmap.rb
rails g bootstrap:install importmap
# Generate a Bootstrap layout
rails g bootstrap:layout application
# ...linking Bootstrap from the CDN, with Subresource Integrity
rails g bootstrap:layout application --cdn
# ...using Popper and bootstrap.js separately rather than the bundle
rails g bootstrap:layout application --cdn --separate-popper
# Write the starter template from the Bootstrap docs
rails g bootstrap:starter # -> public/bootstrap-starter.html
rails g bootstrap:starter --path=public/demo.html
# Print CDN tags to paste into a layout you already own (writes nothing)
rails g bootstrap:cdn
# Generate themed views for scaffold
rails g scaffold Task title:string done:boolean
rails db:migrate
rails g bootstrap:themed Tasks
# Keep tooltips, popovers and modals working with Turbo (needs Stimulus)
rails g bootstrap:stimulusbootstrap:install cdn records the choice in config/initializers/bootstrap.rb,
so bootstrap:layout emits CDN tags by default afterwards. Pass --cdn or
--no-cdn to override it per run.
Both asset pipelines are supported. On Sprockets, static adds require
directives to your manifests. On Propshaft (the Rails 8 default) there are no
directives to add, so the generated layout links the vendored files directly —
run rails g bootstrap:layout after bootstrap:install static either way.
The Bootstrap version and its CDN URLs and integrity hashes live in one place,
Twitter::Bootstrap::Rails::BOOTSTRAP_CDN. No template hardcodes them.
Importmap
rails g bootstrap:install importmap is for apps on importmap-rails (the Rails 7 and
8 default). It vendors the stylesheet, copies Bootstrap's ES module build and Popper
into vendor/javascript/twitter/bootstrap/, pins both in config/importmap.rb, and
adds import "bootstrap" to app/javascript/application.js. Your own modules can
then import { Tooltip } from "bootstrap". Nothing is fetched from a CDN at runtime.
Popper ships no single-file ES module build, so the gem wraps Popper's official UMD build with ES module exports.
bootstrap:layout now keeps <%= javascript_importmap_tags %> in the layout on any
app with config/importmap.rb. Earlier versions dropped it, so regenerating
application silently turned off Turbo and Stimulus. The doctor warns
(importmap_tags) if no layout loads the importmap.
Turbo and Stimulus
Bootstrap's data attributes (data-bs-toggle="modal", "dropdown", "collapse")
work with Turbo on their own. Tooltips and popovers are opt-in JavaScript, and an
open modal can be cached by Turbo and come back with its backdrop stuck. Run
rails g bootstrap:stimulus and add data-controller="bootstrap" to <body>. The
controller sets up tooltips and popovers on every render, disposes of them
afterwards, and closes modals, offcanvases and dropdowns before Turbo caches a page.
On importmap apps it imports Bootstrap, and elsewhere it uses the bundle's global.
Bootstrap NES 4.6 (HeroDevs)
Apps that can't leave Bootstrap 4 yet can run HeroDevs Bootstrap NES 4.6, a security-patched Bootstrap 4.6 (currently 4.6.5). It is a paid product served from HeroDevs' private npm registry, so the gem can't vendor it. It sets your app up to install it:
rails g bootstrap:nes # package.json + .npmrc + initializer
export HERODEVS_NES_TOKEN=... # your HeroDevs access token
npm install
rails g bootstrap:doctor # checks the NES setup- It needs npm, so use jsbundling-rails or cssbundling-rails. importmap can't install from an authenticated registry.
-
package.jsongets"bootstrap": "npm:@neverendingsupport/bootstrap@4.6.2-bootstrap-4.6.5"plus a matchingoverridesentry, so packages that depend on Bootstrap get NES too. -
.npmrcgets the registry and_authToken=${HERODEVS_NES_TOKEN}, so the token stays in the environment and out of git. Use--token-env=NAMEfor another variable name. - Bootstrap 4's JavaScript needs jQuery.
- The doctor checks the package, the override, the registry and the token line. It
warns if a token is written into
.npmrcor the NES release is older than the one the gem knows, and it doesn't flag Bootstrap 4 markup as legacy. - The gem's layout, themed and snippet generators and its view helpers write
Bootstrap 5 markup, so they warn on an NES app.
bootstrap:agenttells coding agents to write Bootstrap 4 markup and points them at the upstreambootstrap-v4-v6-migrationskill.
Snippets
Every snippet published at
getbootstrap.com/docs/5.3/examples
— 62 of them across 12 categories — is a command. Each writes an .html.erb
partial into app/views/shared/:
rails g bootstrap:header # the first variant, "centered"
rails g bootstrap:header dark_search # a specific variant
rails g bootstrap:header --list # what's available, with descriptions| Command | Variants |
|---|---|
rails g bootstrap:header |
centered, nav_pills, with_auth_buttons, dark_search, light_search, grid_dropdown, double, dark_double
|
rails g bootstrap:hero |
centered, centered_screenshot, with_image, signup_form, cropped_image, dark
|
rails g bootstrap:features |
columns_with_icons, hanging_icons, custom_cards, icon_grid, with_title
|
rails g bootstrap:sidebar |
dark, light, icon_only, collapsible, list_group
|
rails g bootstrap:footer |
simple, with_brand, with_nav, columns, with_newsletter
|
rails g bootstrap:dropdown |
simple, with_search, with_icons, calendar, mega_menu
|
rails g bootstrap:list_group |
with_avatars, checkboxes, checkboxes_expanded, checkable, radios
|
rails g bootstrap:modal |
sheet, confirm, whats_new, signup
|
rails g bootstrap:badge |
pills, subtle, subtle_bordered, with_avatar, with_icons, with_avatar_divider
|
rails g bootstrap:breadcrumb |
basic, with_icons, chevron, custom
|
rails g bootstrap:button |
pills, grid, with_icons, loading, circle
|
rails g bootstrap:jumbotron |
with_icon, placeholder, full_width, basic
|
Variants are listed in the order they appear on the docs page, so the first one in each row is the first example Bootstrap shows.
Options, on every snippet command:
| Option | Effect |
|---|---|
--list |
Print the variants with descriptions and exit, writing nothing |
--json |
Print the variants as JSON and exit, writing nothing |
--as=NAME |
Partial name to write (default: the generator name) |
--path=DIR |
Where to write it (default: app/views/shared) |
--no-icons |
Skip installing the shared icon sprite |
rails g bootstrap:footer columns --as=site_footer --path=app/views/layouts
# -> app/views/layouts/_site_footer.html.erbPlaceholder images
Five snippets (hero centered, hero centered_screenshot, hero with_image,
hero cropped_image, features custom_cards) reference placeholder images that
only exist on Bootstrap's docs site, so they 404 until you point them at your own
assets. The generator warns you when you pick one.
Icons
36 of the 62 snippets use Bootstrap Icons through an SVG sprite. Those commands
also write _bootstrap_icons.html.erb next to the partial. Render it once,
near the top of <body> in your layout, or those icons come out as empty boxes:
<%= render "shared/bootstrap_icons" %>Coding agents
Agents like Claude Code, Cursor and Codex often write outdated Bootstrap from memory or guess at an app's asset setup. These commands let them use what this gem already knows instead. The gem doesn't call any AI service and needs no API key.
rails g bootstrap:agent # write a guide tailored to this app
rails g bootstrap:doctor # check how Bootstrap is set up
rails g bootstrap:doctor --json # the same report, for agents and CI
rails g bootstrap:doctor --v6 # also list what changes in Bootstrap 6
rails g bootstrap:header --json # list a snippet's variants as JSONbootstrap:agent writes .claude/skills/bootstrap-rails/SKILL.md and a section in
AGENTS.md, keeping anything else in that file. Run it again after upgrading the gem
or changing your asset setup. Use --no-claude or --no-agents to skip either one.
bootstrap:doctor writes nothing and exits 1 if a check reports an error, so it also
works as a CI step. It checks:
| Check | Looks for |
|---|---|
bootstrap_css, bootstrap_js
|
Whether your layouts actually load Bootstrap, on Propshaft, Sprockets or the CDN |
bootstrap_version |
CDN tags or vendored files on a different version from the one this gem ships |
icon_sprite |
The sprite being installed but never rendered, or rendered more than once |
placeholder_images |
Docs-site images left in your views |
legacy_markup |
Markup from older Bootstrap versions in app/views, app/helpers and app/components, with file, line and replacement |
In an existing app, legacy_markup works as a migration to-do list. Plain names like
close or well may be your own CSS classes, so check those before changing them.
If Bootstrap comes from package.json or your importmap, the doctor warns that it
can't verify it rather than reporting an error.
Preparing for Bootstrap 6
This gem ships Bootstrap 5.3.9. Bootstrap 6 is in alpha (6.0.0-alpha.1 on npm),
and the Bootstrap team publishes an agent skill for the move from 5 to 6. Install it
into your app alongside the gem's own guide:
npx skills add twbs/bootstrap --skill bootstrap-v5-v6-migration
rails g bootstrap:agent # the guide tells agents to stay on Bootstrap 5 markup until you migrate
rails g bootstrap:cdn --bootstrap=6 # prerelease CDN tags, to try Bootstrap 6 out
rails g bootstrap:doctor --v6 # the migration worklist
rails g bootstrap:doctor --v6 --json--v6 adds a v6_migration check and list: every Bootstrap 5 class, data attribute,
Sass @import and JavaScript call that changes in Bootstrap 6, with file, line and
replacement. For example modal-body becomes dialog-body, col-md-6 becomes
md:col-6, btn-primary becomes btn-solid theme-primary, and
data-bs-toggle="dropdown" becomes data-bs-toggle="menu". It scans app/views,
app/helpers, app/components, app/javascript and your stylesheets, and skips the
Bootstrap files this gem vendored. It reports a warning, never an error, and is off
unless you ask for it.
Things the scan can't do for you:
- The vendored assets,
bootstrap:installand every generator are still Bootstrap 5. Switch the assets to Bootstrap 6 before changing markup, and don't re-run the generators afterwards. - The view helpers (
nav_bar,modal_dialog,badgeand the rest) render Bootstrap 5 markup at runtime, so their output isn't scanned. - Bootstrap 6 changes the spacing scale, so check
p-*/m-*utilities by eye.
Quick Start
rails new myapp
cd myapp
echo 'gem "twitter-bootstrap-rails"' >> Gemfile
bundle install
rails generate bootstrap:install static
rails generate scaffold Task title:string description:text completed:boolean
rails db:migrate
rails generate bootstrap:themed Tasks
rails generate bootstrap:layout application
rails serverRun bootstrap:layout even if you already have a layout — that step is what puts
Bootstrap's CSS and JS on the page.
Upgrading to 5.6.1
5.6.1 ships Bootstrap 5.3.9. After bundle update twitter-bootstrap-rails, re-run
the generator for your setup so the app's own copies move to 5.3.9:
-
rails g bootstrap:install static --forceorrails g bootstrap:install importmap --forcefor vendored assets. -
rails g bootstrap:layout --forcefor CDN layouts. Check the diff first if you've edited the layout.
rails g bootstrap:doctor reports any file still on 5.3.8. Bootstrap 5.3.9 also
keeps data-bs-title and data-bs-content exactly as written. Before, a value like
9.90 was read as a number and shown as 9.9, and an empty title stopped the tooltip
from setting up.
Upgrading to 5.6.0
5.6.0 requires Rails 7.1 or later. Apps on older Rails should pin
gem "twitter-bootstrap-rails", "~> 5.5.1".
If your app uses importmap and you generated its layout with an earlier version,
re-run rails g bootstrap:layout or add <%= javascript_importmap_tags %> to its
<head>: earlier layouts left it out. rails g bootstrap:doctor reports this as
importmap_tags. To load Bootstrap's JavaScript through the importmap instead of the
bundle, run rails g bootstrap:install importmap.
Upgrading to 5.5.0
5.5.0 changes the HTML that the view helpers produce. They were still writing Bootstrap 3 markup, so toggles, dropdowns and close buttons did nothing on Bootstrap 5. If you have custom CSS or tests built on the old output, you'll need to update them:
-
nav_barand the menu helpers usenavbar-expand,nav-itemandnav-link, andmenu_itemputsactiveon the link rather than theli. -
drop_down_submenurenders a labelled group, because Bootstrap 5 has no nested dropdowns. -
badgeandtag_labelrenderbadge text-bg-*. -
render_breadcrumbsrendersnav > ol.breadcrumb, and:divider_classno longer applies.
See the CHANGELOG for the full list. rails g bootstrap:doctor finds
older Bootstrap markup in your own views and helpers.
Upgrading to 5.4.0
5.4.0 is a breaking release:
-
Less is gone.
rails g bootstrap:install lessno longer exists; the argument is nowstatic(default) orcdn. Bootstrap 5 has no Less build, and theless-railsandexecjsdependencies have been dropped. -
Glyphicons are gone, including the
glyphhelper. Bootstrap dropped them in v4. The snippet generators ship Bootstrap Icons instead. -
CoffeeScript support is gone, along with
--no-coffeescript. - The gem version no longer tracks the Bootstrap version.
Twitter::Bootstrap::Rails::BOOTSTRAP_VERSIONsays which Bootstrap ships.
If you are on Rails 8, note that bootstrap:install static previously left
Propshaft apps with no Bootstrap at all — that is fixed here. See the
CHANGELOG for the full list.
Sponsor
- Open Collective — back the project with a one-off or monthly contribution
- Patreon — become a patron
- Bootstrap 5 for Rails — release write-up on Patreon
Buy me a Coffee
☕ Support the Project
Contributors
Thanks to all contributors who are helping to make better.
Star History
License
MIT. Copyright (c) 2025 (since 2011) by Seyhun Akyürek