I am able to install and execute a gem as follows:
rbenv install 2.4.1
rbenv local 2.4.1 # enter the environment
gem install fpm
fpm --version
I was expecting to be able to execute the gem from outside the environment too, something like:
rbenv local --unset # leave the environment
rbenv rehash # update shims
fpm --version
But instead I get:
rbenv: fpm: command not found
The `fpm' command exists in these Ruby versions:
2.4.1
Have I misunderstood how rbenv shims work? Can I execute a gem from outside an rbenv environment?
From the comments in your Github issue it seems you got the basic idea now; rbenv shims are nothing more than glorified shell scripts that tie a command to a particular currently active Ruby version.
But nothing prevents you from creating your own hard-coded shims. For example, let's say you want this global fpm command that is available outside any particular rbenv environment.
Here's a simple way to do it:
> rbenv local 2.4.1
> ln -s `rbenv which fpm` ~/.rbenv/bin/fpm24
> rbenv local --unset
> fpm24 --version
1.10.2
This will install a "shim" into ~/.rbenv/bin/fpm24 that is a hard-coded pointer to the 2.4.1 gem that rbenv has installed previously. Now this fpm24 command will work anywhere, as long as you have ~/.rbenv/bin in your PATH.
Related
I need to run 2.5.3. I use brew to manage my ruby installation (as I couldn't get rvm to work on my machine). When I run
$ruby -v
I get
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
However when I attempt to update it using
brew upgrade ruby
I get
Error: ruby 2.6.1 already installed
Why is it so inconsistent on what ruby version I really have installed?
You should be using a Ruby version manager to manage multiple versions of Ruby. I prefer using rbenv. Following are the steps to install it on a mac (they are explained in detail about what is being done and why; if you want a shortcut, try running all the commands in sequence but I'd still insist you read through the steps).
rbenv
Before proceeding with the actual installation, remember these points:
rbenv in itself does not include the ability to install ruby versions. It only changes ruby version per directory. For installing rubies, you need to install the ruby-build tool (which is part of the rbenv project). It is the same for chruby as well which uses another tool to build ruby. EDIT (June 2021): It looks like rbenv installations now come with ruby-build and are capable of compiling ruby right on your machine. I will leave the answer as it is for so that it makes sense for someone on an older setup (or in case they revert that decision later).
ruby-build has to be installed as a plugin to rbenv.
In case you need to learn about the tools in detail, here are the links for rbenv and ruby-build.
The installation procedure for both tools (and a lot of other help) is available in the README files for the projects. Refer to those if things don't work for you.
Installing rbenv
Run the following command to clone rbenv repo into .rbenv directory in your home directory.
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
Your system still does not know where rbenv is. Add it to your path by running:
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
To initialize rbenv so that it can help you with changing rubies when you change directories, run this:
~/.rbenv/bin/rbenv init
This should tell you something like this:
# Load rbenv automatically by appending
# the following to ~/.bash_profile:
eval "$(rbenv init -)"
So run this:
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
By this point rbenv should be installed. When you run rbenv on the command line, you should get something like this:
$ rbenv
rbenv 1.1.1-39-g59785f6
Usage: rbenv <command> [<args>]
Some useful rbenv commands are:
commands List all available rbenv commands
local Set or show the local application-specific Ruby version
global Set or show the global Ruby version
shell Set or show the shell-specific Ruby version
rehash Rehash rbenv shims (run this after installing executables)
version Show the current Ruby version and its origin
versions List all Ruby versions available to rbenv
which Display the full path to an executable
whence List all Ruby versions that contain the given executable
See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme
NOTE: If you get a warning saying that rbenv is not installed, just run source ~/.bash_profile. That will rerun the ~/.bash_profile script and get rbenv in your path. You should be able to run rbenv after that without trouble.
Do notice that rbenv does not give an option to install or uninstall rubies yet. For this we need to install ruby-build.
Installing ruby-build
We need to add the ruby-build package as a rbenv plugin so that we can type rbenv install <ruby version> to install rubies. All you need to do is to create the plugins directory and checkout the git repo for ruby-build in the plugins directory. Run the following:
$ mkdir -p "$(rbenv root)"/plugins
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
Testing if rbenv and ruby-build have been installed
Run rbenv without any arguments on the terminal should now show the install and uninstall commands being available. Something like this:
$ rbenv
rbenv 1.1.1-39-g59785f6
Usage: rbenv <command> [<args>]
Some useful rbenv commands are:
commands List all available rbenv commands
local Set or show the local application-specific Ruby version
global Set or show the global Ruby version
shell Set or show the shell-specific Ruby version
install Install a Ruby version using ruby-build
uninstall Uninstall a specific Ruby version
rehash Rehash rbenv shims (run this after installing executables)
version Show the current Ruby version and its origin
versions List all Ruby versions available to rbenv
which Display the full path to an executable
whence List all Ruby versions that contain the given executable
See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme
If you see that output, your rbenv is installed properly.
Installing ruby
To install ruby 2.5.3, you could run (wait, don't run it yet):
rbenv install 2.5.3
It should output a few lines, take some time and then tell you that version 2.5.3 was installed. However, there is a problem - if the installation fails, especially during compilation, sometimes, the terminal gets stuck and there is no output on the terminal. It just appears to be installing for a long time (forever). To get more info about what is happening, run the following:
rbenv install -f -v 2.5.3
The -f argument tells rbenv to force-install the given version. So if it is already installed, rbenv will re-install (basically overwrite) the given version. So if an installation failed, -f will make sure of the installation.
The -v argument tells rbenv to output verbose messages. So everything that ruby-build does (including the compilation process) will be shown to you. Don't be afraid by the word compilation here. It normally compiles fine without troubles and does not alter your system ruby (the one installed with sudo apt install ruby on Linux or the one you get by default on macOS) whether it succeeds or fails.
Test installation
Once the installation succeeds, you can run the command below to check which versions are installed (output included in the snippet below):
$ rbenv versions
system
* 2.5.3 (set by /home/ubuntu/.rbenv/version)
Note: On mac, the newly installed ruby will have a different path.
The one with the * in front of it is the one which is active right now. If you run which ruby, you should get a path with a ruby shim. If you are curious, read the rbenv documentation to know what shims are, though you wouldn't have to worry about them.
$ which ruby
/home/ubuntu/.rbenv/shims/ruby
Configure & Forget
rbenv is a cool thing to have but to keep writing rbenv shell 2.5.3 and rbenv shell 2.4.5 every single time is a problem. What you should instead do is set a version of ruby for the directory and forget about rbenv.
You can just create a file named .ruby-version containing one line - the version number of ruby you want to use for all ruby scripts within this directory (and subdirectories). Just cd to the required directory and run:
echo "2.5.3" > .ruby-version
All ruby scripts within that directory and subdirectories will then use version 2.5.3.
I would like to thank Vaibhav for his informative answer. I have not yet tried to install rbenv but I will absolutely try that. For now I was able to work around this issue by not specifying a ruby version in my gem file. This is a short term workaround but it worked!
I've installed Ruby 2.2.2 with rbenv. However when I run
gem env
it says that I am using 2.0.0. I read on a different question to run
sudo gem install -n /usr/local/bin --no-ri --no-rdoc bundler
rbenv rehash
bundle --path=vendor/bundle
However, when running the last line, I receive the error:
Could not locate Gemfile or .bundle/ directory
How do I get the system to use Ruby 2.2.2?
EDIT:
I printed out the contents of my .bash_profile and it had the following two lines:
export PATH
export PATH="$HOME/.rbenv/bin:$PATH"
Is it bad that there is a blank export PATH? If so, how do I remove it?
If you have Ruby 2.2.2 installed already, you can use renv global
rbenv global 2.2.2
Check out the documentation here.
It turns out RVM was still present in the system. All I had to do was run:
rvmsudo rvm implode
I'm on Linux Mint 17.2.
I recently removed ruby with apt-get purge ruby.
I then installed rbenv and then did rbenv install 2.3.0 so now, ~/.rbenv/versions/2.3.0/bin/ruby exists.
But now, I can't do gem install rubocop. I get this:
$ gem install rubocop
rbenv: gem: command not found
The `gem' command exists in these Ruby versions:
2.3.0
But I can do ~/.rbenv/versions/2.3.0/bin/gem install rubocop.
However, once I'm done, I can't use it:
$ rubocop --auto-correct
-bash: /usr/local/bin/rubocop: /usr/bin/ruby1.9.1: bad interpreter: No such file or directory
I also can't find out where this ruby came from:
$ dpkg -S /usr/bin/ruby1.9.1
dpkg-query: no path found matching pattern /usr/bin/ruby1.9.1
It's possible it was installed via RVM a long time ago.
Any idea how I can fix my Ruby?
try run rbenv global 2.3.0 && rbenv rehash
It looks like you haven't run the rbenv shell magic to add bin shims to your path.
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
It's recommended to add that to your .bashrc or .bash_profile so it gets executed on login.
Take a read through this - it covers rbenv and bundler but the initial setup will be what you need to look at.
I'm using ruby, and I was given a zip file with some ruby programs and it says: inside the folder, run bundle install to install the packages required.
When I run the command in my terminal, it says bundle command not found.
Can someone please give me a detailed description of how I can fix this?
gem install bundler
is how to do it.
You may want to use a tool such as rbenv to manage gems.
Just reiterating that for those (at least on OSX) for whom
gem install bundler
Gives a permissions error, an option that seems to have worked for many people is to use rbenv, which kind of adds a shim between your ruby commands (like gem install) and your environment (if my understanding is correct).
Definitely check out this answer.
The process is laid out fairly well under the above link. I chose to install via homebrew:
brew update
brew install rbenv
Then you have to add an argument command to your profile, which if you're using the common ~/.bash_profile, can be done with:
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
Which it looks like is adding a command to initialize rbenv via your shell.
Don't for get to start a new shell, possibly by opening a new terminal or using the source ~/.bash_profile command.
Make sure your $PATH has this .rbenv/shims BEFORE any other directory where your shell might be looking for Ruby (OSX comes with it's own version that we don't want to fiddle with): echo $PATH.
which ruby
/Users/mikekilmer/.rbenv/shims/ruby
#GOOD!
Now install a version of Ruby:
rbenv install 2.2.3
(See all possible versions with rbenv install -l).
Now we can use rbenv global 2.2.3 to switch to a use the newer version of Ruby globally. (Hmm. I thought we didn't want to mess with the system version.) You could also try it with rbenv local 2.2.3 or rbenv shell 2.2.3.
Finally run:
rbenv rehash
Now ruby -v should return 2.2.3 and gem install bundler should work.
Did here.
Just run gem install bundler in your terminal.
There is a link to bundler you can take a look:bundler
Some ruby version managers like chruby and rbenv store gems separately for each version, so when you install a different version of ruby, you'll need to gem install bundler.
Tried every solution here but didn't work out. Eventually I got this to work in two different methods:
Set alias bundle=/path/to/bundle in .bashrc if you don't care the nastiness.
Recreate a fresh dev env via rbenv and do bundle install rails will fix it (fixed my issue).
Terminal -
sudo su
then your password:
change directory :
cd command .
if you do not have permissions to write to drive.
chmod 755 foldername.
And you can also mkdir command in terminal
mkdir /Library/Ruby/Gems/2.3.0.1
copy and paste: gem install bundler paste to the terminal.
Fetching: bundler-1.16.2.gem (100%)
bundler's executable "bundle" conflicts with /usr/local/bin/bundle
Overwrite the executable? [yN] y
bundler's executable "bundler" conflicts with /usr/local/bin/bundler
Overwrite the executable? [yN] y
Successfully installed bundler-1.16.2
Parsing documentation for bundler-1.16.2
Installing ri documentation for bundler-1.16.2
Done installing documentation for bundler after 7 seconds
1 gem installed
works for OS X High Sierra.
~/.rbenv/versions/2.0.0-p247/ exists as does a similar folder for 1.9.3-p429, but nothing for 1.8.7-p374.
What is going on or what am I doing wrong? I have some gems that don't work right with 1.9 and 2.0, so set up 1.8.7-p374 to help debug, but can't install gems.
$ rbenv versions
system
* 1.8.7-p374 (set by RBENV_VERSION environment variable)
1.9.3-p429
2.0.0-p195
$ gem install geonames
ERROR: While executing gem ... (Errno::EACCES)
Permission denied - /Users/user_name/.rbenv/versions/1.8.7-p374/lib/ruby/gems/1.8/gems/geonames-0.3.3/.gitignore
I can sudo and install the gem, but I don't know where, and it's not available.
I'm lost in rbenv. Thanks for help.
You obviously have set RBENV_VERSION in your environment, e.g. in your ~/.bashrc (look for export RBENV_VERSION=1.8.7-p374), but that particular Ruby version is not installed.
To proceed, first remove that export line and secondly install Ruby 1.8.7 like so:
$ rbenv install 1.8.7-p374
Ruby version 1.8.7 generally comes pre-installed with the operating system. That is the reason you do not see it under rbenv's directory