Updating Python on Ubuntu system
I want to update the Python build on my Linux box, but the only way I know how to do it is uninstalling the current version and installing the new one. My system is already up to date (I updated yesterday). I wanted to know if there is a way to update a specific program from the command line, like sudo apt-get update <program-name>. I know this command doesn't exist, but I'm hoping something equivalent does.
10 Answers
As others already noted, bare sudo apt-get install package will install latest available version, replacing the older one if needed.
But with some software (among which is Python) the situation is somewhat different. Some major, very- and incompatibly-different versions get their own packages. For instance, Python 2.6, Python 2.7, Python 3.1 all live in separate packages on Ubuntu.
Of particular importance is the fact that one of Ubuntu policies is to extensively use Python for writing end-user software. So in fact, fairly large part of the system is written in Python. At the moment, the code runs on Python 2.6 — so this version is the default upon installation; and the code won't easily run on, say, Python 2.7 — because incompatibilities exist. To switch the system to Python 2.7 there needs to be done a piece of work, consisting of updating and re-testing all the scripts. This can't be done easily; that is, you can't just "switch" your system to Python 2.7 and delete the older version.
But. If you don't care about fancy gears of your system and just need newer Python — see no obstacles. Go and sudo apt-get install python3 and code for 3.x Python bravely; just remember to launch your scripts with python3 and use #!/usr/bin/env python3 shebang line.
Upd: I keep seeing this upvoted; notice that this is a 9-year old answer, things have changed.
What to learn next
From a superuser perspective (not Python developer's), the next things I'd suggest learning to use:
pip/pip3/python3 -m pip— this is thenpmfor Python. Quick tip: trypip3 install --user howdoi(may need toapt install python3-setuptools python3-piponce, before that works). Then for example,howdoi --all compile python3 ubuntu.The
virtualenvtool. It's 100% developer-oriented, but you'll likely need to use it (perhaps underneath a few wrappers, such astox) to work with people's source packages.
Ruby'sbundleror Cabal sandbox may be familiar analogues.The
condatool — which is a totally separate python package repository and installer (think: fork of PyPi).
There's humongous variety of tools in the Python ecosystem in 2020. At the very least, make yourself comfortable with pip before going deeper.
Basic pitfalls
For the brave but unwary, a few classic pitfalls when trying to manually set up a newer CPython on Ubuntu.
Leave
/usralone; you can look but you don't touch. Leave it todpkg, save yourself some confusion. You have the whole/usr/localat your disposal:sudo chown -R `whoami` /usr/local pip3 install --prefix=/usr/local pydfCompiling CPython from source is well-explained on the web; just don't forget your
/usr/localprefix. This is the best way to manually test patches and/or pre-releases (those alpha-, rc- builds) of CPython itself. To wipe built artifacts, you can justrm -rf /usr/local/*; sudo ldconfig.Finding a PPA is decent option too; keep in mind that a PPA is just someone else's private build. Look for credible PPAs with CI/CD running.
sudo apt-get install python 3.3.3this is for python(3.3.3) for different version the corresponding version number should be used.
4sudo apt-get install python3.6This installs python 3 in linux along side python 2.To access python 3 enter after you opened the terminal.
python3 6 You're close with thinking of a command like sudo apt-get update (which is an actual command, but doesn't do what you want it to.)
To upgrade Python, and everything else you have installed, just do the command:
sudo apt-get upgrade 3 From our sister site:
Each of these commands updates-by-installing, which should work just fine in many cases, but might not be what you're looking for in a specific case.
If you only want to upgrade a specific package AND only if it is already installed, then use the command:
sudo apt-get install --only-upgrade packagename In case you are looking to upgrade only minor version of python package like 2.7.11+ t0 2.7.12 then do sudo apt-get install python2.7 should do for you
pyenv may be a good option for you:
In regards to @ulidtko's answer, a possible alternative to replacing the system python altogether may be to use an isolated environment with your desired python version. This is similar to a "virtual environment", but for python itself. I've used this in the past for legacy projects that run on python 2.6, as well as when upgrading from legacy version of python to python3.
2apt-get install pythonI believe this should work. You will need to change 'python' to match the appropriate package name in your repository obviously.
As we all know that Python comes with 3.5 version. So it is very simple to update Python 2.7(which is by default present into Ubuntu 16.04) to Python 3.5.
Follow the following Steps:
- Open terminal.
- Become root user by using sudo command.
Use the code below to update Python 2.7 to 3.5
sudo apt-get install python3.5
You will be asked for your permission, then Give option Y.
- Wait till complete the process.
- clear the screen by using command
clear Check python version by below command.
python -version
In the single case that you are running a LTS version, your python might be behind by a minor version, say 2.7.5 instead of 2.7.10.
One possibility would be to upgrade the system:
- set
prompt=normalin/etc/update-manager/release-upgrades - upgrade the system
- on the command line, you can type
sudo do-release-upgradeto make the upgrade manager do its job to upgrade to the latest (=non-LTS) version. - the GUI solution uses the Update Manager
- on the command line, you can type
As always when upgrading, have a look at the release notes, as the upgrade might break your system in a few cases, see @Gino's comment.
4