Issue creating new Rails App - ruby-on-rails

When I create a new rails app using rails new Test, I am getting the following error.
run bundle install
Your user account isn't allowed to install to the system Rubygems.
You can cancel this installation and run:
bundle install --path vendor/bundle
to install the gems into ./vendor/bundle/, or you can enter your password
and install the bundled gems to Rubygems using sudo.
I have rbenv -v rbenv 0.4.0 and rails -v Rails 4.2.4
How can I fix this so I can create a new rails app without this issue?

Run
bundle install --path=vendor/bundle
That will install the Gems inside the vendor/bundle directory of your project vs the system gems directory. This is a nice way to keep your gems isolated between apps vs polluting your system gems. Just my 2 cents.

Make sure your user belongs to the RVM group
sudo usermod -a -G rvm myUserName
Run gem env. You should see a list of paths like so under GEM PATHS:
/Library/Ruby/Gems/2.0.0
/Users/sam/.gem/ruby/2.0.0
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0
Open your ~/.bashrc file and add the paths as follows:
export GEM_HOME=/Library/Ruby/Gems/2.0.0
export PATH=$PATH:$GEM_HOME:/Users/sam/.gem/ruby/2.0.0:/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0
Open a new shell and run the gem install rubygems-update.
Everything should run fine now without sudo.

Running:
rails new test
will not work for an application name anyway because it is a reserved rails word. You would want to go with something like testapp

Related

"rails s" doesn't work but "bundle exec rails s" works. Why?

I copied the app from Github. Installed a proper version of Ruby using rbenv. I installed bundler by gem install bundler and ran bundle install --path vendor/bundle. All gems were installed in vendor/bundle directory in the app. Now I wonder, why rails s command doesn't work but bundle exec rails s works? The same with rspec command. Is this because I installed gems in vendor/bundle directory? I'm confused.
Also, when I run gem list I get only a few gems, but there are a lot of them in vendor/bundle directory.
Please tell me why gem list command doesn't see gems from vendor/bundle directory and why I need to run commands with bundle exec. Thank you!
The purpose of having the bundle exec command is to look/search for the command which you want to run inside the current bundle or installed gems inside your vendor directory.
If you are running newer rails (v5) then have a look at the binstubs which copies over the command executeable in the bin directory. So that you can simply call
bin/rails server
This is the same approach which is followed by the deployment solutions which we currently have. They create a .bundle directory which eliminates the need to do bundle install everytime.
Prefixing bundle exec to commands executes the command as it isn't of rails. To fix it we can simply run:
gem install rails
This will install all dependencies of rails and commands like rails s or rails c will work without bundle exec.

Failed generating controller due to Bundler version conflict

I'm learning Rails with tutorials from Ruby on Rails by Michael Hartl: https://www.railstutorial.org/book
I used the following command to generate a controller:
rails generate controller StaticPages home help
Which generates the following error regarding version conflicts:
check_version_conflict': can't activate bundler-1.12.4, already
activated bundler-1.13.0.pre.1 (Gem::LoadError)
I don't know which bundler version to use. The current version of bundler is: 1.13.pre.1
The following command continued failing due to about five gem dependencies that failed to install automatically, which included listen and nokigiri.
bundle install --without production
I tried installing the dependent gems manually, but I'm still having issues.
How do I resolve the check_version_conflict issue with Bundler when generating Rails controllers?
I'll accept an answer that instructs removing current Ruby libs and installing a new development environment from scratch.
Bundler will install project-specific versions of your gems so that you don't have to manage global dependencies.
In effect, if you install Rails with bundler and you also install it with sudo gem install rails or something like that, you'll have two versions on your computer. By default, calling rails will refer to the global version.
If you call bundle exec rails (or bundle exec <gem_name>), it will call the bundler-specific version.
Ten steps to resolve your issues with Bundler
(optional) Uninstall Ruby. There are many ways to do so, here's one: https://superuser.com/questions/194051/how-to-completely-remove-ruby-ruby-gems-on-mac-os-x-10-6-4
(optional) Use rbenv to install Ruby. Follow instructions here: https://github.com/rbenv/rbenv
Make a repo directory that will house your future Rails app
From the command line:
mkdir repo
cd repo
Install Bundler and create a Gemfile for the directory
From the command line:
gem install bundler
bundle init
Open the repo/Gemfile with your editor, and configure it to instruct Bundler which version of Rails to install
In repo/Gemfile:
source "https://rubygems.org"
gem "rails", "4.2.6"
Install Rails via Bundler
From the command line:
bundle install
Create a new Rails app using Bundler, and cd into it
From the command line:
bundle exec rails new whatevs
cd whatevs
Your Rails app will have a Gemfile by default. Open it and add the gems you wish to use in your app.
In repo/whatevs/Gemfile:
gem 'nokogiri', '1.6.8'
From repo/whatevs/ directory, install your app's Gems via Bundler
From the command line:
bundle install
From repo/whatevs/ directory, generate a controller
From the command line:
bundle exec rails generate controller static_pages home help

Can't Install Bundler, no permissions

Im using Ubuntu 15.10. This is the code I write in the terminal
gem install bundler
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /var/lib/gems/2.1.0 directory.
Why is it trying to install it there? of course it doesn't have permissions to install it in the root directory. If I try the same command like this sudo gem install bundler it works. But it is not supposed to be installed as sudo, it gives me problems when creating a new Rails app. What should I do?
You should probably start by running gem env and look at your default environment.
You can change the default installation directory by exporting a different GEM_HOME:
export GEM_HOME=$HOME/.gem/

Globally available Gemfile

I have numerous gems that I use across all Rails projects, but that aren't part of the projects' Gems, for example powder for managing POW.
It would make sense to me to manage these with a global Gemfile, but I can't see any examples of this.
How should I manage global Gems that I don't want in my project gemfiles? It would be good to have a single point of install for when I set up a new machine etc.
I'm using chruby alongside ruby-install to manage my Ruby versions.
Make a Gemfile as usual, and place it in any folder. It does not need to be a project folder. Then you can just do:
bundle install --system
This will install the gems in the Gemfile system-wide, and will ask for the root password if you do not have access to the system folder.
--system:
Installs the gems in the bundle to the system location. This overrides any previous remembered use of --path.
You can also name your Gemfile(s), if you want to organize them in some way:
bundle install --system --gemfile=/path/to/my_special_gemfile
As an addition to #Casper's answer:
I created a directory for my system Gem files.
I then added a dir for each version of ruby I wanted to install system gems for. I added a Gemfile to each dir and a .ruby-version file with the correct version. I can now install system gems for a ruby version using:
$ cd path/to/system_gems_dir/1.9.3-p484
$ bundle install --system
or
$ cd cd path/to/system_gems_dir/2.0.0-p353
$ bundle install --system
You can create GlobalGemfile file (or whatever you name it) and than you can do.
bundle install --gemfile GlobalGemfile
Cool isn't it :)

Why Bundle Install is installing gems in vendor/bundle?

Whenever I do bundle install all of the gems get installed at
app_dir/vendor/bundle
path and consumes loads of disk space. I also tried installing gems where it should get installed i.e gemsets while development by this:
bundle install --no-deployement
but this isn't working for me and installeing gems at vendor/bundle. How can I make it to be installed globally for all applications or in ruby gemsets location ? I also tried removing .bundle/config but nothing changed.
I am using:
rvm version: 1.23.14
ruby version: 2.0.0-p247
rails 3.2.13
Here is my ~/.bash_profile:
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
eval "$(rbenv init -)"
alias pg='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log'
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
My ~/.bashrc:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
Some other information that you might need:
aman#Amandeeps-MacBook-Pro ~/Projects/qe (develop)*$ which bundle
/Users/aman/.rvm/gems/ruby-2.0.0-p247#global/bin/bundle
aman#Amandeeps-MacBook-Pro ~/Projects/qe (develop)*$ rbenv which bundle
/Users/aman/.rbenv/versions/2.0.0-p247/bin/bundle
amandeep#Amandeeps-MacBook-Pro ~/Projects/qe (develop)*$ rbenv which ruby
/Users/aman/.rbenv/versions/2.0.0-p247/bin/ruby
aman#Amandeeps-MacBook-Pro ~/Projects/qe (develop)*$ rbenv gemset active
rbenv: NO such command `gemset'
aman#Amandeeps-MacBook-Pro ~/Projects/qe (develop)*$ which rails
/Users/aman/.rvm/gems/ruby-2.0.0-p247#global/bin/rails
I tried this also but didn't helped:
bundle install --system
and removing .bundle directory.
Please help me in installing gems in gemsets not vendor/bundle or a default place.
In your project folder you will have .bundle directory that holds configuration for bundler. try deleting that folder. it should reset the install path for your gems back to system-wide settings.
In the case you just want to edit the install path, opening .bundle/config with your favorite editor should show you the path to vendor/bundle. Removing that line will restore it to defaults without removing other configs you might have.
Also, another less frequent scenario is your system-wide settings being messed up. According to #NaoiseGolden:
I had to delete .bundle from my Home folder (rm -rf ~/.bundle). You can check out your configuration running bundle env
Try installing using
bundle install --system
I think initially the bundle install was run with --path flag and bundler now rememebers that confguration.
From the bundler man page
Some options are remembered between calls to bundle install, and by the Bundler runtime.
Subsequent calls to bundle install will install gems to the directory originally passed to --path. The Bundler runtime will look for gems in that location. You can revert this option by running bundle install --system.
EDIT: As mentioned in comments below, and also otherwise, this installs the gems system wide. In case you are using rvm etc to manage your environment for different apps, check #IuriG's answer mentioned above.
Use bundle env to view paths and bundle configuration
After this set bundle path to ~/.rvm/gems/ruby-2.0.0-p247 like this:
bundle install --path ~/.rvm/gems/ruby-2.0.0-p247
which is global and also you can use your own custom path.
Post this bundle install will never need path again and will always install all of your gems in that directory(~/.rvm/gems/ruby-2.0.0-p247 in my case) for that app not in app_folder/vendor/bundle
Try running bundle env. This will tell you where the path configuration is set.
First of all, acording to your info, it seems that you have installed both rvm and rbenv. Thats a very bad idea. You have to delete one of them (rbenv + bundler works like a charm for me, didnt try rvm).
In regard to your question check .bundle/config in your project, as all the configuration for bundle to that project lies there (if its still deleted, you can create a new one). You migh want to add this line (or change it, if its already there): BUNDLE_DISABLE_SHARED_GEMS: '0' for sharing gems, they go where your BUNDLE_PATH: is set (BUNDLE_PATH: vendor in my case).
For the global configuration file look in ~/.bundle/config
Also this man page could be of use: bundle config
To Install Gem in system wide avoiding path vendor/bundle, just run the following command in project directory
bundle install --system
$ brew install rbenv
$ rbenv install 2.7.6
$ rbenv install -l
2.7.6
$ rbenv global 2.7.6
$ gem install bundler
$ brew install rbenv-gemset
$ rbenv gemset create 2.7.6 mygemset
$ gem env home
/Users/myuser/.rbenv/versions/2.7.6/gemsets/mygemset
$ gem install rails
$ rails new myapp && cd myapp
$ bundle install
$ bundle show --paths
/Users/myuser/.rbenv/versions/2.7.6/gemsets/myapp/gems/actioncable-7.0.1
...
$ bundle config set --local path 'vendor/bundle'
$ file .bundle
$ .bundle: directory
$ cat .bundle/config
---
BUNDLE_PATH: "vendor/bundle"
$ bundle install # Note from time to time you will get some bizarre error "Could not find timeout-0.2.0 in any of the sources". Simply delete Gemfile.lock and run bundle install again.
$ bundle show --paths
/Users/myuser/myapp/vendor/bundle/ruby/2.7.0/gems/actioncable-7.0.4
$ bundle config set --local system 'true' # or you may want to delete BUNDLE_PATH: "vendor/bundle" from .bundle/config
$ rm -rf vendor/bundle
$ rbenv gemset active
mygemset global
$ bundle install
$ bundle show --paths
/Users/myuser/.rbenv/versions/2.7.6/gemsets/mygemset
All in all, when you install a ruby version manager, such as the preferable rbenv, then it will install gems according to its setup. However, if you update .config/bundle in your project to use 'vendor/bundle', then that's where 'bundle install' will install gems. Sometimes this is used to keep project specific gems, instead of using a gemset tool like rbenv-gemset. Other times it is required for deployment, such as deploying to AWS Lambda on its ruby2.7 runtime. The use of 'vendor/bundle' can be easily removed by removing its reference from .config/bundle as shown above. Once it is removed, it will default to the gem environment that exists globally, which in the above case is managed by rbenv.

Resources