ericsysmin's DevOps Blog

How to Install Pyenv on MacOS

Steps to install Pyenv on MacOS

There are a few ways on MacOS to install Python. You can install it via Brew, or by using Pyenv. After needing to switch between different versions of Python often I’ve decided to move to Pyenv. Prior to these steps I removed all versions of Python installed directly with Brew.

1. Update Brew and install prerequisites
We will need to update brew.

In some cases when installing Python >=3.12.1 we will need ncurses. If it’s missing you can install using:

2. Install Pyenv using brew

The recommended way to install pyenv on MacOS is brew.

3. Brew doctor fix

If you want to avoid brew doctor warning about “config” scripts existing outside the system or Homebrew directories please include the following in your shell.

4. Configure your Zsh profile.

If you wish to use Pyenv in non-interactive shells, add the following:

5. Restart shell

6. Install python 3.12

I am going to show how to install python 3.12 but you can select any version of your choice.

7. Switch between your python versions

pyenv shell <version> – modifies python for the current shell session

pyenv local <version> – modifies the python used in the current directory (or subdirectories)

pyenv global <version> – modifies the python used for your user account

 

Continue reading...

How to Install Pyenv on Ubuntu 22.04

Due to the slowness of repositories or even lack thereof being updated with specific versions of Python, I’ve decided to move some of my environments over to Pyenv to allow me to dynamically install and configure Python specifically for my environment. As it turns out this will also allow VS Code to allow me to choose the version of Python that I’d like to use when testing. So, here’s a quick guide to installing Pyenv on Ubuntu 22.04

Steps to install Pyenv on Ubuntu 22.04

1. Update and Install Dependencies

We need to ensure our package cache is updated, and then install the dependencies to download, and build Python from Pyenv.

2. Install Pyenv using pyenv-installer

3. Configure user profile to use pyenv

Ensure the following is in your ~/.bash_profile (if exists), ~/.profile (for login shells), ~/.bashrc (for interactive shells), or ~/.zshrc

Optionally enable pyenv-virtualenv

4. Reload your profile

5. Install python using pyenv

6. Set your python version

pyenv shell <version>  — select just for current shell session
pyenv local <version>  — automatically select whenever you are in the current directory (or its subdirectories)
pyenv global <version>  — select globally for your user account

7. Validate your installation of python

or

 

Continue reading...