If you’re wondering how to update python in linux, you’ve come to the right place. Python on Linux updates by adding the deadsnakes PPA or compiling from source, and this guide walks you through both methods step by step. Whether you’re a beginner or a seasoned developer, keeping Python up-to-date ensures you have the latest features and security patches.
Python is a core component of many Linux distributions, but the version that comes pre-installed is often outdated. Updating Python manually gives you control over which version you use, and it’s easier than you might think. Let’s dive in.
Why Update Python On Linux
Outdated Python versions can cause compatibility issues with modern libraries and frameworks. Security vulnerabilities are also patched in newer releases, so updating is a good practice. Plus, you might need a specific version for a project that requires the latest syntax or features.
Many Linux distros like Ubuntu and Debian rely on Python for system tools, so you should never replace the system Python. Instead, install a separate version alongside it. This keeps your OS stable while giving you access to newer Python releases.
How To Update Python In Linux
There are three main ways to update Python on Linux: using the deadsnakes PPA, compiling from source, or using a version manager like pyenv. We’ll cover all three, so you can choose what works best for you.
Method 1: Using The Deadsnakes PPA (Ubuntu/Debian)
The deadsnakes PPA is a popular repository that provides newer Python versions for Ubuntu and Debian-based systems. It’s maintained by the community and is generally safe to use.
- First, update your package list:
sudo apt update - Install the software-properties-common package if you haven’t already:
sudo apt install software-properties-common - Add the deadsnakes PPA:
sudo add-apt-repository ppa:deadsnakes/ppa - Press Enter when prompted to confirm.
- Update your package list again:
sudo apt update - Install the Python version you want, for example Python 3.11:
sudo apt install python3.11 - Verify the installation:
python3.11 --version
You can now use python3.11 to run scripts with the new version. The system Python remains unchanged. If you need to set this as your default Python3, you can use update-alternatives, but be careful not to break system tools.
Setting Python3.11 As Default (Optional)
To make Python3.11 the default when you type python3, use the update-alternatives command:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
sudo update-alternatives --config python3
Select the number corresponding to Python3.11. This is optional and not recommended for production servers, as it might affect system scripts.
Method 2: Compiling Python From Source
Compiling from source gives you the most control and works on any Linux distribution. It’s also the only option if you need a version not available in any repository. The process takes a bit longer but is straightforward.
- First, install build dependencies:
sudo apt update && sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget - Download the Python source code from the official website. For example, Python 3.12.0:
wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz - Extract the tarball:
tar -xf Python-3.12.0.tgz - Navigate into the directory:
cd Python-3.12.0 - Configure the build with optimizations:
./configure --enable-optimizations - Compile using make. This step takes a while:
make -j $(nproc) - Install the compiled Python:
sudo make altinstall - Verify the installation:
python3.12 --version
Using altinstall instead of install prevents overwriting the system Python. The new version will be available as python3.12. You can also create a symlink if needed.
Common Compilation Issues
If you encounter errors during compilation, double-check that all dependencies are installed. Missing libraries like libssl or libffi can cause failures. Use sudo apt build-dep python3 to install most required packages automatically.
Method 3: Using Pyenv (Version Manager)
Pyenv is a tool that lets you install and switch between multiple Python versions easily. It’s perfect for developers who work on different projects with different Python requirements.
- Install pyenv dependencies:
sudo apt update && sudo apt install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev - Install pyenv using the installer script:
curl https://pyenv.run | bash - Add pyenv to your shell configuration. For bash, add these lines to ~/.bashrc:
export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" - Reload your shell:
source ~/.bashrc - List available Python versions:
pyenv install --list - Install a specific version, e.g., Python 3.11.5:
pyenv install 3.11.5 - Set the global Python version:
pyenv global 3.11.5 - Verify:
python --version
Pyenv manages versions per user, so it doesn’t affect system Python. You can also set local versions per project directory using pyenv local 3.11.5.
Checking Your Current Python Version
Before updating, you should know what you’re currently running. Use these commands:
python --version(might show Python 2.x on some systems)python3 --version(shows default Python 3 version)which python3(shows the path to the executable)
If you have multiple versions installed, you can list them with ls /usr/bin/python* or pyenv versions if using pyenv.
Best Practices For Python Updates
Always test your code after updating Python. Some libraries might not be compatible with the new version yet. Use virtual environments to isolate project dependencies.
- Never replace the system Python (usually located at /usr/bin/python3).
- Use
pipto install packages for your new Python version. For example,python3.11 -m pip install requests. - Keep a backup of your important scripts before major updates.
- Consider using Docker containers for reproducible environments.
Troubleshooting Common Issues
Even with careful steps, you might run into problems. Here are some common ones and how to fix them.
Command Not Found After Installation
If you installed Python but can’t run it, check that the installation directory is in your PATH. For source installations, the default path is /usr/local/bin. For deadsnakes, it’s /usr/bin. Use echo $PATH to see your current path.
SSL Module Not Found
When compiling from source, you might get an SSL error. This usually means the OpenSSL development headers are missing. Install them with sudo apt install libssl-dev and recompile.
Pip Not Working
If pip isn’t available after installation, install it manually: python3.11 -m ensurepip --upgrade. Or use sudo apt install python3-pip for system-wide pip.
Frequently Asked Questions
What is the safest way to update Python on Linux?
The safest method is using a version manager like pyenv or installing a separate version via deadsnakes PPA. This avoids interfering with the system Python that your OS relies on.
Can I update Python without sudo?
Yes, using pyenv or compiling from source in your home directory allows you to install Python without root privileges. Just make sure you have the necessary build dependencies installed globally.
Will updating Python break my system?
If you replace the system Python, yes. But if you install a separate version and don’t change the default symlinks, your system will remain stable. Always use altinstall or a version manager.
How do I switch between Python versions?
Use pyenv to switch globally or per project. Alternatively, you can manually call the specific Python binary, like python3.11 or python3.12.
What is the latest Python version for Linux?
As of early 2025, Python 3.13 is the latest stable release. Check the official Python website for the most current version. Always use a stable release, not alpha or beta versions, for production.
Final Thoughts On Updating Python
Updating Python on Linux is a skill every developer should have. Whether you choose the deadsnakes PPA for simplicity, compiling from source for control, or pyenv for flexibility, you now have the knowledge to do it safely. Remember to test your environment after each update and keep your tools current.
If you run into issues, the Python community is huge and helpful. Forums like Stack Overflow and the Python mailing list are great resources. Don’t be afraid to ask questions—everyone starts somewhere.
Now go ahead and update your Python version. Your future self will thank you when you’re running the latest libraries without compatibility headaches.