bundle install - command not found - running as root - ruby

I am using Amazon EC2 instance and when I run bundle install as ec2-user, it works fine but when I try the same as root user, it gives error.
bash: bundle: command not found
There is bundler gem exist when I run gem list for both users. I have installed ruby-devel rubygems ruby-libs as root user. Can anyone please help me understand this issue? I have searched and read for hours.
echo $PATH of both users are as below.
root:
/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin
ec2-user:
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin

I added /usr/local/bin and /usr/local/sbin to secure_path in /etc/sudoers
sudo visudo
Then change this line:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Changed this to
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
Explanation:
Gems were installed at /usr/local path and without adding this in secure_path, root user cannot run scripts from there. You can check your gems path by running gem env in shell.
I also had another issue. I installed gems as ec2-user using bundle and default rubygems path in Amazon Linux installed them only for ec2-user, something I did not like so I installed ruby and rubygems from source which has path to installed them at central location under /usr/local. I know it is not recommended by many but I like it this way. By the way, I tried to find a way to change rubygems path but I could not.

Try sudo -i -u $USER bundle install
where $USER is the correct username so : sudo -i -u root bundle install
If this does not work, consider giving a try to the rbenv-sudo plugin.

Related

Getting a "don't have write permissions" when installing cocoapod

I'm trying to put a google sign in for my ios app and to do that I need CocoaPods. But when installing it like there own website on terminal I get this:
$ sudo gem install cocoapods
[Password: (*Put in my admin password for my macbook*)
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /usr/bin directory.
This is being done on a Macbook Air running High Sierra 10.13.2
(Don't know if you need that info but I've never done anything with cocoapods or ruby before)
I also tried following this link and followed the awnser and installed RVM, or at least I think I did. I just put this in:
ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install)"
Well I tried installing CocoaPods after that but still got the same thing.
You might try the following command
sudo gem install cocoapods -n /usr/local/bin
The problem is that gem install was trying to use /usr/bin for installing the binaries, which shouldn't be touched for the System Integrity Protection. Since:
A centerpiece is the protection of system-owned files and directories
against modifications by processes without a specific "entitlement",
even when executed by the root user or a user with root privileges
(sudo)
Sudo is without any power in this situation.
Hence the solution uses -n, from gem help install it says:
-n, --bindir DIR Directory where binary files are located
in this way you will be able to move the binaries in a more comfortable destination: /usr/local/bin which stands for programs that a normal user may run.

Bundler 1.12.0 cannot be run as a user with no $HOME directory

With the latest version of bundler, I keep getting errors such as:
STDOUT: There was an error while trying to write to /home/user1/.bundle/cache/compact_index/rubygems.org.443.29b0360b937aa4d161703e6160654e47.
It is likely that you need to grant write permissions for that path.
I'm running sudo su - user2 bundle install synapse, where user2 has no HOME directory. I'm trying to install Airbnb Smartstack using Chef.
It seems that the latest version of Bundler insists on writing the new index cache files to $HOME/.bundle. This means that commands such as:
sudo su - homelessuser bundle install synapse
...fail when they try to write to the $HOME/.bundle of the user that called sudo ....
As a workaround, I'm pinning my bundler version to 1.11.2 for now.
Note The GitHub issue for this can be found here

Why can't I install Sass on Mac OS? [duplicate]

I am not able to install and run fakes3 gem on El Capitan Beta 5.
I tried:
sudo gem install fakes3
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/fakes3
Then I tried doing it the cocoapods way. It worked for cocoapods but not for fakes3.
mkdir -p $HOME/Software/ruby
export GEM_HOME=$HOME/Software/ruby
gem install cocoapods
[...]
1 gem installed
gem install fakes3
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
Disclaimer: #theTinMan and other Ruby developers often point out not to use sudo when installing gems and point to things like RVM. That's absolutely true when doing Ruby development. Go ahead and use that.
However, many of us just want some binary that happens to be distributed as a gem (e.g. fakes3, cocoapods, xcpretty …). I definitely don't want to bother with managing a separate ruby. Here are your quicker options:
Option 1: Keep using sudo
Using sudo is probably fine if you want these tools to be installed globally.
The problem is that these binaries are installed into /usr/bin, which is off-limits since El Capitan. However, you can install them into /usr/local/bin instead. That's where Homebrew install its stuff, so it probably exists already.
sudo gem install fakes3 -n/usr/local/bin
Gems will be installed into /usr/local/bin and every user on your system can use them if it's in their PATH.
Option 2: Install in your home directory (without sudo)
The following will install gems in ~/.gem and put binaries in ~/bin (which you should then add to your PATH).
gem install fakes3 --user-install -n~/bin
Make it the default
Either way, you can add these parameters to your ~/.gemrc so you don't have to remember them:
gem: -n/usr/local/bin
i.e. echo "gem: -n/usr/local/bin" >> ~/.gemrc
or
gem: --user-install -n~/bin
i.e. echo "gem: --user-install -n~/bin" >> ~/.gemrc
(Tip: You can also throw in --no-document to skip generating Ruby developer documentation.)
In my case, I had to re-install Ruby using Brew. That seems to have solved the problem as I can install gems again.
brew install ruby
After this, you need to log out and log back in, either graphically or just restarting your terminal.
That is because of the new security function of OS X "El Capitan".
Try adding --user-install instead of using sudo:
$ gem install *** --user-install
For example, if you want to install fake3 just use:
$ gem install fake3 --user-install
sudo gem install -n /usr/local/bin cocoapods
Try this. It will definately work.
You have to update Xcode to the newest one (v7.0.1) and everything will work as normal.
If after you install the newest Xcode and still doesn't work try to install gem in this way:
sudo gem install -n /usr/local/bin GEM_NAME_HERE
For example:
sudo gem install -n /usr/local/bin fakes3
sudo gem install -n /usr/local/bin compass
sudo gem install -n /usr/local/bin susy
Looks like when upgrading to OS X El Capitain, the /usr/local directory is modified in multiple ways :
user permissions are reset (this is also a problem for people using Homebrew)
binaries and symlinks might have been deleted or altered
[Edit] There's also a preliminary thing to do : upgrade Xcode...
Solution for #1 :
$ sudo chown -R $(whoami):admin /usr/local
This will fix permissions on the /usr/local directory which will then help both gem install and brew install|link|... commands working properly.
Solution to #2 :
Ruby based issues
Make sure you have fixed the permissions of the /usr/local directory (see #1 above)
First try to reinstall your gem using :
sudo gem install <gemname>
Note that it will install the latest version of the specified gem.
If you don't want to face backward-compatibility issues, I suggest that you first determine which version of which gem you want to get and then reinstall it with the -v version. See an exemple below to make sure that the system won't get a new version of capistrano.
$ gem list | grep capistrano
capistrano (3.4.0, 3.2.1, 2.14.2)
$ sudo gem install capistrano -v 3.4.0
Brew based issues
Update brew and upgrade your formulas
$ brew update
$ brew upgrade
You might also need to re-link some of them manually
$ brew link <formula>
As it have been said, the issue comes from a security function of Mac OSX since "El Capitan".
Using the default system Ruby, the install process happens in the /Library/Ruby/Gems/2.0.0 directory which is not available to the user and gives the error.
You can have a look to your Ruby environments parameters with the command
$ gem env
There is an INSTALLATION DIRECTORY and a USER INSTALLATION DIRECTORY. To use the user installation directory instead of the default installation directory, you can use --user-install parameter instead as using sudo which is never a recommanded way of doing.
$ gem install myGemName --user-install
There should not be any rights issue anymore in the process. The gems are then installed in the user directory : ~/.gem/Ruby/2.0.0/bin
But to make the installed gems available, this directory should be available in your path. According to the Ruby’s faq, you can add the following line to your ~/.bash_profile or ~/.bashrc
if which ruby >/dev/null && which gem >/dev/null; then
PATH="$(ruby -rubygems -e 'puts Gem.user_dir')/bin:$PATH"
fi
Then close and reload your terminal or reload your .bash_profile or .bashrc (. ~/.bash_profile)
This is the solution that I have used:
Note: this fix is for compass as I wrote it on another SO question, but I have used the same process to restore functionality to all terminal processes, obviously the gems you are installing are different, but the process is the same.
I had the same issue. It is due to Apple implementing System Integrity Protection (SIP). You have to first disable that...
Reboot in recovery mode:
Reboot and hold Command + R until you see the apple logo.
Once booted select Utilities > Terminal from top bar.
type: csrutil disable
then type: reboot
Once rebooted
Open terminal back up and enter the commands:
sudo gem uninstall bundler
sudo gem install bundler
sudo gem install compass
sudo gem install sass
sudo gem update --system
The the individual gems that failed need to be fixed, so for each do the following:
On my machine this was the first dependency not working so I listed it:
sudo gem pristine ffi --version 1.9.3
Proceed through the list of gems that need to be repaired. In all you are looking at about 10 minutes to fix it, but you will have terminal commands for compass working.
Screenshot
If the gem you are trying to install requires xml libraries, then try this:
sudo gem install -n /usr/local/bin <gem_name> -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib/
Specifically, I ran into a problem while installing the nokogiri gem v 1.6.8 on OS X El Capitan
and this finally worked for me:
sudo gem install -n /usr/local/bin nokogiri -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib/
To make sure you have libxml2 and libxslt installed, you can do:
brew install libxml2 libxslt
brew install libiconv
and then check to make sure you have xcode command line tools installed:
xcode-select --install
should return this error:
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
Reinstalling RVM worked for me, but I had to reinstall all of my gems afterward:
rvm implode
\curl -sSL https://get.rvm.io | bash -s stable --ruby
rvm reload
I ran across the same issue after installing El Capitan, I tried to install sass and compass into a symfony project, the following command returned the following error:
$ sudo gem install compass
ERROR: Error installing compass:
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
checking for ffi.h... /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:434:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
So I then tried to install sass with:
$ sudo gem install sass
Got the same error message, after some googling I managed to install sass using the following command:
$ sudo gem install -n /usr/local/bin sass
The above worked for me with installing sass but did not work for installing compass. I read that someone somewhere had opened an instance of xcode then closed it again, then successfully ran the same command after which worked for them.
I attempted to open xcode but was prompted with a message saying that the version of xcode installed was not compatible with El Capitan.
So I then updated xcode from the app store, re-ran the following command which this time ran successfully:
$ sudo gem install -n /usr/local/bin compass
I was then able to run $ compass init
I now have all my gems working and can proceed to build some lovely sass stuff :)
I had to rm -rf ./vendor then run bundle install again.
You might have two options:
If you've installed ruby and rails, you can first try running the command:
rvm fix-permissions
You can uninstall ruby completely, and reinstall in your ~ directory aka your home directory.
If you're using homebrew the command is:
brew uninstall ruby
For rails uninstall without homebrew the command is:
rvm remove
This should reinstall the latest ruby by running command:
curl -L https://get.rvm.io | bash -s stable --rails<br>
Mac has 2.6.3 factory installed, and it's required... if not run this command:
rvm install "ruby-2.6.3"
and then:
gem install rails
You'll get a few error messages at the end saying you have to add some other bundles...
Just make sure you're in the home ~ directory when you're installing so the permissions won't be an issue, but just in case...
I again ran:
rvm fix-permissions
and:
rvm debug
which told me I had to download yarn, I didn't save the output for it. Basically I did whatever the prompt told me to do if it had to do with my OS.
-D
I don't like to install stuff with sudo.
once you start with sudo you can't stop..
try giving permissions to the Gems directory.
sudo chown -R $(whoami) /Library/Ruby/Gems/2.0.0
sudo chown -R $(whoami):admin /usr/local
That will give permissions back (Homebrew installs ruby there)

rbenv and passenger - cannot install from not root user account(sudo group)

I'm just trying to setup my first rails server - on Ubuntu 12.04. I installed rbenv (as user: deployer with groups sudo and admin). Now trying to install passenger according to their docs.
I run passenger-install-nginx-module it starts but then I get
Please specify a prefix directory [/opt/nginx]:
--------------------------------------------
Permission problems
This installer must be able to write to the following directory:
/opt/nginx
But it can't do that, because you're running the installer as deployer.
Ok, I run sudo passenger-install-nginx-module then, and get sudo: passenger-install-nginx-module: command not found.
Does it mean I have to install it by loggin as root, installing whole rbenv for root user and then install passenger? Every tutorial I read they run it from not root user account :/, it doesn't work for me.
As I wrote in my comment, you have to run the passenger-install-nginx-module as root with the full path, because it is not in the $PATH var of your bash. To find out the path, run which passenger-install-nginx-module

Heroku command not found

After installing Heroku Toolbelt, in terminal on Mac when trying to run the following command:
heroku
I get the error:
bash: heroku: command not found
When I do:
gem environment
I get:
- RUBYGEMS VERSION: 1.3.6
- RUBY VERSION: 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin11.0]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-11
- GEM PATHS:
- /Library/Ruby/Gems/1.8
- /Users/Bart/.gem/ruby/1.8
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
I've tried adding several paths to $PATH, but nothing works...
Manually adding the symlink after installing Toolbelt fixed it for me.
sudo ln -s /usr/local/heroku/bin/heroku /usr/bin/heroku
(This answer is for typical other persons, that may land here, and that may find it useful)
If you come to install heroku snap using snap command through the command line as follow
sudo snap install heroku --classic (the thing you will find in the heroku doc).
And that after installation the heroku command isn't available. Then here the solution and the why:
First know that when you install a new snap, it get added to /snap folder. A new folder with the snap name is created (/snap/heroku), and the executable file for the command is added to /snap/bin (/snap/bin/heroku).
Try
/snap/bin/heroku help
and you will find it work very well.
Solution: So you have just to add /snap/bin to your PATH environement variable.
Heroku is supposing that it's already done. I don't know, if that should have been done automatically at the installation of snapd package. But any way, that's it.
For how to add new paths to the PATH environment variable look at the links bellow, to get a good idea (case you don't know that already):
https://stackoverflow.com/a/26962251/7668448
https://askubuntu.com/questions/866161/setting-path-variable-in-etc-environment-vs-profile
https://www.computerhope.com/issues/ch001647.htm
https://hackprogramming.com/2-ways-to-permanently-set-path-variable-in-ubuntu/
http://www.troubleshooters.com/linux/prepostpath.htm
https://serverfault.com/questions/166383/how-set-path-for-all-users-in-debian
Here links about why you need to logout and login back or reboot
Setting environment variable globally without restarting Ubuntu
https://superuser.com/questions/339617/how-to-reload-etc-environment-without-rebooting
Here an example:
sudo nano /etc/environment
i chose to add the path through /etc/environment (remember you can't use shell commands).
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/node-v9.6.1-linux-x64/bin:/snap/bin
You can see i add it at the end (that simple).
Reboot your computer or logout and login back (PAM script handle the construction of the PATH from /etc/environment at session creation time)
If You want to have the effect take place right away, execute:
source /etc/environment && export PATH
(it affect only the current opened shell and the children processes)
Here another example doing it in /etc/profile:
if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
PATH="$PATH:/snap/bin"
export PATH
I just added one line (the one before the last, and note that a portion from the whole file (/etc/profile)).
Reboot or logout and login back.
Execute :
source /etc/profile
to be operational right away (affect the current shell and the children processes).
There is different ways to add to PATH, even an infinity of ways if we give our imagination a go. The difference between the ways is about when it get set, and executed, and what scope it reach. As also organization aspect (i can have my own text list (one path per line), and have it compiled and executed in the right manner and place for example). Better see the links above, i put a good selection out there, to get a better understanding about how things work, and what method to choose. But generally the two above for a system wide configuration, are mostly what you need.
Do remember to actually source the installation file.
wget -0- wget https://toolbelt.heroku.com/install-ubuntu.sh | sh
didn't work for me. And as a linux noob I used instead:
wget 0- wget https://toolbelt.heroku.com/install-ubuntu.sh | sh
notice that the '-' is missing from the option to wget. This downloaded the install source to my current directory.
then I did:
bash install-ubuntu.sh
which finished up the installation for me.
then:
heroku login
works!!
Just run
$ gem install heroku
Form your app that's it.
I am using zsh which didn't have snap in its path. So just add this in ~/.zshrc.
export PATH=$PATH:/snap/bin
try npm install -g heroku for any platform.
Ran gem install heroku first and it gave me the following message:
heroku must be installed from cli.heroku.com. This gem is no longer available. (RuntimeError)
Steps from Heroku:
brew tap heroku/brew && brew install heroku
or Ubuntu
sudo snap install --classic heroku
when you install heroku in linux as per the documentation using
sudo snap install heroku --classic
it will install heroku inside /snap/bin/heroku
but when you type the command in terminal it will look into /usr/bin/ directory,
a simple solution is to create a symlink by
sudo ln -s /snap/bin/heroku /usr/bin/heroku
after that you can just run the heroku command in terminal.
First install heroku:
wget -qO- https://toolbelt.heroku.com/install.sh | bash
After that add a symlink to binary like #Garrett did:
sudo ln -s /usr/local/heroku/bin/heroku /usr/bin/heroku
Export snap Directory
export PATH=$PATH:/snap/bin
For yarn
If you want to deploy your backend or server, go to backend or server folder, use -
yarn global add heroku
For deploying frontend or client, go to frontend or client folder and use the same cmd.
For npm
Go to the respective folder which you want to deploy and use npm i -g heroku
After installing Heroku Toolbelt using the .pkg file I downloaded from Heroku's Getting Started with Rails 4.x on Heroku page, I got the heroku command not found message. My /usr/local/heroku/bin folder did exist.
I was able to resolve this issue by going to https://toolbelt.heroku.com and downloading the same .pkg file from that site and re-installing it. Note, I did not uninstall the previous package first.
After you run wget -0- wget https://toolbelt.heroku.com/install-ubuntu.sh | sh you might get the following warning:
WARNING: The following packages cannot be authenticated!
heroku heroku-toolbelt
If this happens, run this apt-get install -y --force-yes heroku-toolbelt
I've run all the commands with sudo, but I don't know if it makes a difference. Thanks to this answer
Brew install did not work in macOS?
For me brew tap heroku/brew && brew install heroku did not work in macOS.
So I tried the standalone download.
Here is the command which worked for me
curl https://cli-assets.heroku.com/install.sh | sh

Resources