简体   繁体   中英

Install pyenv on a Vagrant Ubuntu 20.04 VM

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: 在此处输入图像描述

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM