Python is a popular and versatile programming language used in a wide variety of applications, from web development to data analysis and task automation.
In Ubuntu, you can install various versions of Python to meet the specific needs of your projects. In this article, we'll walk you through the process of installing different versions of Python on your Ubuntu 22 system, but it's valid for Ubuntu 20 or 24.
System Upgrade
Before installing Python, it is a good practice to make sure that all system packages are up to date. Open a terminal and run the following commands:
# apt update && apt upgrade -y
These commands will update the list of available packages and upgrade the installed packages to the latest versions.
Install Python from the Official Repository
Ubuntu 22 usually comes with Python pre-installed. To check if Python is installed, run the following command in your terminal:
# python3 --version
If Python is installed, you will see the current version. If it is not installed, you can install it from the official Ubuntu repository by running the following command. This command will install the latest stable version of Python 3 available in the Ubuntu repositories:
# apt install python3
Install Python using pyenv
If you need to install specific versions of Python or work with multiple versions of Python on your system, a popular option is to use pyenv. pyenv is a tool that allows you to easily install and manage multiple versions of Python on your system.
First, install the necessary dependencies using the following command:
# apt install git curl make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl
Then, download and install pyenv by executing these commands:
# curl https://pyenv.run | bash
This will download the pyenv installer and run it. Follow the on-screen instructions to complete the installation.
Configure pyenv
Once pyenv is installed, you need to configure your shell profile for pyenv to work properly. Add the following lines to the end of your file with nano or vi in .bashrc o .bash_profile:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
After adding these lines to the .bashrc file, reload your shell profile by running the following command:
Installing Python Specific Versions with pyenv
Now that pyenv is configured, you can install specific versions of Python using pyenv. For example, to install Python 3.9.10, run the following command:
# pyenv install 3.9.10
You can list all the Python versions available for installation by running:
# pyenv install --list
You can list all the Python versions available to install by running:Once you have installed a specific version of Python, you can set it as the global or local version of Python using the pyenv global or pyenv local commands, respectively.
For example, to set Python 3.9.10 as the global version of Python, run:
# pyenv global 3.9.10
If you need to know the pyenv version you can run the following command:
# pyenv --version
Remember, if you have any questions about this or any other issue related to your Clouding servers, do not hesitate to write to support@clouding.io. We are at your side for whatever you need!