One of the hardest processes to learning Python for beginners is installing Python. Sometimes it was a version problem, the machine doesn’t recognize commands, etc.
I was included with people who deal with those problems. My solution at that time was just to install the Anaconda package. But since the Anaconda package size is too big for what I need, I feel that method is too overkill for me.
After searching for a while, I got the solution to this problem, and the solution 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.
If Homebrew hasn’t been installed on your computer, run this command in the terminal.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
It will take a while. After the installation process is done, it will show you a message like this.
For the next step, we will need to copy the text highlighted above to the terminal.
echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /Users/fahminurfikri/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/fahminurfikri/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
When the process is finished, restart the terminal, and Homebrew is already installed on your computer.
Once Homebrew is installed, we can use it to install Pyenv. This can be done by running the command below:
brew install pyenv
Once the installation progress is finished, you can check it by using the command pyenv versions on your terminal. The output would be like this.
If the output doesn’t show an error, then we can continue to the Python 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:
And the result will look like this.
* system (set by /home/user/.pyenv/version)
* 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>.