Verified guide
This guide was verified by creating a cloud server and running every command in it, from start to finish, exactly as written.
- OS
- Ubuntu 24.04.5 LTS · x86_64
- Installed version
- Python 3.15.0, Python 3.13.16 (소스 빌드), pyenv 2.8.10
- Test server
- DigitalOcean · 2 vCPU / 2 GB RAM / 60 GB SSD (s-2vcpu-2gb, sgp1)
- Last checked
- 2026-10-16
This guide contains affiliate links. If you sign up through them, Codesand may earn a commission. This doesn't affect the content or our assessments.
This guide installs the latest Python on an Ubuntu 24.04 server with pyenv, then covers installing several versions and setting a version per project, creating a venv virtual environment and installing packages with pip, fixing build errors and the error where the pyenv command cannot be found, and removing everything cleanly.
1. Requirements
- One Ubuntu 24.04 LTS server or PC
- A regular user account with
sudoprivileges - An internet connection (files are downloaded from GitHub and python.org)
If you don't have a server, try creating a Droplet on DigitalOcean. pyenv builds Python from source code, so more CPUs make it finish faster. Every step in this guide was verified on a Droplet (2 vCPU, 2 GB memory, 60 GB SSD, Ubuntu 24.04, Singapore) using a regular user account with sudo privileges.
2. Check the state before installing
Ubuntu 24.04 comes with Python 3.12. System programs use this Python, so do not remove it or replace it with another version.
python3 --versionPython 3.12.3There is no python command. Ubuntu provides Python only under the name python3.
python --versionCommand 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3pip3, which installs packages, is not available either, and venv, which creates virtual environments, cannot be used right away.
pip3 --versionCommand 'pip3' not found, but can be installed with:
sudo apt install python3-pippython3 -m venv myenvThe 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 install python3.12-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: /home/devuser/myenv/bin/python3The failed command leaves a partially created myenv folder behind, so remove it. There is no output.
rm -rf myenvPython 3.12 is supported until October 2028, and the latest Python on the day this guide was verified was 3.15. This guide leaves the system Python as it is and uses pyenv to install the latest Python separately in your user folder (~/.pyenv). pyenv downloads the source code from python.org and builds it, and lets you keep several versions installed and switch between them with a single command.
3. Install the packages needed for the build
Because pyenv builds Python itself, it needs a compiler and libraries. First, update the package list. The beginning of the output (the Hit and Get lines) is omitted.
sudo apt updateFetched 12.1 MB in 5s (2361 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
55 packages can be upgraded. Run 'apt list --upgradable' to see them.Install the build packages listed in the pyenv wiki. You can see in step 9 how the build fails when there is no compiler. Only the final summary of the output is shown.
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev curl git libncurses-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev libzstd-dev11 upgraded, 116 newly installed, 0 to remove and 44 not upgraded.
Need to get 103 MB of archives.
After this operation, 352 MB of additional disk space will be used.4. Install pyenv
Download and run the pyenv install script. curl -fsSL downloads without a progress display and shows an error if it fails. | bash passes the downloaded script to bash to run it right away. The lines showing pyenv and its plugins being downloaded from GitHub are omitted.
curl -fsSL https://pyenv.run | bashCloning into '/home/devuser/.pyenv'...
Cloning into '/home/devuser/.pyenv/plugins/pyenv-doctor'...
Cloning into '/home/devuser/.pyenv/plugins/pyenv-update'...
Cloning into '/home/devuser/.pyenv/plugins/pyenv-virtualenv'...
…
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by appending
# the following to
# ~/.bash_profile if it exists, otherwise ~/.profile (for login shells)
# and ~/.bashrc (for interactive shells) :
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
# Restart your shell for the changes to take effect.The final warning means the settings that load pyenv have not been added yet. Append the three lines it shows to the end of ~/.bashrc. Ubuntu also reads ~/.bashrc when you log in, so adding them to this file is enough. None of the three commands produce output.
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc5. Verify the pyenv installation
In a terminal opened before the settings were added, you get a message that pyenv cannot be found.
pyenv --versionCommand 'pyenv' not found, did you mean:
command 'p7env' from deb libnss3-tools (2:3.98-1ubuntu0.2)
Try: sudo apt install <deb name>Do not install the suggested package. Close the terminal and open a new one (for SSH, disconnect and reconnect), or reload ~/.bashrc with the command below. There is no output.
source ~/.bashrcpyenv --versionpyenv 2.8.106. Install the latest Python
If you give only the first two parts of the version, such as 3.15, pyenv installs the latest release in that series. Because it downloads and builds the source code, it takes a few minutes, and after the Installing Python-3.15.0... line nothing is displayed until it finishes.
pyenv install 3.15Downloading Python-3.15.0.tar.xz...
-> https://www.python.org/ftp/python/3.15.0/Python-3.15.0.tar.xz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 34.0M 100 34.0M 0 0 172M 0 --:--:-- --:--:-- --:--:-- 173M
Installing Python-3.15.0...
Installed Python-3.15.0 to /home/devuser/.pyenv/versions/3.15.0Check the installed versions. system is the system Python that comes with Ubuntu, and the one marked with * is the version currently in use.
pyenv versions* system (set by /home/devuser/.pyenv/version)
3.15.0Installing alone does not change anything; the system Python is still used. Set the default version to 3.15 with pyenv global. There is no output.
pyenv global 3.15Now both python and python3 run Python 3.15. pip is installed as well.
python --versionPython 3.15.0python3 --versionPython 3.15.0pip --versionpip 26.2.1 from /home/devuser/.pyenv/versions/3.15.0/lib/python3.15/site-packages/pip (python 3.15)The python that runs is a file in the shims folder created by pyenv. pyenv goes through this file to run the Python version currently selected.
which python/home/devuser/.pyenv/shims/python7. Install another version and set a version per project
Use pyenv install --list to see the versions you can install. The list is long, so only the 3.13 series is selected and the last three lines are shown.
pyenv install --list | grep -E '^ *3\.13\.[0-9]+$' | tail -n 3 3.13.14
3.13.15
3.13.16Install Python 3.13 as well. This is also built from source, so it takes a few minutes.
pyenv install 3.13Downloading Python-3.13.16.tar.xz...
-> https://www.python.org/ftp/python/3.13.16/Python-3.13.16.tar.xz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 22.1M 100 22.1M 0 0 74.9M 0 --:--:-- --:--:-- --:--:-- 74.8M
Installing Python-3.13.16...
Installed Python-3.13.16 to /home/devuser/.pyenv/versions/3.13.16pyenv versions system
3.13.16
* 3.15.0 (set by /home/devuser/.pyenv/version)To use 3.13 only in a specific project folder, run pyenv local in that folder. A .python-version file is created in the folder, and the version written in it is used in that folder and its subfolders.
mkdir old-project && cd old-project
pyenv local 3.13cat .python-version3.13python --versionPython 3.13.16When you leave the folder, the default version 3.15 is used again.
cd ~python --versionPython 3.15.08. Create a venv virtual environment and install packages
Create a virtual environment for each project and install packages inside it. This keeps package versions from mixing between projects. Open a new terminal, create a virtual environment (.venv) in a hello-python folder, and activate it. None of the three commands produce output.
mkdir hello-python && cd hello-python
python -m venv .venv
source .venv/bin/activateWhen the virtual environment is active, (.venv) appears at the start of the prompt. Check the Python version in the virtual environment, then install the requests package, which sends HTTP requests. The download lines from the installation are omitted.
python --versionPython 3.15.0pip install requestsCollecting requests
…
Installing collected packages: urllib3, idna, charset_normalizer, certifi, requests
Successfully installed certifi-2026.7.22 charset_normalizer-3.5.2 idna-3.20 requests-2.34.2 urllib3-2.8.0Create an app.py file that uses requests and run it. The first line creates the file from the text in quotes, so it has no output.
echo "import sys, requests; print('Hello Python', sys.version.split()[0], 'requests', requests.__version__)" > app.py
python app.pyHello Python 3.15.0 requests 2.34.2Turn off the virtual environment with deactivate. If you run the file again in this state, you get an error that requests cannot be found, because requests is installed only inside the virtual environment.
deactivate
python app.pyTraceback (most recent call last):
File "/home/devuser/hello-python/app.py", line 1, in <module>
import sys, requests; print('Hello Python', sys.version.split()[0], 'requests', requests.__version__)
^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'requests'9. Troubleshooting
configure: error: no acceptable C compiler found in $PATH
If you run pyenv install without installing the build packages from step 3, the build fails because there is no compiler (gcc). Run the sudo apt install command from step 3, then run pyenv install again. The failed build is cleaned up automatically, so there is nothing to remove.
pyenv install 3.15Installing Python-3.15.0...
BUILD FAILED (Ubuntu 24.04 using python-build 2.8.10)
Inspect or clean up the working tree at /tmp/python-build.20261016112128.3221
Results logged to /tmp/python-build.20261016112128.3221.log
…
configure: error: in '/tmp/python-build.20261016112128.3221/Python-3.15.0':
configure: error: no acceptable C compiler found in $PATH
…Command 'pyenv' not found, did you mean:
This means the terminal was opened before the three lines were added to ~/.bashrc in step 4. Close the terminal and open a new one, or run source ~/.bashrc. Do not install the package suggested with it. If a new terminal shows the same message, check that the three lines are there with tail -n 3 ~/.bashrc.
sudo: python: command not found
sudo replaces the command search path (PATH) with the system default path, so it cannot find the pyenv Python in your user folder. Run the Python installed with pyenv without sudo.
sudo python --versionsudo: python: command not foundModuleNotFoundError: No module named 'requests'
This means you ran code that uses a package installed in the virtual environment from outside that environment. Activate the virtual environment with source .venv/bin/activate in the project folder, then run it.
Running pyenv uninstall 3.13 does not remove the version
Unlike pyenv install, pyenv uninstall finishes without any message and does not remove anything when you give only the first two parts of the version. Use the full version number shown by pyenv versions (3.13.16).
pyenv uninstall -f 3.13pyenv versions system
3.13.16
* 3.15.0 (set by /home/devuser/.pyenv/version)The virtual environment was not created successfully because ensurepip is not available.
This message appears when you run python3 -m venv with the system Python (3.12). The Ubuntu system Python is missing the package needed to create virtual environments. The Python installed with pyenv includes venv, so set the version with pyenv global and then create the environment with python -m venv.
error: externally-managed-environment
This error appears on Ubuntu 24.04 when you install pip with sudo apt install python3-pip and then run pip3 install. Installing packages directly into the system Python can conflict with system packages installed by apt, so Ubuntu blocks it. Do not install into the system Python; create a virtual environment as in step 8 and install packages there.
pip3 install requestserror: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
…10. Uninstall
Remove a single Python version
To remove just one version installed with pyenv, give pyenv uninstall the full version number. -f removes it right away without asking for confirmation.
pyenv uninstall -f 3.13.16pyenv: 3.13.16 uninstalledpyenv versions system
* 3.15.0 (set by /home/devuser/.pyenv/version)Remove pyenv and all Python versions
pyenv and all Python versions installed with it are inside the ~/.pyenv folder. First, check the three lines you added to ~/.bashrc in step 4.
tail -n 3 ~/.bashrcexport PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"Delete the ~/.pyenv folder and the practice folders, then remove the three lines from ~/.bashrc. sed -i edits the file in place, and /PYENV_ROOT/d and /pyenv init/d delete the lines that contain that text. Neither command produces output.
rm -rf ~/.pyenv ~/hello-python ~/old-project
sed -i '/PYENV_ROOT/d; /pyenv init/d' ~/.bashrcClose the terminal, open a new one, and check. If pyenv cannot be found and python3 is back to the system Python, everything has been removed. The build packages installed in step 3 are also used to build other programs, so they were left in place.
pyenv --versionCommand 'pyenv' not found, did you mean:
command 'p7env' from deb libnss3-tools (2:3.98-1ubuntu0.2)
Try: sudo apt install <deb name>python3 --versionPython 3.12.3Try it yourself
No server yet? You can spin up an Ubuntu server in a few minutes with one of these providers. We verified this guide on DigitalOcean.
Was this guide helpful? Let us know if anything is wrong or out of date.
Request a correction