I'm using this vagrantfile:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"
...bla bla bla bla bla...
config.vm.provision "shell", path: "provision/setup.sh", privileged: false
end
In my setup.sh I have:
sudo apt-get update
sudo apt-get install --yes git-all libreadline-dev build-essential curl git m4 python-setuptools ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
yes | ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
echo PATH=$HOME/.linuxbrew/bin:$PATH >> ~/.bashrc
export PATH=$HOME/.linuxbrew/bin:$PATH
brew doctor
then I have:
brew install rbenv ruby-build
brew update
brew upgrade
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
. ~/.bashrc
rbenv init
rbenv install 2.3.0
rbenv global 2.3.0
rbenv local 2.3.0
rbenv versions
gem install bundler
but I receive this error:
==> default: ERROR: While executing gem ... (Errno::EACCES) ==> default: Permission denied - /var/lib/gems`
How can I install "bundler" and other "gems" with my setup script?
It's not a good idea to run sudo in a script, but try sudoing the script itself with
$ sudo ./script.sh
and take all your 'sudos' out of your sh script.
Next change
config.vm.provision "shell", path: "provision/setup.sh", privileged: false
to
config.vm.provision "shell", path: "provision/setup.sh", privileged: true
This way sudo is only unlocked for the file, and it has control for whatever you are doing.
If this is a new file in the machine, you may need to use
chmod +x ./script.sh
for your os to execute it properly. From what I see, things should work okay unless you have accidentally provisioned your vm twice in which you may want to override it. You also can try running each command yourself to see if it is an isolated issue outside of vagrant.
edit:
The main issue is that the /var/lib/gems directory is own by root, so by whatever means get that under the sudo command.
edit: Massive clarity.
Related
I'm using this vagrantfile:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"
...bla bla bla bla bla...
config.vm.provision "shell", path: "provision/setup.sh"
end
Since I want to install Linuxbrew I have in my provision/setup.sh this code:
sudo apt-get update
sudo apt-get install --yes git-all libreadline-dev build-essential curl git m4 python-setuptools ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
# or maybe also this: (but nothing anyway):
# sudo git clone https://github.com/Linuxbrew/linuxbrew.git /home/vagrant/.linuxbrew
export PATH=$HOME/.linuxbrew/bin:$PATH
brew doctor
But I retrieve errors:
==> default: /tmp/vagrant-shell: line 35: brew: command not found
How to fix this?
There's an issue how you run your script - as you run with config.vm.provision "shell", path: "provision/setup.sh" vagrant will run it as root user and so you do not need sudo
however you should really run it as your user so do config.vm.provision "shell", path: "provision/setup.sh", privileged: false
also the export will not be saved for your future session so add it to .bashrc file something like echo PATH=$HOME/.linuxbrew/bin:$PATH >> .bashrc so the final script would look like
sudo apt-get update
sudo apt-get install --yes git-all libreadline-dev build-essential curl git m4 python-setuptools ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
yes | ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
echo PATH=$HOME/.linuxbrew/bin:$PATH >> ~/.bashrc
export PATH=$HOME/.linuxbrew/bin:$PATH
brew doctor
The export is needed if you run brew from the script but note that brew doctor will likely ends up with warning and do not return so you might end up seeing vagrant message as
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
and finally for the original error, #BMW gets all credit adding yes | to the command will default the enter key on the question
Let me guess. Add yes before the ruby command
yes | ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
When running my docker container it knows rbenv global. But when ever it try to find ruby via ruby or which ruby or whereis ruby I get nothing. It also doesn't recognize rails-api or gem. What is going on?
Dockerfile
FROM centos:6.6
RUN yum update -y
RUN yum install git openssl-devel openssh-server sudo openssl readline-devel readline zlib-devel zlib libxml2-devel libxml2 libxslt-devel libxslt nginx tar gcc libaio libaio-devel -y
RUN rpm -Uvh https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.5.1-1.el6.x86_64.rpm
RUN sed -i -e "s/Defaults requiretty.*/ #Defaults requiretty/g" /etc/sudoers
RUN mkdir -p /var/run/sshd
RUN useradd -m -u 1000 -G wheel deploy && echo '%wheel ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/wheel
USER deploy
RUN mkdir ~/dev
RUN git clone https://github.com/sstephenson/rbenv.git ~/.rbenv/
RUN git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
ENV PATH ~/.rbenv/bin:$PATH
RUN echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
RUN source ~/.bash_profile
ENV CONFIGURE_OPTS --disable-install-doc
RUN rbenv install 2.2.3
RUN rbenv global 2.2.3
RUN bash -l -c 'gem update --system'
RUN bash -l -c 'gem update'
RUN bash -l -c 'gem install nokogiri -- --use-system-libraries'
RUN bash -l -c 'gem install bundler rails-api --no-rdoc --no-ri'
COPY oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm /tmp/oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
COPY oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm /tmp/oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
COPY oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm /tmp/oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm
RUN sudo rpm -Uvh /tmp/oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
RUN sudo rpm -Uvh /tmp/oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
RUN sudo rpm -Uvh /tmp/oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm
RUN sudo touch /etc/sysconfig/network
RUN NLS_LANG=American_America.UTF8
ENV ORACLE_HOME=/usr/lib/oracle/12.1/client64
ENV LD_LIBRARY_PATH=/usr/lib/oracle/12.1/client64/lib
EXPOSE 22
EXPOSE 3000
EXPOSE 5000
The issue at hand here is that every RUN statement in the Dockerfile happens in its own environment. The RUN source ~/.bash_profile will set $PATH to have /root/.rbenv/shims in it. But subsequent RUN will not have this environment set as they won't source .bash_profile since this isn't an interactive shell.
The easy way to solve this is to just add a ENV PATH $HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH somewhere before the ruby and gem commands. This is really the only bit you should need from the rbenv init shell script.
Have you tried rbenv rehash after installing the new Ruby version?
Your Dockerfile only uses one version of Ruby. Given your use case, I would recommend just using the official ruby docker images: https://hub.docker.com/_/ruby
I have a vagrant provisioning script which succeeds -- I can see the output from the logs and my dependencies are being installed, my directories and files are being created and copied over, etc. but when I vagrant ssh into the VM none of the folders, files, env variables, and installations are there.
Edit: git, curl, etc. work, but gvm, go, and $GOPATH etc do not, and my go directory does not exist
I'm confident the provisioning works correctly because I can run my web server from the script and confirm the application is being served.
Is this just the way Vagrant is set up? What's the point of vagrant ssh if so?
I'm running the default "hashicorp/precise32" box, Ubuntu 12.04, default provider.
Shell script
#! /bin/bash
echo "Provisioning virtual machine"
sudo apt-get update
echo "Installing Dependencies"
# Base dependencies: curl git
# Dependencies for gvm: make bison
sudo apt-get install curl git make bison -y 2> /dev/null
# Dependencies for add-apt-repository: python-software-properties software-properties-common
sudo apt-get install python-software-properties software-properties-common -y 2> /dev/null
# This allows us to get an updated version of git, which we need for gvm
sudo add-apt-repository ppa:git-core/ppa -y 2> /dev/null
sudo apt-get update
sudo apt-get install git -y 2> /dev/null
echo "Installing GVM"
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
source ~/.gvm/scripts/gvm
echo "Installing and configuring Go"
gvm install go1.4
gvm use go1.4 --default
mkdir -p ~/go/{bin,pkg,src}
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
echo "Installing Nginx"
sudo apt-get install nginx -y 2> /dev/null
Vagrantfile
`# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "hashicorp/precise32"
config.vm.provision :shell, path: "init.sh"
config.vm.network :forwarded_port, host: 4000, guest: 8080
end`
The Vagrant provisioning script is running as root, so ~ refers to /root instead of /home/vagrant.
Options to resolve are su -c "*your command*" vagrant or using absolute paths, which is probably the best approach.
Similar issue: Why is my Vagrant bootstrap file not modifying bash_login?
And more detail in my answer here: Vagrant - Rails Not Installed
I am trying to set versiĆ³n ruby version in the vagrant user with rvm using the following script into the vagrant file:
config.vm.provision "shell", inline: <<-SHELL
sudo apt-get -y update
sudo apt-get -y install git ruby libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
nodejs
#Install ruby environment
curl -sSL https://rvm.io/mpapis.asc | gpg --import -
curl -L https://get.rvm.io | bash -s stable
sudo "source ~/.rvm/scripts/rvm"
echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
sudo /usr/local/rvm/bin/rvm install 2.1.5
rvm 2.1.5 --default
sudo chown -R vagrant:vagrant /usr/local/rvm/gems/
gem install middleman
git clone XXXX
SHELL
END
Vagrant is returning me the following message:
/tmp/vagrant-shell: line 10: rvm: command not found
But if i run the command with the full path it returns me the following message:
/usr/local/rvm/bin/rvm 2.1.5 --default
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.
Is there anyway to execute the inline script in logging mode or other way to use rvm to set the default version?
Thanks :)
The alterations to the script provided would prevent the code above from exiting with an bad exit status.
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.provision "shell", inline: <<-SHELL
RUBY_VERSION="2.1.5"
sudo apt-get -y update
sudo apt-get -y install git nodejs
# Install ruby environment
if ! type rvm >/dev/null 2>&1; then
curl -sSL https://rvm.io/mpapis.asc | gpg --import -
curl -L https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
fi
if ! rvm list rubies ruby | grep ruby-${RUBY_VERSION}; then
rvm install ${RUBY_VERSION}
fi
rvm --default use ${RUBY_VERSION}
rvm all do gem install middleman
git clone <REPLACE_WITH_YOUR_REPO>
SHELL
end
Explanation of changes:
There is no need to source "~/.rvm/scripts/rvm". Firstly, it fails because it doesn't exist as mentioned previously due to vagrant running as a non-login shell. Secondly the installation of rvm creates startup files in /etc/profile.d/rvm.sh which handles this case for you. See https://rvm.io/integration/gnome-terminal. You will still see a warning (in red) in the vagrant's console output but the script doesn't fail because of a bad exit status. As a result we need to source /etc/profile.d/rvm.sh immediately after installing RVM because our current shell hasn't loaded rvm.sh yet. This was mentioned in the RVM installation output. An alternative is to break up the script into multiple parts to force trailing scripts to pick up the new path.
As vagrant is running as a non-login shell there is no need to change ownership of the /usr/local/rvm/gems/ folder. I think at some point vagrant did run scripts as the vagrant user but this has changed in more recent versions. Scripts will actually run as root; this occurs by default due to the privileged option on shell scripts; see Shell Provisioner. If you are uncertain which user is running you can do a whoami in the script. The script will run as the vagrant user if you de-escalate the privileges by setting privileged => false.
Added a conditional block around the rvm GPG key and installation. Unless you need rvm installed on every provision. An argument could be made that you might be trying to keep it up-to-date but that could potentially introduce unknowns and break the repeatable results from one day to the next.
Added a conditional block around the installation of the ruby version. This prevents the warning regarding the package is already installed, use reinstall.
Cleaned up the packages you were installing. You might re-look at this but the packages you were installing with apt-get will be automagically installed by the rvm installer and add extra fluff to your scripts.
Alternative and bit more flexible method
If you aren't stuck on using inline scripts. I would go the route as described in Using RVM with Vagrant. Some of the suggestions I made above I would re-apply to the general logical of the scripts in this article. One change that would be REQUIRED is in the install-rvm.sh script. Add the GPG key import; shown below:
#!/usr/bin/env bash
curl -sSL https://rvm.io/mpapis.asc | gpg --import -
curl -sSL https://get.rvm.io | bash -s $1
I am not sure if I should use Puppet for this. I update and install through provision.sh.
My Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu32"
config.vm.provision :shell, path: './provision.sh'
config.vm.network "public_network"
end
provision.sh
apt-get update
apt-get -y install build-essential git-core python-software-properties nodejs
apt-get -y install vim
apt-get -y install curl
curl https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash
Now I need to add the following to ~/.bashrc at the top. Or I can prepare a file .bashrc and replace it with ~/.bashrc
export RBENV_ROOT="${HOME}/.rbenv"
if [ -d "${RBENV_ROOT}" ]; then
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"
fi
Then run source .bashrc
Then run following commands.
rbenv install 2.0.0-p247
rbenv rehash
gem install bundler
bundle
sudo apt-get install libpq-dev
gem install pg -v '0.15.0'
You can do this in your provision.sh script. Vagrant automatically shares the directory where your Vagrantfile lives with the guest VM as the /vagrant folder.
Create the .bashrc file as you want it, and put it in the same directory as your Vagrantfile. I would leave out the '.' and call it bashrc so you don't loose track of it.
Then you can add to your provision.sh script:
cp /vagrant/bashrc ~/.bashrc
source ~/.bashrc
Note: the bash provisioning runs as the root user, you'll have to modify this a bit if you want to use it as a non-root user.
cp /vagrant/bashrc /home/<username>/.bashrc
su - <username> -c "<command to run as user>"