Using pyenv on Ubuntu
Pyenv is a tool that allows you to easily install multiple different Python versions and flawlessly switch between them. Pyenv is not a replacement for virtual environment but it can also help you manage those.
The easiest way to obtain pyenv along with a set of useful plugins is the following
1 | $ curl https://pyenv.run | bash |
You can list plugins installed via
1 | $ ls -1 ~/.pyenv/plugins/ |
The pyenv-virtualenv one is used to manage you virtual environments and also will be mentioned below.
Update your shell login file ~/.bashrc
with content
1 | export PATH="/home/user/.pyenv/bin:$PATH" |
and restart terminal or source ~/.bashrc
in current session and you are good to go.
Install new version of Python
To make sure every version of Python will be built without any errors and all the modules will work out of the box you should install development packages in advance
1 | $ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \ |
After that you want to check which options you are allowed to choose from
1 | $ pyenv install --list |
and to be more specific you can do something like pyenv install --list | grep 3.5
1 | $ pyenv install 3.5.6 |
If you are interested about what’s happening under the hood you can provide --verbose
flag. Once you successfully installed it you can list all the versions available with
1 | $ pyenv versions |
NOTE: Sometimes you might need to pass custom flags to a build process in order to tune your installation. This can be done in a such way $ CONFIGURE_OPTS=--enable-shared pyenv install 2.7.6
The best way to deal with multiple Python versions on the same system is by leveraging virtual environments (see the section below). But in case you want to set a version globally (across every shell) or locally (per specific directory) you can use commands
1 | $ pyenv global 2.7.6 # will be used py all shells |
Working with virtual environments
Create virtual environment
1 | $ pyenv virtualenv [version] [name] |
Activate earlier created virtual environment
1 | $ pyenv activate [name] |
and deactivate it (usual deactivate
will not work)
1 | $ pyenv deactivate [name] |
You can also spawn a REPL with specific python version without creating virtual environment in current shell like this
1 | $ pyenv shell 3.5.6 # set specified python version for current session |
To list all available virtual environments on your system invoke
1 | $ pyenv virtualenvs |