Pyenv is an excellent tool for installing and managing several Python versions. It keeps the system tidy and clear of unused package bloat. It enables developers to access newer versions of Python. It allows users to set a project’s Python version and easy version switching. This guide covers installing Pyenv on Ubuntu 18.04, 20.04, and 23.04. If curious about improving the server’s security setup, there’s an excellent resource. Especially on configuring firewalls in Ubuntu, which you might find particularly useful.

Many people use the utility Pyenv to handle various Python versions on a single system. It enables simple installation, switching, and management. Enables many Python versions for your development projects. You can establish isolated Python environments and guarantee compatibility. Specifically, 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, and 23.04.

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

With the following steps understand in detail what will help you to install pyenv on Ubuntu. With dedicated hosting, you get the entire physical server access to yourself for maximum uptime and complete control.

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 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 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. The VPS hosting provider gives highly efficient infrastructure to your business by conveying extraordinary performance.

When you conclude, you will have a clear picture or concept about installing pyenv on Ubuntu versions 18.04, 20.04, or 22.04, 23.04, etc. To manage multiple websites or database functionalities, you need VestaCP to install 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.

Frequently Asked Questions (FAQ)

1. What should be done in case Pyenv fails to install a Python version because of missing dependencies?

When Pyenv fails to install a certain Python version, it generally indicates the absence of some build dependencies. If you are unsure, make sure you install them in Ubuntu by running: [sudo apt-get update && sudo apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev curl libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev].

These are the libraries required for the applications that compile Python. Should the problem persist, the specific missing dependencies will show up in the error logs on the terminal or in ~/.pyenv/cache, which you can then remedy by installing the said dependencies.

2. Can you use Pyenv to manage Python versions for a specific project without affecting the environment?

Yes, it is possible to use pyenv to set Python versions on a per-project basis using pyenv local. In your project directory, run pyenv local <version> to create a .python-version file inside that directory. Pyenv will then immediately switch to that version any time you enter the directory. Run python– version inside the project folder to check that it works. This isolates the Python environment per project without modifying the global or system-wide Python settings.

3. Can I keep Pyenv up to date to include the newest versions of Python?

A simple git pull would fetch the changes if you navigate to the Pyenv directory (~/.pyenv). Update also your Python version definitions by issuing pyenv update if you have the pyenv-update plugin; otherwise, reinstall Pyenv with curl https://pyenv.run | bash. Once this is done, check for available Python versions with pyenv install– list and install the required one using pyenv install <version>. This is guaranteed to be supported by the latest Python releases.