Hi, happy? I hope so :)
So here I'll show you how to setup a Rails environment using RVM, if you are a Windows user, I'm sorry, RVM is not for you but you can do it using a VM.
We are going to install RVM that stands for Ruby Version Manager and create some Gemsets with different Rails, Ruby and Gem versions.
You can also append some options like --rails, --ruby or specify the version like --ruby=1.9.3.
After the installation there will be a folder .rvm on your home directory, that's where all the magic will happen. If something goes wrong in the future you can remove that folder and install again but all your Gemsets, Gems and Rubies will be gone as well, be careful.
When the installtion is over it will automatically update your .bash_profile adding this:
source $HOME/.rvm/scripts/rvm
Which will make your RVM recognizable on your terminal as a function, otherwise it won't work. If for some reason it doesn't happen, you can add it by yourself like:
echo "source $HOME/.rvm/scripts/rvm" >> ~/.bash_profile
When that's done you must reload your dotfile so the new line take effect:
. ~/.bash_profile
Or you can close and reopen your terminal :)
OK, now you have RVM working as a function and the next step is to check your OS requirements for it to work.
rvm requirements
In my case, using Manjaro, it has no requirements but when it does it will start downloading and in some cases it must be run as sudo.
To check what is available for you to use like ruby, jruby, macruby, etc:
rvm list known
All those options are available for you to install like this:
rvm install your_option_goes_here
So let's install Ruby 1.9.3. As you can see it has 1.9.3[-p448]
rvm install 1.9.3-p448
This step will download, extract, configure and compile so it may take some time depending on your CPU and connection and if something goes wrong during one of these steps, be calm and read carefully the messages given, they are really helpful.
When Ruby is installed you can check it:
ruby -v
Which will return, in case you installed 1.9.3-p488, something like this:
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]
You can install any Ruby version you wish following the above step and to use it you do this:
rvm use 1.9.3
Let's suppose you also have Ruby 2.0.0 installed, to use it:
rvm use 2.0.0
You can also reinstall it. That will happen in case your OS doesn't have a required library, e.g.: libyaml and you have already installed Ruby
rvm reinstall RUBY_VERSION
And voilĂ
That's it about installing Ruby.
Now let's work with Gemsets. This is how you'll organize all your Gems so you don't make a mess.
First you need to check all the available Gemsets
rvm gemset list
It will list a (default) and global Gemsets. If you do not specify the Gemset where you want to install a gem, it will be installed in the default Gemset.
The global Gemset has some Gems that are used frequently like bundler, json, rake, rdoc, etc, to have a complete list use gem list.
Now you'll create a Gemset called my_project and we'll install rails 3.2.14
rvm gemset create my_project
You can check it using rvm gemset list
IMPORTANT: For each and every Ruby version there will be a different Gemset list. To test it, change your Ruby version (rvm use 1.9.3 or whatever) and check it's Gemset list.
To use that Gemset
rvm use 1.9.3@my_project
Now install your gems
gem install rails -v 3.2.14
When the process is over, you can check it using
rails -v
And that Rails version will be available only when using that Gemset.
Obviously you can create lots and lots of Gemsets and each with lots and lots of Gems that will not conflict with each other.
Now let's suppose you'll create a project using Rails 4.0.0
You'll create a new Gemset for that project
rvm gemset create my_new_project
rvm use 1.9.3@my_new_project
gem install rails
Done, you have now two Gemsets (my_project and my_new_project) each with a different Rails version and you can install all other Gems required for your project.
If you need to remove a Gemset, you must be using it (rvm RUBY_VERSION@GEMSET_NAME) and do this:
rvm gemset delete
You can rename your Gemset:
rvm gemset rename current_name new_name
To make my life easier I have created some aliases on my .bash_profile so I don't have to type rvm RUBY_VERSION@GEMSET_NAME.
That's it, if you know a way I can improve it, please let me know it.
I wish you the best.
Today, be calm when you would be nervous
very useful, thank you!
ReplyDelete