Pyenv is a fantastic tool for installing and managing multiple Python versions. It enables a developer to quickly gain access to newer versions of Python and keeps the system clean and free of unnecessary package bloat. It also offers the ability to quickly switch from one version of Python to another, as well as specify the version of Python a given project uses and can automatically switch to that version. This tutorial covers how to install pyenv on Ubuntu 18.04.
It’s always a good idea to start off any installation process by updating system packages:
root@ubuntu:~# apt update -y
Once that has finished up, run the following command to install all of pyenv’s dependencies:
root@ubuntu:~# apt 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\
> git
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
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"
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
The list of the available version is long. Let’s go ahead and install Python version 3.8.3:
root@ubuntu:~# pyenv install 3.8.3
Downloading Python-3.8.3.tar.xz...
-> https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tar.xz
Installing Python-3.8.3...
Installed Python-3.8.3 to /root/.pyenv/versions/3.8.3
Do not be surprised if it takes a while for this command to run. Pyenv is building this version of Python from source.
To verify that Python 3.8.3 is now installed run the pyenv versions command:
root@ubuntu:~# pyenv versions
* system (set by /root/.pyenv/version)
3.8.3
Now for further verification, change the version of Python to 3.8.3 and drop into a python shell.
root@ubuntu:~# pyenv global 3.8.3
root@ubuntu:~# python
Python 3.8.3 (default, Jun 10 2020, 22:45:23)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Switching back is just as easy!
Finally, to get an idea of all the commands and features pyenv has to offer, run the following command:
root@ubuntu:~# pyenv help
Usage: pyenv <command> [<args>]
Some useful pyenv commands are as follows.
The full documentation for pyenv can be found at GitHub.
There you have it! With pyenv installed, you’re off and running with more granular control of your Python environment!
#tutorials #apt #debian #environment #git #github #linux #package #package management #package manager #personal package archive #ppa #pyenv #python #python 3 #python support #python-pip #repository #smb #software #source install #ubuntu #ubuntu 18.04 #venv #virtualenv #web application development