Installing Python is fairly easy. You can go to their official site and download the latest python version or browse through the list of versions and download it. This may be okay if you are planning to learn python, working with the same version and all. But what if you have multiple projects with multiple python projects? It may not be practical for you to download every python version as per the project. Therefore you require a python version manager for this task.
Pyenv is the simple solution for this. It easily lets you install any python version and switch between them with ease.
Getting Started
We will look into how to get pyenv up and running in ubuntu. You can always refer to the official docs for all other OS.
Ubuntu already comes with python preinstalled.
$ python -V
Python 2.7.17
$ which python
/usr/bin/python
1. Before pyenv, python build dependencies must be installed https://github.com/pyenv/pyenv/wiki#suggested-build-environment
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
2. Clone pyenv to the root " $HOME/.pyenv"
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
3. Defining environment variable PYENV_ROOT to point to the pyenv repo and add $PYENV_ROOT/bin
to your $PATH
command line access
Zsh:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
Bash:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
Ubuntu Desktop:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
You can view the respective file to check if the change has occurred.
In Zsh, view ~/.zshrc
file if the following code is there at the end
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
4. Add pyenv init to the shell which enables shims and autocompletion
Zsh:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc
Bash:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Ubuntu Desktop:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
*Note please refer to https://github.com/pyenv/pyenv if any problem occurs
Similarly, after doing this, there will be the addition of a new line in ~/.zshrc
file
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
5. Restart your shell/terminal for changes to occur
exec "$SHELL"
All done now. You check if it is working as it must by running $ which python in terminal. You will notice that the path is changed.
$ which python
/home/user/.pyenv/shims/python
Also, you can check if pyenv is working by checking its version.
$pyenv --version
pyenv 1.2.22-63-g421ff608
List Python Versions
You can list available python version in your system by
pyenv versions
For listing available python version to download by
pyenv install --list
Install Python version
pyenv install 3.9.1
Change global python version by
pyenv global 3.9.1
pyenv global system #changes back to the system python
Change local python version (application-specific) by
pyenv local 3.9.1
Top comments (0)