Installing python on Linux - help?
How do I install python?
How to instala python on linux mint?
Has anyone installed python-is-python3 on Ubuntu 20?
Videos
I am a long-time user of Python but I have never understood how to install python "properly" - I tend to figure out some way to get it done when I need to, and then forget all about it. But I want to understand it a bit better because it isn't straightforward in my opinion. I am not considering Pyenv or other such "helper" tools/dependencies - I want to learn how to do this the "official" way. I've looked at the official docs but can't see what I've done incorrectly. The main issue is that at some stage I often find myself wanting a newer version of python than what is provided, and then I end up a bit stuck.
I installed Debian and it came with python3 under my /usr/local/bin directory. There is also a Python installation under /usr/bin/python3.11, which I guess is my system python? I believe I was always interacting with my /usr/local/bin python - not the system python, so that's good. (Also I'm always using virtual environments, so let's not discuss their importance please). That was working fine for me, but now I want to upgrade my version of python and I am facing difficulties.
-
What should I do to upgrade python in an "official" way (i.e. not adding dev repositories like deadsnakes, not using helper tools like Pyenv)?
-
What do I need to do with pip? Currently, the pip command actually points to
/usr/bin/python- i have to use pip3* (see below). -
Can I simply delete the old python and pip versions from
/usr/local/binif I wanted to? -
How do I ensure that every time I type
python3in the terminal, it grabs the latest one? Do I just ensure it is higher up in my PATH variable? -
Why is there not one simple way to do this? Obviously everyone has slightly different needs, but I imagine 80% of python users just want to use python and have a reasonable way to upgrade when required without screwing something up in their system.
To explain why I'm asking this now, I installed the python source from the main website yesterday and tried to get it working but something is off. Here's what I did
-
Extracted the Python-3.12.3.tar.xz
-
Moved into the dir and ran
./configure --enable-optimizations --with-ensurepip=install -
Ran
make -
Ran
sudo make install
This worked, but I notice when I run python in the terminal REPL that I can't use the up/down keys to cycle through my command history:
>>> print("hello")
hello
>>> ^[[A*Also, I notice that I now have pip (/usr/bin/python), pip3, and pip3.12 now, and I am confused about that. Should I alias pip with pip3.12 to prevent interacting with the system python? Should I just delete pip3 and make sure pip3 points to pip3.12?
These issues have convinced me that I've now installed python incorrectly somehow. I found some info about readline but that's deprecated, and I am starting to go down the rabbit hole of running random commands to try and fix things, which is probably going to make things worse.
To install Python 3.8 on Ubuntu version 23
Open your terminal and run these commands:
Install build dependencies
sudo apt-get update
sudo apt-get install -y 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 python3-openssl git
Download and extract Python 3.8 source code
mkdir ~/python38
cd ~/python38
wget https://www.python.org/ftp/python/3.8.16/Python-3.8.16.tgz
tar -xf Python-3.8.16.tgz
cd Python-3.8.16
Configure the build
./configure --enable-optimizations
Compile the source code
make -j$(nproc)
Install Python
sudo make install
Verify the installation
python3.8 --version
To create a virtual environment specify the Python version.
Example:
python3.8 -m venv venv
Assuming you need this simply for development reasons, there's no reason to install it, but rather build it only.
Follow the steps from @GODFREY but skip the install altogether - which most probably will screw up your system.
After make -j$(nproc) you get a python binary in the directory which you can test
./python --version
# => Python 3.10.14
Now in your python project you can use virtualenv directly to use the binary
# Assuming the source is in ~/Python-3.10.14
virtualenv -p ~/Python-3.10.14/python .venv
source .venv/bin/activate
python --version
# => Python 3.10.14