Learning Center
Python

How to Update Python

29 Jan 2025
Awais Khan
Awais Khan

As software evolves, so does the need to keep your programming environment up-to-date. Python, known for its versatility and widespread application, frequently sees new version releases. These updates frequently bring new features, performance enhancements, and crucial security patches for developers and organizations that depend on Python. Ensuring that Python is up-to-date guarantees enhanced performance and fortified security.

We'll explore different methods for updating Python, suited to your needs.

Prerequisites
Copy link

Before starting, ensure you have:

  • Administrative access to your cloud server.
  • Reliable internet access.

Updating Python
Copy link

Several methods are available to update Python on a cloud server. Here are four effective methods to achieve this.

Method 1: Via Package Manager
Copy link

Employing a package manager makes updating Python a quick and effortless task. This approach is simple and quick, especially for users who are familiar with package management systems.

Step 1: Find the Current Python Version

Begin by validating the Python version on your server via:

python --version

or for Python 3:

python3 --version

Image3

Step 2: Update Package Repository

Make sure your package repository is updated to receive the latest version data by applying:

sudo apt update

Step 3: Upgrade Python

Then, proceed to use your package manager to install the current version of Python:

sudo apt install --upgrade python3

Image4

This will bring your Python installation up to the latest version provided by your package repository.

Method 2: Building Python from Source
Copy link

Compiling Python from the source provides the ability to customize the build process and apply specific optimizations. This method is especially useful for developers who need a customized Python build tailored to their requirements. Check out these instructions:

Step 1: Install Dependencies

Get the essential dependencies from the default package manager for building process:

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev pkg-config libffi-dev wget

Image7

Step 2: Download Python Source Code

Then, get the updated Python source code by visiting the official website

Image6

You could also opt to download it directly using wget:

wget https://www.python.org/ftp/python/3.13.1/Python-3.13.1.tgz

Substitute 3.13.1 with your preferred Python version number.

Image9

Step 3: Extract the Package

Once downloaded, simply extract the tarball via:

tar -xf Python-<latest-version>.tgz

Image8

Step 4: Set Up and Compile Python

Enter the extracted folder and configure the installation using these commands:

cd Python-<latest-version>
./configure --enable-optimizations

Image11

Once done, compile Python via make command given below:

make -j $(nproc)

Image10

Note: The above command utilizes all available CPU cores to speed up the build process. On a machine with limited resources, such as CPU and 1GB RAM, limit the number of parallel jobs to reduce memory usage. For example, apply:

make -j1

Step 5: Install Python

Following compilation, go ahead and install Python through:

sudo make install

Image14

Note: The make altinstall command can be applied too instead of make install. Implementing this will prevent any interruptions to your system tools and applications that require the default Python version. However, extra steps are needed:

  • Verify the installed location via:

ls /usr/local/bin/python3.13
  • Apply update-alternatives system for managing and switching multiple Python versions:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.13 1
sudo update-alternatives --config python3

Step 6: Validate the Python Installation

Close the terminal and open again. Then check the newly installed version via:

python3 --version

Image12

Method 3: Via Pyenv 
Copy link

Pyenv is a go-to solution for maintaining different Python versions on the same system. It offers a versatile method for installing and switching between various Python versions. To update Python through Pyenv, use the following instructions.

Step 1: Install Dependencies

First, set up the dependencies needed for compiling Python:

sudo 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 git

Image13

Step 2: Install Pyenv

Following that, utilize the curl command to download and install Pyenv:

curl https://pyenv.run | bash

Image15

Step 3: Update Shell Configuration

After that, reload the shell configuration:

export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"

Step 4: Install Recent Python 

Once completion is completed, display all available Python versions with:

pyenv install --list

Image16

Then proceed to install the version you want via:

pyenv install <latest-version>  

Image17

Configure the newly installed version as the system-wide default through:

pyenv global <latest-version>

Image18

Step 5: Verify the Installation

Confirm the new Python version by applying:

python --version

Image19

Method 4: Via Anaconda 
Copy link

Anaconda supplies a full-featured distribution of Python and R, specifically aimed at data science and computational applications. It simplifies package handling and implementation, providing an accessible and efficient framework for developers. Here’s are the steps:

Step 1: Fetch Anaconda Installer

Fetch the Anaconda installer script directly from the official site:

wget https://repo.anaconda.com/archive/Anaconda3-<latest-version>-Linux-x86_64.sh

Replace <latest-version> with the desired version number.

For example:

wget https://repo.anaconda.com/archive/Anaconda3-2024.06-1-Linux-x86_64.sh

Image20

Step 2: Run the Installer

Run the installer script through bash:

bash Anaconda3-<latest-version>-Linux-x86_64.sh

Image21

Adhere to the prompts to finalize the installation.

Step 3: Initialize Anaconda

Set up Anaconda by incorporating it into your shell configuration using:

source ~/.bashrc

Step 4: Update Anaconda

Ensure Anaconda is updated by applying:

conda update conda

Image22

Confirm the Python installation through:

conda install python=<version>

Image1

Step 5: Verify the Installation

Identify the Python version being utilized in your Anaconda configuration. Apply:

python --version

Image2

Additional Tips for Maintaining Your Python Environment
Copy link

Listed below are some key practices to ensure your Python environment runs smoothly and efficiently:

  • Regular Updates and Maintenance

For maintaining optimal performance and security, it is important to keep your Python environment updated frequently. It's recommended to check for updates periodically and apply them as needed. 

  • Using Virtual Environments

It's a good idea to use virtual environments when working with Python. They let you set up separate environments for each project, so dependencies and versions stay separate. Tools like venv and virtualenv can help manage these environments efficiently.

  • Backup and Version Control

It's always a good idea to maintain backups of your important projects and configurations. Git helps you record changes, work with teammates, and switch back to older versions when needed.

Troubleshooting Common Issues
Copy link

Listed here are frequent problems you may face and ways to solve them:

  • Dependency Conflicts

Sometimes, upgrading Python or installing new packages can lead to dependency conflicts. To resolve these conflicts, consider using tools like pipenv or poetry that manage dependencies and virtual environments.

  • Path Issues

After upgrading Python, you might encounter issues with the PATH environment variable. Ensure that your system recognizes the correct Python version by updating the PATH variable in your shell configuration file (e.g., .bashrc, .zshrc).

Security Considerations
Copy link

Ensuring the protection of your Python environment is essential. Follow these recommendations to maintain a secure environment:

  • Stick to trusted sources when downloading packages. Use PIP's hash-checking mode to confirm package integrity.
  • Always review the code and documentation before incorporating a new package.
  • Stay informed with security updates and advisories from the Python ecosystem and package maintainers.
  • Keep PIP and your packages updated regularly to ensure protection with the newest security fixes and improvements.

FAQs
Copy link

Q1: What's the recommended approach to updating Python on a cloud server?
Copy link

A: The best method depends on your requirements. For a straightforward update, using a package manager is ideal. For customization, building from source is recommended. Pyenv is great for managing multiple versions, while Anaconda is tailored for data science needs.

Q2: How frequently should I update my Python environment?
Copy link

A: Periodically review for updates and implement them to ensure top performance and robust security.

Q3: What should I do if I encounter issues after updating Python?
Copy link

A: Refer to the troubleshooting section for common issues. Check the PATH variable for accuracy, and use virtual environments to solve any dependency conflicts.

Conclusion
Copy link

Updating Python on a cloud server can be accomplished through various methods depending on your preferences and requirements. Whether using a package manager, compiling from source, managing versions with Pyenv, or leveraging Anaconda, each approach has its benefits. By following this comprehensive guide, you can ensure your Python environment remains current, secure, and equipped with the latest features. Regularly updating Python is essential to leverage new functionalities and maintain the security of your applications.