For installing and managing several Python versions, Pyenv is an excellent tool. It keeps the system tidy and clear of unused package bloat and enables developers to access newer versions of Python easily. Additionally, it allows users to specify the Python version that a particular project uses and immediately transition to that version. It also enables easy version switching between Python versions. How to install Pyenv on Ubuntu 18.04, 20.04, and 23.04 is covered in this guide. Simultaneously, Set Up a Firewall with UFW on Ubuntu you may understand it clearly by going through this.

For handling various Python versions on a single system, many people use the utility Pyenv. It enables simple installation, switching, and management of many Python versions for your development projects. You can establish isolated Python environments and guarantee compatibility with particular Python versions required by your apps by using Pyenv. You can follow the instructions in this article to install Pyenv on Ubuntu 18.04, 20.04, 22.04, 23.04

Step-to-Step Guide for Installing Pyenv on Ubuntu 20.04 | 22.04

Step 1: Install and Update Dependencies

Starting any installation process by updating system packages is usually a good idea:

root@ubuntu:~# apt update -y

Run the following command to install all of pyenv’s dependencies after that has finished:

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

Step 2: In this Step, you need to Download the Script of Pyenv in Ubuntu

With the following command and the present step, download the script of Pyenv:

$ curl https://pyenv.run | bash

Step 3: Set the Environment Up

Run the following block of commands to set certain crucial environment variables and set up pyenv autocompletion in order to correctly configure pyenv for use on the system:

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, restart the shell by running: to begin utilizing pyenv.

root@ubuntu:~# exec "$SHELL"

Step 4: Look over the Installation

We will attempt to install a fresh copy of Python to make sure Pyenv is set up properly. The Python versions that are currently available are listed first:

root@ubuntu:~# pyenv install --list

The list of versions that are offered is extensive. Let’s install Python version 3.8.3 now:

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

If this command takes a while to execute, do not be alarmed. Pyenv is building this version of Python from the source code.

Run the pyenv versions command to see if Python 3.8.3 has now been installed:

root@ubuntu:~# pyenv versions
* system (set by /root/.pyenv/version)
3.8.3

Change Python to version 3.8.3 and then drop it into a Python shell to perform additional checking.

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.
>>>

Commands Useful

Finally, execute the following command to get a sense of all the commands and features that pyenv has to offer:

root@ubuntu:~# pyenv help
Usage: pyenv <command> [<args>]

Wrapping Up on the Installation of Pyenv on Ubuntu 20.04 | 22.04

It’s simple to manage Python versions and build isolated environments for your projects on Ubuntu 18.04 by installing pyenv. You may quickly switch between various Python versions with pyenv, ensuring flexibility and compatibility in your development workflow. You may successfully set up Pyenv and start using its capabilities to speed up your Python development process by following the instructions provided in this article.

When you come to the conclusion you will have a clear picture or concept about installing pyenv on Ubuntu on versions 18.04, 20.04, or 22.04, 23.04, etc. If you have got a clear idea about installing pyenv on Ubuntu you can also go through how to Install VestaCP on Ubuntu. We tried to explain to you in a simple and detailed way all the basics and steps for the installation. Hope you got things clear through this.