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
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
How to install python 3.8.12 on /usr/bin/python? - Stack Overflow
How can I install Python 3.8 on Ubuntu 16.04? (ppa:deadsnakes doesn't support Ubuntu 16.04 anymore)
How do I install pip for python 3.8 on Ubuntu without changing any defaults? - Stack Overflow
How to Install Python 3.8 on LinuxMint
Videos
This post is community wiki so as to not claim the credit of @Kulfy's comment. This procedure worked on Ubuntu 18.04.
DON'T EVER CHANGE DEFAULT PYTHON!!! It may cause your system to break and some applications won't even run. It's far far far better to invoke python3.8 using
python3.8command
When installing python3.8, do the following
$ sudo apt-get install python3.8 python3.8-dev python3.8-distutils python3.8-venv
For most people this will be acceptable as they will be using a virtual environment for development. Construct a virtual environment and activate it as you usually would. This will leave you in a terminal where python resolves to python3.8:
$ python3.8 -m venv dev3.8/
$ source dev3.8/bin/activate
(dev3.8) $ which python
...dev3.8/bin/python
(dev3.8) $ python --version
Python 3.8.0
Neglecting to install python3.8-venv will result in an unhelpful error, that suggests you should install python-venv which resolves to python3.6-venv:
$ python3.8 -m venv dev3.8/
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ... (trimmed for formatting)
Step 1: Install the latest version of python. Currently, 3.8 is the latest
sudo apt install python3.8
Step 2: Add Python 3.6 & Python 3.8 to update-alternatives
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6.9
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8.1
Step 3: Update Python 3 to point to Python 3.7
By default, Python 3.6 is pointed to Python 3. So, we run python3 it will execute as python3.6 but we want to execute this as python3.8
sudo update-alternatives --config python3
You should get a similar output. Now type 2 and hit enter for Python 3.
Step 4: Test the version of python
Finally test the current version of python by typing
python3 -V
That article is outdated, most likely written at a time where Python 3.8 was not yet available on the stable Ubuntu repositories.
You can install the latest Python available with just apt install python3 (3.9 at the moment).
If you really want 3.8, I suggest you take a look at the deadsnakes PPA. It contains an archive of all Python packages for Ubuntu, and also newer versions that haven't hit Ubuntu stable yet (like 3.10).
You can add this PPA and install Python 3.8 with just three commands:
Copyadd-apt-repository ppa:deadsnakes/ppa
apt update
apt install python3.8
Regarding your other two bullet points:
- Yesn't. The thing is that, by default,
/usr/binis included in yourPATHenvironment variable, which what enables you to just typepython ...instead of providing the absolute path for the binary. It is also expected by some scripts, I suppose. - You could, and you could also symlink it. The reason why it wasn't put there already is because the article used
altinstall, which prevents exactly that. It is way more preferable that you install stuff through your package manager, however.
You can do it using snap:
Copysnap install python38
I used to use the following gist to install Python 3.8 on Ubuntu 16.04:
# install PPA sudo add-apt-repository ppa:deadsnakes/ppa # update and install sudo apt update sudo apt install python3.8 python3.8-dev python3.8-venv
However, ppa:deadsnakes doesn't support Ubuntu 16.04 anymore, and the above script doesn't work:
> [6/6] RUN apt install -y python3.8: #9 0.222 #9 0.222 WARNING: apt does not have a stable CLI interface. Use with caution in scripts. #9 0.222 #9 0.224 Reading package lists... #9 0.960 Building dependency tree... #9 1.098 Reading state information... #9 1.148 E: Unable to locate package python3.8 #9 1.148 E: Couldn't find any package by glob 'python3.8' #9 1.148 E: Couldn't find any package by regex 'python3.8'
How can I install Python 3.8 on Ubuntu 16.04?
While we can use pip directly as a Python module (the recommended way):
python -m pip --version
This is how I installed it (so it can be called directly):
Firstly, make sure that command pip is available and it isn't being used by pip for Python 2.7
sudo apt remove python-pip
Now if you write pip in the Terminal, you'll get that nothing is installed there:
pip --version
Output:
Command 'pip' not found, but can be installed with:
sudo apt install python-pip
Install python3.8 and setup up correct version on python command using update-alternatives (as done in the question).
Make sure, you have python3-pip installed:
(This won't work without python3-pip. Although this will install pip 9.0.1 from python 3.6, we'll need it.)
sudo apt install python3-pip
This will install pip 9.0.1 as pip3:
pip3 --version
Output:
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
Now, to install pip for Python 3.8, I used pip by calling it as a python module (ironic!):
python -m pip install pip
Output:
Collecting pip
Downloading https://files.pythonhosted.org/packages/36/74/38c2410d688ac7b48afa07d413674afc1f903c1c1f854de51dc8eb2367a5/pip-20.2-py2.py3-none-any.whl (1.5MB)
100% |ββββββββββββββββββββββββββββββββ| 1.5MB 288kB/s
Installing collected packages: pip
Successfully installed pip-20.2
It looks like, when I called pip (which was installed for Python 3.6, BTW) as a module of Python 3.8, and installed pip, it actually worked.
Now, make sure your ~/.local/bin directory is set in PATH environment variable:
Open ~/.bashrc using your favourite editor (if you're using zsh, replace .bashrc with .zshrc)
nano ~/.bashrc
And paste the following at the end of the file
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi
Finally, source your .bashrc (or restart the Terminal window):
source ~/.bashrc
Now if you try running pip directly it'll give you the correct version:
pip --version
Output:
pip 20.2 from /home/qumber/.local/lib/python3.8/site-packages/pip (python 3.8)
Sweet!
As suggested in official documentation you can try with get-pip.py.
wget https://bootstrap.pypa.io/get-pip.py
python3.8 get-pip.py
This will install pip as pip3.8.
Make sure you have python3.8-distutils installed to avoid below error
"No module named 'distutils.cmd'" error.
You can install distutils using:
sudo apt install python3.8-distutils