1. Introduction
When developing with Python on Ubuntu, you often need different Python versions for different projects. In such cases, “pyenv” is an extremely useful version management tool. This article explains how to easily manage multiple Python versions on Ubuntu using pyenv.
The Importance of Python Version Management
Python versions evolve each year, introducing new features and security patches. However, some programs only work with older versions. Therefore, efficiently switching between required versions for specific projects is essential for smooth development.
Benefits of Using pyenv on Ubuntu
Ubuntu is a popular OS among developers and provides a smooth setup for Python environments. Using pyenv on Ubuntu allows you to easily install different Python versions and switch between them without affecting the system environment, making development environment management more convenient.
2. What is pyenv?
pyenv is a tool that allows easy management of multiple Python versions on the same system. Traditionally, using a specific version of Python required installing it system-wide. However, with pyenv, you can install independent versions per user or project, making version management much easier.
Key Features of pyenv
- Manage Multiple Versions: Install and use multiple Python versions on a single system simultaneously.
- Switch Between Versions: Easily switch Python versions per project as needed.
- Integration with Virtual Environments: Works seamlessly with tools like venv and pyenv-virtualenv for efficient virtual environment management.
Why pyenv is Useful
In development, some projects require the latest Python version, while others depend on older versions. pyenv is incredibly useful in such cases, allowing you to use different versions as needed, eliminating compatibility issues between projects.
3. How to Install pyenv on Ubuntu
To install pyenv on Ubuntu, you first need to set up several dependency packages. These dependencies are essential for pyenv to function properly.
Installing Required Dependencies
First, install the necessary packages using the following commands:
sudo apt update
sudo apt install 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
Installing pyenv
Next, install pyenv. The most common method is to clone it directly from GitHub.
curl https://pyenv.run | bash
Setting Environment Variables
To ensure pyenv works properly, add the following lines to your ~/.bashrc
(or ~/.zshrc
):
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
Now, pyenv is successfully installed. Restart your shell or run source ~/.bashrc
to apply the changes.

4. Installing and Managing Python Versions
Once pyenv is installed, the next step is to install a Python version. With pyenv, you can easily install a specific Python version and switch between them as needed.
Checking and Installing Python Versions
First, display the list of available Python versions:
pyenv install --list
From the displayed list, choose the version you want to install and run the following command:
pyenv install 3.10.8
Switching Python Versions
To set a specific version as the global default, use the following command:
pyenv global 3.10.8
To switch versions per project, use the pyenv local
command, which applies only within the specific directory:
pyenv local 3.10.8
This allows you to flexibly manage the required Python versions.
5. Creating Virtual Environments with pyenv
In addition to using pyenv, leveraging virtual environments allows you to maintain isolated Python environments for each project. Virtual environments are a useful tool for installing and managing libraries and packages without affecting other projects.
Using venv
On Ubuntu, you can create a virtual environment using the built-in venv
module. Run the following command to create a virtual environment:
python -m venv .venv
After creation, activate the virtual environment with the following command:
source .venv/bin/activate
To deactivate the virtual environment, use:
deactivate
By using virtual environments, you can manage dependencies for each project independently.
6. Troubleshooting pyenv
When using pyenv, you might encounter issues such as installation failures or errors related to the PATH configuration. Below are common problems and their solutions.
Common Errors and Fixes
- Installation Failure: If a Python version fails to install, it may be due to missing dependencies. Use
sudo apt install
to install the required packages. - Incorrect PATH Configuration: If Python versions do not switch properly, check that your
~/.bashrc
contains the correct PATH settings. Apply changes by runningsource ~/.bashrc
.
7. Advanced Configuration and Usage
Once you are comfortable with the basic usage of pyenv, you can explore advanced settings and usage techniques. This section covers using plugins, integrating with other package managers, and optimizing your development workflow.
Using pyenv-virtualenv
pyenv supports plugins, and one of the most useful is pyenv-virtualenv. This tool enhances virtual environment management, allowing you to handle multiple environments efficiently. With pyenv virtualenv
, you can easily create and switch between different virtual environments.
How to Install pyenv-virtualenv:
- First, ensure that pyenv is installed on your system.
- Next, install
pyenv-virtualenv
using the following command:
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
- Create a virtual environment and activate it using the following commands:
pyenv virtualenv 3.10.8 myenv
pyenv activate myenv
- To deactivate the virtual environment, use
pyenv deactivate
.
Integrating with Other Package Managers
When developing on Ubuntu, you might use pyenv
alongside other package managers such as Homebrew or Miniconda. These tools can help install additional libraries and frameworks.
- Using Homebrew: While Homebrew is more common on macOS, it is also available for Ubuntu. To install pyenv via Homebrew, use:
brew install pyenv
- Using Miniconda: Miniconda is a lightweight Python package manager. You can install it via
pyenv install
to manage different Python versions and package sets per environment.

8. Conclusion
In this article, we covered how to manage Python environments on Ubuntu using pyenv. We focused on installing multiple Python versions, creating virtual environments, and troubleshooting common issues.
Pythonの複数バージョン管理に困っていますか?この記事では、pyenvを使ったPythonのバージョン管理やvirt…