31

I'd like to add Oh My Zsh to my Vagrant bootstrap process, but a straight install isn't working.

via curl:

curl -L http://install.ohmyz.sh | sh

via wget:

wget --no-check-certificate http://install.ohmyz.sh -O - | sh
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
Alan Quigley
  • 1,642
  • 2
  • 15
  • 19

3 Answers3

34

Found the solution:

# Added zsh shell.
sudo apt-get install zsh
wget --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh 
sudo chsh -s /bin/zsh vagrant
zsh

As an nice addition, so that your terminals don't look too similar on the different boxes

# Change the oh my zsh default theme.
sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="3den"/g' ~/.zshrc
Alan Quigley
  • 1,642
  • 2
  • 15
  • 19
  • Where in the Vagrantfile do you add this? – damianesteban Sep 17 '14 at 03:07
  • In the bootstrap file. You may need to install git as well. – Alan Quigley Sep 18 '14 at 05:16
  • can you provide an example vagrantfile? – SuperUberDuper Dec 15 '15 at 13:45
  • ```sudo apt-get install -y zsh```to automatically install (i need this on Debian) – ch. vonrohr Jan 23 '18 at 15:47
  • Maybe in the future the robbyrussel theme will be not the default one anymore, so you can use `sed -i 's/ZSH_THEME=[^ ]*/ZSH_THEME="3den"/g' ~/.zshrc` – BntMrx Jun 16 '20 at 17:36
  • I try this, but the `zsh` and `oh-my-zsh` installed to `root`. Not in user `vagrant`. So I must install they as user-vagrant but under root. `su -l vagrant -s "/bin/sh" -c "curl -fsSO https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh; chmod 755 install.sh; ./install.sh --unattended"; chsh -s /bin/zsh vagrant` It is a snippet of my bootstrap. And it works. – Dzulfikar Adib Jun 23 '21 at 02:53
24

Here's a complete Vagrantfile that installs Oh My Zsh on an Ubuntu 14.04.2 LTS box and sets it as the default shell for standard vagrant user.

This works with Vagrant 1.7.2. (Your milage may vary with different versions.) It uses the directions from the Manual Installation section of the Readme instead of trying to use the automatic scripts.

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # Pick a box to use:
  config.vm.box = "ubuntu/trusty64"

  ############################################################
  # Oh My ZSH Install section

  # Install git and zsh prerequisites 
  config.vm.provision :shell, inline: "apt-get -y install git"
  config.vm.provision :shell, inline: "apt-get -y install zsh"

  # Clone Oh My Zsh from the git repo
  config.vm.provision :shell, privileged: false,
    inline: "git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh"

  # Copy in the default .zshrc config file
  config.vm.provision :shell, privileged: false,
    inline: "cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc"

  # Change the vagrant user's shell to use zsh
  config.vm.provision :shell, inline: "chsh -s /bin/zsh vagrant"

  ############################################################


end

As a bonus, you can do a one time copy of your host machine's .zshrc file to the vagrant box with:

config.vm.provision "file", source: "~/.zshrc", destination: ".zshrc"

(Keep in mind, You may have to figure things that don't work initially because of differences between the host machine and the vagrant box's setups.)

Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
  • I done `vagrant reload` and I can't use zsh commands like `..` – SuperUberDuper Dec 15 '15 at 14:13
  • I get the zsh installing if I do `vagrant destroy` but then I get error: `==> default: fatal: destination path '/home/vagrant/.oh-my-zsh' already exists and is not an empty directory.` – SuperUberDuper Dec 15 '15 at 14:15
  • 2
    Try running `vagrant reload --provision`, if the box is currently running. Otherwise, `vagrant up --provision` if it is not running. This worked for me. – hellojason May 31 '16 at 18:59
0

I came here because had a same issue. After seeing some answer and trying it, mostly the zsh & oh-my-zsh got installed as root. The root will set his $SHELL with zsh. What I want is they are installed as user vagrant. The bootstrap was done by root when provisioning. So the logic is try to run install zsh & oh-my-zsh as user. Here is what I did after trying many times until I got what I wanted :

## In Vagrantfile try to call bootstrap.sh
config.vm.provision "shell", path: "bootstrap.sh"
## This is the bootstrap.sh
aptInstl() {
  DEBIAN_FRONTEND=noninteractive apt-get install -qq -y $1 > /dev/null
}
install_zsh() {
  aptInstl "zsh"
  su -l vagrant -s "/bin/sh" \
    -c "curl -fsSO https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh; chmod 755 install.sh; ./install.sh --unattended"
  chsh -s /bin/zsh vagrant
}
install_miscellaneous() {
  apt-get update > /dev/null
  apt-get upgrade > /dev/null
  for i in curl git; do
    aptInstl "$i"
  done
}
main() {
  install_miscellaneous
  install_zsh
}
main

And It works perfectly :)
When you are done try to vagrant ssh it will automatically logged you in with zsh shell and oh-my-zsh. Here is the complete file.

Dzulfikar Adib
  • 116
  • 1
  • 7