Skip to content

Instantly share code, notes, and snippets.

@atishayjain708
Created January 10, 2021 20:36
Show Gist options
  • Save atishayjain708/b263208a1b8c5582d5e5b8bcd219e80b to your computer and use it in GitHub Desktop.
Save atishayjain708/b263208a1b8c5582d5e5b8bcd219e80b to your computer and use it in GitHub Desktop.
Setting up Python on a new machine using pyenv

Install pyenv

Ref: https://www.liquidweb.com/kb/how-to-install-pyenv-on-ubuntu-18-04/

Step #1: Install pyenv and dependencies using:

sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Step #2: Clone the Repository To install the latest version of pyenv and provide a straightforward method for updating it, run the following command to pull it down from GitHub:

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Step #3: Configure the Environment Next, to properly configure pyenv for use on the system, run the following block of commands to set some important environment variables and setup pyenv autocompletion:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc

Finally, to start using pyenv, restart the shell by running:

root@ubuntu:~# exec "$SHELL" Step #4: Verify the Installation To verify that pyenv is installed correctly, we will try installing a new version of Python. First, we will list the available versions of Python:

root@ubuntu:~# pyenv install --list

pyenv is now installed if you see a lot of differnet packages show up.

Install a specific version of python using pyenv

Install pyenv-virtualenv

Clone git repo: git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

Add to bash profiel for auto-activation:

echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile

Creating a virtual environment using pyenv (and a specific version of python)

pyenv virtualenv <python version you need> <name of env>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment