Manage multiple Python versions using pyenv
In this tutorial, I will show you how to Manage multiple Python versions using pyenv. You can install pyenv on mostly all the Linux distributions and Mac also.
pyenv lets you easily switch between multiple versions of Python. It’s simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well. This project was forked from rbenv and ruby-build and modified for Python.
Before install pyenv, there are some dependencies that we need to install because of pyenv build python from source.
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
sudo yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite \ sqlite-devel openssl-devel xz xz-devel libffi-devel
1. Install pyenv
Let’s start with installing pyenv. Installation of pyenv is pretty simple. Use the following command to install pyenv.
curl https://pyenv.run | bash
In this end, you will see a warning.
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by adding
# the following to ~/.bashrc:
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
So follow the instruction and add the following in ~/.bashrc and then execute the following command:
Now you will be able to use pyenv.
Install pyenv on Mac
On Mac, you can use brew to install pyenv.
brew install pyenv
2. Install Python using pyenv
Now we have installed pyenv let’s install python using pyenv. You can install any specific version of python.
$ pyenv install 3.7.0
Installed Python-3.7.0 to /root/.pyenv/versions/3.7.0
3. List installed python versions
You can list locally installed versions with pyenv.
$ pyenv versions
* system (set by /root/.pyenv/version)
4. Set global python version
Now we will set the global python version using pyenv.
pyenv global 3.7.0
Now you can check the Python version
Python 3.7.0 (default)
So you can python 3.7.0 is now set globally. You can also check the global python version using the same command.
Or you can check installed versions. * means the current global python version.
* 3.7.0 (set by /root/.pyenv/version)
If you like the tutorial please feel free to comment below.