###RVM for Pythonistas, Virtualenv for Rubyists
For Python Developers:
virtualenv
,virtualenvwrapper
orBuildout
Note: envs = environments For Ruby Developers:rvm
####Installation #####Python
Info | Command |
---|---|
Install pip |
$ sudo pip install virtualenvwrapper |
Create dir. to contain virtual envs. | $HOME/.virtualenvs: OR mkdir ~/.virtualenvs |
Edit your $HOME/bash_profile
file & add these lines:
export WORKON_HOME=$HOME/.virtualenvs
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true
Source It: source ~/.bash_profile
#####Ruby First, make sure Git is installed on your system.
bash <<( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
Edit your $HOME/.bash_profile
file & add this line at the very end:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
Source It: source ~/.rvm/scripts/rvm
####Managing Multiple Interpreters
#####Python
You have to install the different Python versions on your system (via package manager).When you will create a virtual environment with virtualenv
, you will pass the interpreter path of the desired Python version as -p
argument of mkvirtualenv
command.
$ mkvirtualenv -p /opt/local/bin/python3 myproject
#####Ruby RVM compiles & installs the desired Ruby interpreter & Ruby version in the current user directory. It supports MRI/YARV, Rubinius, JRuby, Ruby Enterprise Edition, MagLev, IronRuby, MacRuby & GoRuby. This can be achieved in a single one command.
Info | Command |
---|---|
List available interpreters & versions | $ rvm list known |
Install different versions | $ rvm install RUBY_VERSION |
List Installed versions | $ rvm list |
Switch between interpreters | $ rvm use RUBY_VERSION |
Set up the default interpreter | $ rvm --default use X.X.X |
Switch back to default system interpreter | $ rvm use system |
####Managing Multiple Environments
#####Python
Generally, with virtualenv
, you create one environment per project and/or project stage (development, testing, staging, production, etc).
Info | Command |
---|---|
themkvirtualenv command |
$ mkvirtualenv --no-site-packages PROJECT_NAME |
$ mkvirtualenv --no-site-packages superdjango |
The --no-site-packages
option removes the standard site-packages directory from the environment sys.path. Use this option if you want more isolation to avoids dealing with system packages conflicts. As seen above, you can specify the Python version with the -p
option:
$ mkvirtualenv --no-site-packages -p /opt/local/bin/python3 PROJECT_NAME
Add export PIP_RESPECT_VIRTUALENV=true
into $HOME/.bash_profile
file, when your environment is active, pip
auto-installs packages in this environment; auto-switch to this environment instantly.
Info | Command |
---|---|
Install Packages to env | $ pip install PACKAGE OR $ pip install Django |
The -E option | $ pip install -E ENVIRONMENT_NAME PACKAGE |
$ pip install -E superdjango Django |
|
List available envs | Use the workon command. |
Activate env | $ workon ENVIRONMENT_NAME OR $ workon superdjango |
Create virtual env | mkvirtualenv command |
Deactivate current env | Use thedeactivate command |
Delete a virtual env | $ rmvirtualenv ENVIRONMENT_NAME OR $ rmvirtualenv superdjango |
#####Ruby
Where virtualenv
has environments, RVM has gemsets.
Interpreters & packages are all separated & self-contained from system & from each other. Creating a virtual env with virtualenv
is creating a gemset with RVM; you create one gemset per project and/or project stage (development, testing, staging, production, etc).
Info | Command |
---|---|
Install Gemset | $ rvm use RUBY_VERSION@GEMSET_NAME --create |
$ rvm use 1.9.2@myproject --create |
|
Equivalent to | $ rvm use 1.9.2 |
$ rvm gemset create myproject |
|
$ rvm gemset use myproject |
|
Install packages to gemset | $ gem install PACKAGE |
Know path of dir. | $ rvm gemdir command |
Deleting a gemset | $ rvm gemset delete GEMSET_NAME OR $ rvm gemset delete myproject |
Empty/Remove gemset | $ rvm gemset empty GEMSET_NAME OR $ rvm gemset empty myproject |
####Managing project dependencies
#####Python
With pip
& virtualenv
. Just have to create a text file containing package names (& optionally package versions) & give this file to pip
via the -r
option. For “superdjango” project, created a requirements.txt
file to start project with Django 1.2.x & South support.
Inside requirements.txt
:
Django >= 1.2
South == 0.7.2
Created environment:
$ mkvirtualenv --no-site-packages superdjango
Plus install project dependencies:
$ pip install -r /path/to/my/requirements.txt
This will install the latest Django 1.2 version & South 0.7.2 into virtual environment.
#####Ruby
With RVM, RubyGem & Bundler. Where pip
has requirements files, Bundler has Gemfile files. Create a dedicated gemset for Pjt:
$ rvm use 1.9.2@myproject --create
# OR #
$ rvm use 1.9.2
rvm gemset create myproject
rvm gemset use myproject
Install Bundler into gemset:
$ gem install bundler
Created a directory for project:
$ cd /path/to/my/workspace
mkdir myproject
Go to directory & create an empty Gemfile file:
$ cd myproject
bundle init
Edit the Gemfile file & add dependencies:
$ source "http://rubygems.org"
gem "sinatra", "~> 0.9.0"
gem "rack-cache"
bundle install