Home Linux How To Manage Multiple Python Versions With Pyenv On Linux

How To Manage Multiple Python Versions With Pyenv On Linux

By sk
Published: Last Updated on 2.5K views

You're a programmer who wants to test your python code on multiple different Python environments. What would you do? Install a specific python version and test your code and then uninstall that version and again install another different version and test code? No, wait! It is not unnecessary. Meet Pyenv, a command line utility to manage multiple Python versions, simultaneously. It made the python version management easier than ever. It is used to install, uninstall and switch to multiple different versions of Python.

Pyenv allows you to change the global Python version on a per-user basis, provide support for per-project Python versions, override the Python version with an environment variable, and search commands from multiple versions of Python at a time. Pyenv can be used to test your code across multiple python environments. In this tutorial, we will how to install and use Pyenv on GNU/Linux.

Install Pyenv on Linux

First of all, install the following packages to avoid the common build problems.

For Debian based systems:

$ sudo apt-get install -y make build-essential git libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev

For RPM based systems:

$ sudo yum install zlib-devel bzip2 bzip2-devel git readline-devel sqlite sqlite-devel openssl-devel xz xz-devel

For SUSE/openSUSE:

$ sudo zypper in zlib-devel bzip2 libbz2-devel git readline-devel sqlite3 sqlite3-devel libopenssl-devel xz xz-devel

We have just installed the necessary prerequisites. It is time to install Pyenv. The official and recommended way to install Pyenv is using the pyenv-installer script. All you have to do is run the following command in your shell to install pyenv.

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

At the end of the installation, the installer will notify you to add Pyenv to your PATH. In my case, I added the following lines in my ~/.bash_profile file.

 export PATH="/home/ostechnix/.pyenv/bin:$PATH"
 eval "$(pyenv init -)"
 eval "$(pyenv virtualenv-init -)"

Run the below command to take effect the changes.

$ source ~/.bash_profile

Pyenv is ready to use. Now let us see how to manage different versions of Python using Pyenv.

Manage Multiple Python Versions With Pyenv On Linux

First, let us see all available versions. To do so, simply run the following command:

$ pyenv install -l

Or,

$ pyenv install --list

You should an output something like below.

Available versions:
 2.1.3
 2.2.3
 2.3.7
 2.4
 2.4.1
 .
 .
 .
 3.6.2
 3.6.3
 3.6.4
 3.7.0b1
 3.7-dev
 3.8-dev
[...]

To list the currently installed Python versions:

$ pyenv versions
* system (set by /home/ostechnix/.pyenv/version)

By default, Pyenv uses our system default python. Here * indicates default.

Install Python

Let us say you need Python 2.7.14 and 3.6.4 versions on your system.

To do so, just run:

$ pyenv install 2.7.14

To install Python 3.6.4, run:

$ pyenv install 3.6.4

You don't need sudo rights. All Python versions will be installed under a separate directory in the pyenv root directory in your $HOME. The default pyenv root directory is ~/.pyenv. For example, Python 2.7.14 is installed under /home/ostechnix/.pyenv/versions/2.7.14 in my case.

$ ls /home/ostechnix/.pyenv/versions/
2.7.14 3.6.4

Now list the installed Python versions.

$ pyenv versions
* system (set by /home/ostechnix/.pyenv/version)
 2.7.14
 3.6.4

Switching between different Python versions

We can switch to different python in two ways, globally and locally.

Let us see the current global python version using command:

$ pyenv global
system

There are none. My system still uses the system default python. Let us switch to different Python version, for example 2.7.14.

$ pyenv global 2.7.14

Now check the default Python version:

$ pyenv versions
 system
* 2.7.14 (set by /home/ostechnix/.pyenv/version)
 3.6.4

Did you notice the star(*)? Yes, 2.7.14 is our global default Python version.

You can also specify multiple versions as global Python at once. Let's say if you have two versions of 2.7.14 and 3.6.4. If you prefer 2.7.14 over 3.6.4, just run:

$ pyenv global 2.7.14 3.6.4

Now check the global versions.

$ pyenv versions
 system
* 2.7.14 (set by /home/ostechnix/.pyenv/version)
* 3.6.4 (set by /home/ostechnix/.pyenv/version)

Now 2.7.14 and 3.6.4 are the default global versions.

We can set a local application-specific (per project) Python version by writing the version name to a .python-version file in the current directory. Please note that the local version overrides the global version, and can be overridden itself by setting the PYENV_VERSION environment variable or with the pyenv shell command.

Switch to your project directory:

$ cd myproject/

Let us switch to 3.6.4.

$ pyenv local 3.6.4

Check the current local python version.

$ pyenv versions
 system
 2.7.14
* 3.6.4 (set by /home/ostechnix/myproject/.python-version)

Setup Virtual Environment

To setup a virtual environment with specific Python version, do:

$ pyenv virtualenv 3.6.4 myenv

To make it globally available, run:

$ pyenv global myenv
(myenv) ostechnix@ostechnix:~$

You're in your virtual environment now. Do whatever you want. Install, remove python applications using pip and what not. It's all yours.

If you want to setup a local virtual environment (per directory):

CD to the your project directory and run:

$ pyenv local mylocalenv

To remove a virtual environment, just do:

$ pyenv uninstall myenv
pyenv-virtualenv: remove /home/ostechnix/.pyenv/versions/3.6.4/envs/myenv? y

Removing specific python version

To uninstall a specific python version, for example 3.6.4:

$ pyenv uninstall 3.6.4

Alternatively, simply remove the directory of the version you want to remove.

Check the currently installed python versions:

$ pyenv versions

For more details, read the help section by running the following command:

$ pyenv

Also, check "pyenv help <command-name>" for information on a specific command. For example, you can view the information of "install" command as below.

$ pyenv help install

And, that's all. As you can see, managing multiple Python versions is quite easy! If you're a Python programmer, Pyenv is an essential tool to keep in your arsenal.


Related read:


Resources:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More