Home > Mobile >  Install pyenv on a Vagrant Ubuntu 20.04 VM
Install pyenv on a Vagrant Ubuntu 20.04 VM

Time:05-21

I am trying to install pyenv on my vagrant vm.

My Vagrantfile looks like this:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"
  config.vm.box_version = "20220517.0.0"

  config.vm.provision :shell, path: "./provision/install-pyenv.sh", privileged: false
  config.vm.provision :shell, path: "./provision/install-python.sh", privileged: false
end

Where ./provision/install-pyenv.sh is:

#!/usr/bin/env bash

# Install required dependencies
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl

# Run the installer
curl https://pyenv.run | bash

# Add the required variables to the ~/.bashrc file
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
source ~/.bashrc

And ./provision/install-python.sh is:

#!/usr/bin/env bash

# Install the required Python version
pyenv install -v 3.10.4

# Set the global python version
pyenv global 3.10.4

But when I run vagrant up - I get the following error: enter image description here

pyenv is not a recognised command. What am I doing wrong? Am I adding the env vars to the incorrect ~/.bashrc?

I don't really understand where I'm going wrong with this.

CodePudding user response:

Seems the best way to achieve this was to install the dependencies as a privileged user.

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"
  config.vm.box_version = "20220517.0.0"

  # Python
  config.vm.provision :shell, path: "./provision/install-pyenv-dependencies.sh"
  config.vm.provision :shell, privileged: false, path: "./provision/install-python.sh"

end

Where install-pyenv-dependencies.sh is:

#!/usr/bin/env bash

# Install required dependencies
apt-get update

apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl --fix-missing

And install-python.sh, is:

#!/usr/bin/env bash

# Run the installer
curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash

# Update the bashrc file
cat >> ~/.bashrc <<'EOL'
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
EOL

# Export the env vars
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"

# Use pyenv to install Python
pyenv install -v 3.10.4
pyenv global 3.10.4
  • Related