I have created a vagrant box for running rails applications and I have managed to create it manually.

My next step was to create a shell script that I can include in the Vagrantfile so when creating new boxes all installation will be done automatically.

But when I reach the line:

source ~/.bash_profile 

I get this error

mkdir: cannot create directory `/home/vagrant/.rbenv/shims': Permission denied
mkdir: cannot create directory `/home/vagrant/.rbenv/versions': Permission denied

Works fine from CLI

Any ideas?


I have fixed the mkdir error and the script runs from end to end with no apparent errors.

Now when I vagrant ssh and check my home directory I do not find any of the git repos I downloaded and installed using my script nor .bash_profile hence I can not rbenv

Any ideas why this may happen - what am I doing wrong?

vagrant@precise64:~$ ls -a
.   .bash_history    .cache          .profile  .sudo_as_admin_successful  .veewee_version
..  .bash_logout   .bashrc        postinstall.sh  .ssh      .vbox_version

this is setup.sh:

# Update sources:
sudo apt-get -y update

# Install development tools:
sudo apt-get -y install build-essential

# Packages required for compilation of some stdlib modules
sudo apt-get -y install tklib 

# Extras for RubyGems and Rails:
sudo apt-get -y install zlib1g-dev libssl-dev

# Readline Dev on Ubuntu:
sudo apt-get -y install libreadline-gplv2-dev

# Install some nokogiri dependencies:
sudo apt-get -y install libxml2 libxml2-dev libxslt1-dev

# Install Git
sudo apt-get -y install git-core 

# Install Sqlite
sudo apt-get -y install sqlite3 libsqlite3-dev 

# Install Make
sudo apt-get -y install make

# Install NodeJS (Required for Rails)
sudo apt-get -y install python-software-properties
sudo add-apt-repository -y ppa:chris-lea/node.js
sudo apt-get -y update
sudo apt-get -y install curl nodejs

# Install RBENV
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
touch ~/.bash_profile
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

# Install Ruby 2.1.0
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

sudo sh ~/.rbenv/plugins/ruby-build/install.sh
rbenv install 2.1.0
rbenv rehash
rbenv global 2.1.0

# Install gems for Rails
# gem install rdoc
gem install bundler
# gem install rake 
gem install sqlite3 -v '1.3.9'
gem install rails
rbenv rehash

Many thanks in advance.

  • 1st of all, you do not want a command that attempts to create a directory in your ~/.bash_profile. Just i) create the directories once and leave it at that or ii) create them from the script instead or iii) at least use mkdir -p so it will fail silently if the dir exists. That said, the errors you are getting are permissions problems. What user is running setup.sh? It looks like you're not running it as vagrant. – terdon May 22 '14 at 16:14
  • this is how I call my script from within Vagrantfile config.vm.provision :shell, :path => "setup.sh" – Altons May 22 '14 at 16:26
  • We need to know i) which user is launching the script (try adding echo whoami to setup.sh to check), and ii) What the permissions are for /home/vagrant/.rbenv/. – terdon May 22 '14 at 16:28
  • I am not creating any folder myself - I think is the "offender" is rbenv init - in this case – Altons May 22 '14 at 16:28
  • I think we you do vagrant up the default root user vagrant kicks off the script. am I wrong? – Altons May 22 '14 at 16:29

This looks familiar to me. I've written some shell scripts for Vagrant provisioning and did hit a lot of issues before I can run it.

This could be related to rbenv, check the following

  1. You don't need sudo to install Ruby via rbenv + ruby-build (see you get permission error?)
  2. The correct way to install Ruby via rben is -> rbenv install 2.1.2 -v
  3. After installing ruby via rbenv, you may have to restart shell (I used exec $SHELL -l which worked fine for my script)
  4. For ubuntu, you are NOT supposed to use $HOME/.bash_profile, as per rbenv docs, you should be using $HOME/.bashrc

Fix all above and see if it works.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.