Skip to content

Instantly share code, notes, and snippets.

@afeld
Last active November 27, 2023 15:43
Show Gist options
  • Save afeld/5704079 to your computer and use it in GitHub Desktop.
Save afeld/5704079 to your computer and use it in GitHub Desktop.
Using Rails+Bower on Heroku

Updated for Rails 4.0.0+

Bower

  1. Set up the bower gem.

  2. Follow the Bower instructions and list your dependencies in your bower.json, e.g.

    // bower.json
    {
      // ...
      "dependencies": {
        "d3": "~3.1.0",
        "underscore": "~1.4.4",
      }
    }
  3. Include them in your JavaScript:

    // app/assets/javascripts/application.js
    //
    // Bower packages
    //= require d3/d3
    //= require underscore/underscore
    //
    //= require jquery
    //= require jquery_ujs
    // ...
  4. Ignore the components in your repo. If you prefer to "vendor" your dependencies, skip this step.

    # .gitignore
    /bower_components/
    
  5. Install the components.

    npm install -g bower
    bower install

At this point, make sure your app runs and the JS libs installed by Bower are loading on the page.

Heroku

To have Heroku deploys (Cedar stack only) install and compile Bower components on push:

  1. Add the rails_12factor gem.

  2. Use a custom Heroku buildpack that includes Node.js and Bower (see heroku/heroku-buildpack-ruby#67). If you vendored your components (skipping the .gitignore step above), you can skip this step and use the regular Ruby buildpack.

    heroku config:set BUILDPACK_URL='git://github.com/qnyp/heroku-buildpack-ruby-bower.git#run-bower'

Try a deploy: it should successfully compile assets.


Additional resources:

@philwilt
Copy link

thanks for this!
I was able to leave config.assets.compile = false and still have this work.

@caedes
Copy link

caedes commented Oct 30, 2014

Some improvements I made:

  • From Rails 4, assets configuration go in config/initializers/assets.rb
  • Use of rails_12factor gem to replace the production compilation configuration
  • Add a package.json to install Bower as a Node.js dependency
  • Use "postinstall": "bower install" in the scripts part of package.json
  • Use of the heroku-buildpack-multi with Node.js and Ruby support

@zakelfassi
Copy link

Why can't you just use https://rails-assets.org/ ?

@afeld
Copy link
Author

afeld commented Dec 27, 2014

Updated the gist – much simpler now.

Use of rails_12factor gem to replace the production compilation configuration

Good call – updated.

Use of the heroku-buildpack-multi with Node.js and Ruby support

@caedes Mind sharing instructions for this? Though multi-buildpacks are more flexible, it seems like a fair number of extra setup steps.

Why can't you just use https://rails-assets.org/ ?

I didn't know about that site, actually. You could, but I am hesitant to reluctant to add yet another deploy-time external dependency, especially since (from their site) "For new components you'll need bundle install two times because Rubygems index update is asynchronous." My approach here is trying to stay as close to the standard (not-Rails-specific) Bower workflow as possible.

@paulomcnally
Copy link

I use Bowerfile not bower.json

ENOENT No bower.json present

@dovfrank
Copy link

On Heroku,
I did as described and skipped the .gitIgnore part (used rails_12factor gem).
Everything works fine other than the "fonts" folder that is added to the package and now is not recognized.
Any idea?

@vinhnglx
Copy link

vinhnglx commented Mar 4, 2015

I have an error when run bower install: ENOENT No bower.json present.... How to fix it?

@vinhnglx
Copy link

vinhnglx commented Mar 4, 2015

yah. I fixed it. We need to edit .bowerrc file.

{
   "directory": "bower_components"
}

@dukex
Copy link

dukex commented Nov 8, 2015

To change the buildpack run:

$ heroku buildpacks:set 'git://github.com/qnyp/heroku-buildpack-ruby-bower.git#run-bower'

@victor-soto
Copy link

victor-soto commented Nov 27, 2018

Hi I have a problem when deploy to heroku,

remote: rake aborted! remote: Sprockets::FileNotFound: couldn't find file 'jquery' with type 'application/javascript

in the local environment all is done, can someone help me please?

@afeld
Copy link
Author

afeld commented Jan 6, 2019

Bower is now deprecated, and Rails 5.1+ introduced first-class support for Yarn, so I wouldn't use this anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment