How To Install Pyenv on Ubuntu 24.04 LTS

Install Pyenv on Ubuntu 24.04

In this tutorial, we will show you how to install Pyenv on Ubuntu 24.04 LTS. Python has become one of the most popular programming languages in recent years, thanks to its versatility, simplicity, and powerful libraries. As a Python developer, you may find yourself working on multiple projects that require different versions of Python. This is where Pyenv comes in handy. Pyenv is a powerful tool that allows you to easily manage multiple Python versions on a single system, making it a must-have for any serious Python developer.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the Pyenv on Ubuntu 24.04 (Noble Numbat). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • Basic familiarity with the terminal and command-line interface.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • At least 1 GB of RAM (3 GB or more recommended).
  • Minimum 5 GB of free disk space (25 GB or more recommended).
  • An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies.
  • An Ubuntu 24.04 system with root access or a user with sudo privileges.

Install Pyenv on Ubuntu 24.04 LTS

Step 1. Updating the Package Repository.

The first step in installing Pyenv is to update your system packages and install the required dependencies. Open your terminal and run the following commands:

sudo apt update
sudo apt upgrade

Next, install the necessary dependencies:

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

This command will install all the packages and tools required for Pyenv to function properly on your Ubuntu 24.04 LTS system.

Step 2. Installing Pyenv.

With the dependencies installed, we can now proceed to install Pyenv itself. The easiest way to install Pyenv is by using the official installer script. Follow these steps:

curl https://pyenv.run | bash

This command will download and execute the Pyenv installer script, which will clone the Pyenv repository and set up the necessary configuration files.

Verify the installation by checking the Pyenv version:

pyenv --version

If the installation was successful, you should see the version number of Pyenv displayed in the terminal.

Step 3. Configure Shell Environment.

To use Pyenv effectively, you need to configure your shell environment. This involves updating your shell configuration files to include Pyenv-related paths and initializing Pyenv when the shell starts. Follow these steps:

Open your shell configuration file (e.g., ~/.bashrc for Bash) using a text editor:

nano ~/.bashrc

Add the following lines to the end of the file:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"

Save the changes and exit the text editor (press Ctrl+X, then Y, and finally Enter in nano).

Reload the shell to apply the changes:

exec "$SHELL"

Step 4. Installing Python Versions Using Pyenv.

With Pyenv installed and configured, you can now easily install and manage different Python versions. Here’s how:

List all available Python versions:

pyenv install --list

Install a specific Python version (e.g., Python 3.9.7):

pyenv install 3.9.7

Set the global Python version:

pyenv global 3.9.7

Set the local Python version for a specific project directory:

pyenv local 3.9.7

Step 5. Using Pyenv with Virtual Environments.

Pyenv integrates seamlessly with virtual environments, allowing you to create isolated Python environments for your projects. To use Pyenv with virtual environments, follow these steps:

Install the pyenv-virtualenv plugin:

git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

Configure your shell to load the pyenv-virtualenv plugin:

echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc

Reload your shell:

exec "$SHELL"

Create a new virtual environment:

pyenv virtualenv 3.9.7 myenv

Activate the virtual environment:

pyenv activate myenv

This command activates the myenv virtual environment, allowing you to install packages and run Python scripts within the isolated environment.

Congratulations! You have successfully installed Pyenv. Thanks for using this tutorial for installing Pyenv on the Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official Pyenv website.

[su_box title=”VPS Manage Service Offer” style=”bubbles” box_color=”#000000″ radius=”10″]If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal![/su_box]

r00t is a Linux Systems Administrator and open-source advocate with over ten years of hands-on experience in server infrastructure, system hardening, and performance tuning. Having worked across distributions such as Debian, Arch, RHEL, and Ubuntu, he brings real-world depth to every article published on this blog. r00t writes to bridge the gap between complex sysadmin concepts and practical, everyday application — whether you are configuring your first server or optimizing a production environment. Based in New York, US, he is a firm believer that knowledge, like open-source software, is best when shared freely.

Related Posts