I'm using WSL how I upgrade Python to the last version through the console?
Asked Answered
M

4

17

I'm using WSL how I upgrade Python to the last version through the console?

Right now I have 3.8.10

Munich answered 29/10, 2021 at 21:25 Comment(3)
You don't "upgrade" python, you just get a new interpreterDorfman
The distribution you are using offers python 3.8. If you want other versions there is always anaconda or you can download it from python's site.Driven
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.Mcnully
V
42

I tried these steps and it worked.

Note: in step 3 you just need to change the version that you want to install

Installation steps

Run the following commands in your WSL terminal:

# Update package lists{
sudo apt update

# Install dependent libraries:
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev

# Download Python binary package:
wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tgz

# Unzip the package:
tar -xzf Python-3.10.8.tgz

# Execute configure script
cd Python-3.10.8
./configure --enable-optimizations

# Build Python 3.10
make -j 2

# Install Python 3.10
sudo make install

# Verify the installation
python3.10

You should see:

Python 3.10.8 (default, october 15 2022, 14:44:10)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

If you want to make Python 3.10 the default that runs when you type python, you can follow these steps:

  1. Change ~/.bashrc file to add the following line:

    alias python='/usr/local/bin/python3.10'
    
  2. And then run the following command to have it take effect in the current shell:

    source ~/.bashrc
    
  3. You can verify it using the python --version command, which should now show:

    Python 3.10.8
    

Originally found in this article.

Visionary answered 14/10, 2022 at 16:32 Comment(2)
Thanks for fixing this! Upvoted - It's not my favorite solution, but it's a solution ;-) Note that you have some formatting issues - I'll fix those for you, but keep that in mind as you post future questions and answers. Thanks!Vernverna
I would like to avoid building if possible. However, that's the official way as per the link: spencerharston.com/posts/…. Therefore, the suggested approach is the safest possible way to upgrade.Teevens
E
3

I'm not sure if others are overthinking it, things have changed in WSL, or my distro (Debian) is well behaved?

I simply ran this, and Python3 went from 3.9 to 3.11:

sudo apt update
sudo apt upgrade

As others have said, due to the way Python changes, this may loose some backward compatibility, but if you're not worried about that this should be fine.

What may have also helped this run smoothly, is I have all of these repos defined:

deb http://deb.debian.org/debian bullseye main
deb http://deb.debian.org/debian bullseye-updates main
deb http://security.debian.org/debian-security/ bullseye-security main
deb http://ftp.debian.org/debian bullseye-backports main
deb http://ftp.debian.org/debian stable main contrib non-free
Equestrian answered 4/7, 2023 at 15:59 Comment(0)
V
2

A couple of notes -- First, to quote @juanpa.arrivillaga's comment:

You don't "upgrade" python, you just get a new interpreter

The word "upgrade", of course, means to replace one version with a later/greater version. You don't want to do this. So, so, so many packages in most Linux distributions rely on Python, and these distributions are usually heavily tested against a particular Python release. You don't mention which Linux distribution you are using in WSL, but since Ubuntu is the default, we'll assume that. To see a list of all packages that are currently installed in Ubuntu that depend on Python, run:

apt-cache rdepends python3 | grep -v python | less

From this Ask Ubuntu answer

What you really want to do, most likely, is have access to a second (or third) Python version for a specific use. There are multiple ways to do this. I would suggest reading the answers on How do I install a different Python version using apt-get? on Ask Ubuntu. There are some good methods there as well. My solution is to use a Docker Python image -- There are images available for every currently support Python release (and some unsupported Python2 releases, even) there is.

Vernverna answered 14/10, 2022 at 20:40 Comment(0)
B
0

Thanks for the excellent instructions!

FYI: I had to manually add pip soft links to /usr/local/bin also. I was getting file not found when trying to install pytorch (on python3.10 for my case). enter image description here

Beattie answered 4/11, 2022 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.