How to Install Python using Pyenv on Ubuntu 20.04
Easy way to install and switch python version
When it comes to python, did you ever find a problem installing or changing the python version? If yes, then we’re the same. I always got a problem when it comes to changing the python version and my only solution at that time is just uninstalling python and re-installing the version that I need.
After dealing with that problem for a long time, I finally found the tools to manage the python version, named Pyenv.
What is Pyenv?
Basically, Pyenv is a tool to simplify installation and version changing in python. It helps developers quickly install or change the python version without needing to change the whole system.
In this post, I will show you how to install pyenv and manage the python version.
Install Pyenv
To start the installation process, it’s a good idea to update the system packages. Open the terminal and write the command below.
apt update -y
After that, let’s install all Pyenv dependencies.
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
After that, we will install the latest version on Pyenv. So, we will need to clone from the Pyenv repository.
git clone https://github.com/pyenv/pyenv.git ~/.pyenvAnd for the last step, we must add pyenv to our path so that the pyenv command is recognized globally.
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrcFinally, to start using pyenv, restart the shell by running:
exec "$SHELL"Install Python
To check whether pyenv is already installed on our machine, we can use this command.
pyenv --version
In mine, the response is pyenv 2.3.6–13-g4c261e6e, if you do not see a result similar to this, then maybe there is an error in your installation.
To see the available python version, you can use this command.
pyenv install --list
For example, I will install python 3.9.15. So the command will be like this.
pyenv install 3.9.15
Don’t worry if your terminal doesn’t return anything, the process takes a while. Maybe you can leave it for a while to make coffee or something.
If the installation is already done, you can verify if the python version is already installed, you can use the:
pyenv versions
And the result will look like this.
* system (set by /home/user/.pyenv/version)
3.9.15 The * is mean that system is the default python version. To set python 3.9.15 as the default python, use the command:
pyenv global 3.9.15
After that, check the python version.
python --version # or python -VIf the python version is 3.9.15 or whatever python version you install, then the python installation is a success. If you want to add another python version, you can just be using pyenv install <version> and if you want to change the python version, you can just use pyenv global <version>.
