How to update Python version in Terminal?

Learn how to update python version in terminal? with practical examples, diagrams, and best practices. Covers python, python-3.x development techniques with visual explanations.

How to Update Python Version in Terminal

Hero image for How to update Python version in Terminal?

Learn the essential steps to update your Python installation using the terminal on various operating systems, ensuring you have the latest features and security patches.

Keeping your Python installation up-to-date is crucial for accessing new language features, performance improvements, and critical security fixes. This guide will walk you through the process of updating Python using your terminal, covering common scenarios for different operating systems. We'll focus on best practices to avoid conflicts and ensure a smooth transition to a newer version.

Understanding Python Versions and Installation Methods

Before diving into the update process, it's important to understand how Python is typically installed and managed on your system. Many operating systems come with a pre-installed version of Python (often Python 2.x or an older Python 3.x), which is sometimes used by system utilities. Directly modifying this system Python can lead to unexpected issues. Therefore, it's generally recommended to use version managers or separate installations for your development work.

flowchart TD
    A[Start] --> B{Check Current Python Version}
    B --> C{Identify OS}
    C --> D{macOS/Linux}
    C --> E{Windows}
    D --> F{Use pyenv/asdf or Homebrew}
    E --> G{Use pyenv-win or Official Installer}
    F --> H{Install New Python Version}
    G --> H
    H --> I{Set New Version as Default}
    I --> J{Verify Installation}
    J --> K[End]

General workflow for updating Python in the terminal.

Updating Python on macOS and Linux

On Unix-like systems, you have several robust options for managing Python versions. Using a version manager like pyenv or asdf is highly recommended as it allows you to install multiple Python versions side-by-side and switch between them easily without affecting the system's default Python. Homebrew is another popular package manager for macOS that can also be used.

1. Step 1: Install a Python Version Manager (e.g., pyenv)

If you don't already have one, install pyenv. This allows you to manage multiple Python versions. For macOS, you can use Homebrew:

brew update
brew install pyenv

For Linux, follow the instructions on the pyenv GitHub page or use your distribution's package manager if available.

2. Step 2: Configure your Shell for pyenv

After installing pyenv, you need to add it to your shell's configuration file (e.g., ~/.bashrc, ~/.zshrc, or ~/.profile). Add the following lines:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

Then, reload your shell configuration:

source ~/.bashrc

(Replace ~/.bashrc with your shell's config file if different).

3. Step 3: Install the Desired Python Version

Use pyenv to install the specific Python version you want. For example, to install Python 3.10.12:

pyenv install 3.10.12

You can list available versions with pyenv install --list.

4. Step 4: Set the New Python Version

You can set the new version globally, locally for a project, or for your current shell session:

  • Globally: pyenv global 3.10.12
  • Locally (in a project directory): pyenv local 3.10.12
  • For current shell: pyenv shell 3.10.12

5. Step 5: Verify the Update

Check your Python version to confirm the update:

python --version
python3 --version

Also, verify pip:

pip --version

Updating Python on Windows

On Windows, the process is slightly different. While you can use the official installer, pyenv-win provides a similar version management experience to pyenv on Unix-like systems, making it a powerful tool for developers.

1. Step 1: Install pyenv-win

The easiest way to install pyenv-win is via PowerShell. Open PowerShell as Administrator and run:

Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"

Alternatively, you can install it via pip:

pip install pyenv-win --target %USERPROFILE%\.pyenv

Ensure that PYENV_HOME and PYENV_ROOT environment variables are set correctly, and that %PYENV_ROOT%\bin and %PYENV_ROOT%\shims are in your system's PATH.

2. Step 2: Install the Desired Python Version

Once pyenv-win is installed and configured, you can install Python versions from the terminal:

pyenv install 3.10.12

To see available versions:

pyenv install --list

3. Step 3: Set the New Python Version

Similar to pyenv, you can set the version globally or locally:

  • Globally: pyenv global 3.10.12
  • Locally (in a project directory): pyenv local 3.10.12

4. Step 4: Verify the Update

Open a new command prompt or PowerShell window and check the Python version:

python --version
python3 --version

And pip:

pip --version
# Update pip in your new Python environment
python -m pip install --upgrade pip

# Create a new virtual environment
python -m venv my_project_env
source my_project_env/bin/activate # On Windows: .\my_project_env\Scripts\activate

# Install dependencies in the new environment
pip install -r requirements.txt

Post-update steps: updating pip and creating a virtual environment.