Fahmi Nurfikri
Posted on September 30, 2023
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 have 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 ~/.pyenv
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)"' >> ~/.bashrc
Finally, 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 *
means that the 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 -V
If 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 use pyenv install <version>
and if you want to change the Python version, you can just use pyenv global <version>
.
Posted on September 30, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.