Explain the differences in setting up a web dev environment on Mac - ruby

I'm new to web development and as I research how to install various dev tools on my Mac the following questions have come up,
Should I install Heroku via the standalone toolbelt or via homebrew?
Isn't the point of homebrew to manage non-Apple packages in a single place.
Ruby development using the bundler gem again seems like a wise decision, so that gems are no longer installed via gem install but rather with a project Gemfile. This would suggest that the only gem install required is Bundler. However, then I see developers install a range of tools like this
gem install bundler foreman pg rails thin --no-rdoc --no-ri
Is this just laziness or is there some reasoning behind this choice that I don't understand?

bundler and foreman I understand being outside of the project. Bundler for obvious reasons, and foreman because sometimes it doesn't like to run in the constraints of the bundle very well, but to my knowledge there is no reason to install pg, rails, and thin outside the bundle.
In certain bundler configurations, bundler will check for locally installed gems and use them as part of the bundle, while in others, it does not. Perhaps the reason for installing that suite of gems is to prevent multiple versions of the gem being installed in different projects on the system, essentially acting as a global repository of gems.
In all honesty, I'm brainstorming on this one, but its the only logical explanation I can come up with, hopefully someone can answer this better than myself.

Related

How to run bundle install as normal user

I am attempting to use bundle install. I am not a Ruby user - this is my first real foray into even running rake. I'm simply trying to user some packages from ThrowtheSwitch.org that use Rake and so on.
I initially installed ruby several days ago using:
sudo apt-get install ruby-full
This allowed me to use rake with Unity testing framework. Now I'd like to use CMock. the instructions for using CMock say to cd into the directory then
$ bundle install
$ bundle exec rake
So I install bundler using:
$ sudo apt-get install bundler
But then running the bundle install continuously asks me for my password. So I try:
$ gem install bundler
Which fails for write permissions on /var/lib/gems/1.9.1. So I try:
$ sudo gem install bundler
which installs OK so I try the bundle install again. But still get continuously asked for my password. So I try:
$ sudo bundle install
And get a warning that I should not run bundler as root:
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine.
How can I install this properly so that I can run it as expected?
Note: I have seen that there are several other questions on this topic, none of which I understood the answers to so let me underline that I am not a ruby (or even web stack) dev - I need this in layman's terms as much as possible.
Note also: I did see several mentions of RVM and rbenv. I'm not sure if they were incidental to those questions in particular or if one or both is required. I do not currently have either. Am about to research exactly what they are now.
Note the third: My platform is WSL (so Ubuntu, kind of).
I've been working with Ruby only a few weeks now. I'll say, I know where you are. I am not about to help much but will say, to your last question in the comments, that my understanding is that the two commands are different.
My understanding of a short answer.. One can have diff versions of Ruby they need to work with (legacy projects, etc) and not every version of Ruby can run every version of a given gem. For this reason, one can use rbenv or rvm to help manage the art of setting up a project with a particular version of ruby and then installing the needed gems.
I've been working with rvm on my mac and rbenv in linux and find them both similar enough for the simple stuff I've been doing. Installing rbenv on linux proved slightly easier. Once set up properly, sudo is no longer needed to install gems - which is preferred. I would recommend trying one of these and installing per their website instructions. Things should go smoother once set up.
Try running:
gem install bundler
bundle install
I will try to explain how the ruby ecosystem works:
Bundler is a gem - a module - that is installed on top of ruby. Gems are installed using
gem install <gem name>
I would recommend you look at installing a ruby version manager before doing anything else. the two main candidates are RVM and rbenv.
I find RVM is the simpler option for the beginner, but it eats up more space on your hard drive.
check out how to install rvm at https://rvm.io/
alternatively look at rbenv at https://github.com/rbenv/rbenv
after you have installed ruby you can test that it is installed correctly by calling ruby -v from the command line.
After that you can install bundler by calling gem install bundler
Once the gem is installed you should be able to run bundler, however if you are using rbenv you might need to run rehash first
I hope that explanation makes sense - please shout if you have questions.
If you - for some reason - stuck with a system installation of ruby,
this does the job:
export GEM_HOME="$(ruby -e 'puts Gem.user_dir')"
export PATH="$GEM_HOME/bin:$PATH"
found it elsewhere: https://guilhermesimoes.github.io/blog/using-bundler-with-system-ruby

Does Bundler have to be installed so that singularity.gs can be installed?

do you have to have Bundler installed to install singularity.gs or can you install like on the video on Viemo?
While you don't need Bundler per se, it is highly recommended that if you are working in any Ruby stack that you use Bundler to ensure that your gem dependencies are locked in place and you don't run into incompatibility issues across gem updates or across team. When singularity 1.2.0 gets a full release, you'll be able to drop the Singularity Sass straight into your project and not need the gem, but if you are compiling with the Ruby compiler it's still recommended to use Bundler.

Installing Sinatra and JSon for ruby

I want to install JSON and Sinatra on my Mac that does not have an internet connection.
How can I download and install Sinatra and JSON with all their dependency packages from another machine and then install on my Mac?
JSON is already installed on Ruby 1.9.2+. If you're not running that already you should be as Mac OS comes with 1.8.7, which is pretty old now, and has been deprecated.
You don't want to try to install a newer version of Ruby on top of Apple's version of Ruby, as they installed it for their own use. Something like rbenv or RVM would be the suggested ways of installing something newer. However, if you're not attached to the internet then you'll have a lot of work ahead of you.
Rubygems can tell us what gems another gem depends on:
gem depend sinatra
Returns:
Gem sinatra-1.4.3
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3.4, ~> 1.3)
Those are the gems you'd have to download and copy over, and install prior to installing Sinatra. Be aware that each of those dependencies probably have their own dependencies also, so you'll need to walk through the list to get everything necessary.
gem fetch sinatra
will retrieve the Sinatra gem to the local directory. Once that's done you can copy it somewhere else convenient. Do the same for the other files you need/want.
Rubygems can install a gem from a local archive. Type gem help install at the command-line for more information, or see "How can I install a local gem?" and "RubyGems Basics", in particular the "Fetching and Unpacking Gems" section for more information.
Honestly though, trying to do any development on a machine that isn't attached to the internet in some form is going to be very, very, painful. I consider an internet connection essential for my development work these days, and when I have to work on a machine that doesn't give me that at work I get pretty grumpy, even when the machine has network connections to other machines that are attached to the internet. That delay and extra step is a real pain.
You could use a networked machine and instruct bundler to install all required gems in a specific location.
$ bundle install --deployment
will install gems at vendor/cache, whereas
$ bundle install --path path/to/directory
will install gems at the given path. Please refer to the bundler documentation.
This will allow you to install everything on a thumb drive (or other portable storage device) and copy the entire directory to your Mac.
If you have more complex requirements, such as controlling ruby versions with rbenv, you can
get your setup right on a networked machine
create a disk image
use vagrant on your mac with the image

How to make gem install command only install when not installed or update needed

i've read the documentation from rubygems site, but i guess the "gem install" command always reinstall, recompile everything, even if the same version already installed..
how to make gem install command only install when needed?
It looks like the --conservative flag will make the gem command do what you want.
gem install rake --conservative
From the documentation gem install --help:
--conservative Don't attempt to upgrade gems already meeting version requirement
You may want to use something external like gembundler to handle project’s gem installation.
If you must use rubygems directly for this, a command like
ruby -e "puts `gem install GEMTOINSTALL` if(`gem list --no-versions | grep GEMTOINSTALL`) == ''"
would do the job.
That is why we use bundle install. This command will look into the gemfile for the gems.
This Gemfile says a few things. First, it says that bundler should look for gems declared in the Gemfile at http://rubygems.org. You can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the sources.
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of the required gems that meet the requirements you specified. Because all of the gems in your Gemfile have dependencies of their own (and some of those have their own dependencies), running bundle install on the Gemfile will install quite a few gems.
If any of the needed gems are already installed, Bundler will use them. After installing any needed gems to your system, bundler writes a snapshot of all of the gems and versions that it installed to Gemfile.lock.
When you run bundle install, bundler will (by default), install your gems to your system repository of gems. This means that they will show up in gem list. Additionally, if you are developing a number of applications, you will not need to download and install gems in common for each application. This is nice for development, but somewhat problematic for deployment.
In a deployment scenario, the Unix user you deploy with may not have access to install gems to a system location. Even if the user does (or you use sudo), the user that boots the application may not have access to them.
As a result, bundler comes with a --deployment flag that encapsulates the best practices for using bundler in a deployment environment.
The --deployment flag requires an up-to-date Gemfile.lock to ensure that the testing you have done (in development and staging) actually reflects the code you put into production. You can run bundle check before deploying your application to make sure that your Gemfile.lock is up-to-date. Note that it will always be up-to-date if you have run bundle install, successfully booted your application (or run your tests) since the last time you changed your Gemfile.

Rails -- gem installation

Quick question about gem installation -- when I use bundle install I know it installs the gems necessary for my individual project, but it doesn't affect other projects on my computer. If I use gem install name_of_gem would that also only affect the current project or would it affect all projects on my computer using rails (a generic installation)? In general I think I am a little confused about how exactly gem installation works, so if your answer could include some general background information to help me understand this that would be great!
Yes, gems are typically installed system-wide, or in your home directory is this is not possible. By default, when your application uses a gem, RubyGems loads the latest installed version. If you want to use a specific version, RubyGems lets you do that with this syntax:
require 'rubygems'
gem 'RedCloth', '3.0'
Bundler is a helpful tool that tracks the versions of a gem that are being used to develop a project, and then allows you to both install them in one fell swoop with bundle install, and also to load those exact versions. The application loads them by loading the Bundler code, which overrides parts of RubyGems to use the versions specified in the Gemfile.
By default, Bundler just calls RubyGems to install gems (again, system-wide or in your homedir). You can ask it to store the gems in a directory called vendor/cache by using bundle package. This lets you "freeze" the gems so that you can distribute them with the source code.
If you want further isolation of your Ruby environments, you should use RVM, which lets you set up isolated gemsets, and in fact, different versions of Ruby, to use on different projects. When you're using RVM, the directory where RubyGems installs things is overridden and is specific to your current Ruby version and gemset.
I'd recommend reading the docs for both RubyGems and Bundler; they're both quite good.
When you do bundle install the gems are installed at rubygems and would be available for all your projects unless you're using RVM and setting up gemsets for your projects.
When you're not using RVM and you do a gem install your operating system is probably going to install the gem at your current user's files (usually ~/.gem), if you sudo install gem it's going to install wherever is the place your system Ruby is installed.
I would really recommend you to setup RVM do manage separate groups of gems and rubies. You can read their website linked above or a simple tutorial I wrote to use it.

Resources