Installing python from download in Ubuntu 19.10 - python

My default version of python3 is 3.7.5. I need to install 3.6.5. I was told that ubuntu 19.10 depends on 3.7.5. So I cannot replace it. I need them to be side by side. I have ran these lines on the command prompt:
wget -P ~/Downloads https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
cd ~/Downloads
tar -xJf Python-3.6.5.tar.xz
cd Python-3.6.5
./configure
make
make test
After the "make test" command is entered, some tests failed. And it has been running for hours.
The final line is either:
sudo make install
or
sudo make altinstall
I was told that the first option will replace 3.7.5, while the second will install them side by side.
Questions:
Since some tests failed, can I still install 3.6.5? What does it mean that the tests are failing.
is "sudo make altinstall" the right command to install them side by side? If so, what is the command to run 3.6.5, if 3.7.5 will still be the default?

How about using version manager?
Example.
pyenv
anaconda
virtualenv

It depends on what tests are failing. It could have been some packages which are currently not required/not being used. So yes, you can continue with the install and check.
Yes it is correct. The corresponding versions can be invoked by mentioning the absolute path until the python folder.
For example /usr/bin/python3.6.5 or /usr/bin/python3.7.5

Related

WSL2 - How do I uninstall other, accidentally installed versions of python(3)?

I installed a bunch of python versions from a bunch of different sources while trying to upgrade from python3.8 to python3.11 on WSL2 and now I have no idea:
which ones of the installed versions I can delete (since they were wrongly installed and are not used)
how I can delete them
which one is the one I am using now as python3.11 (since I would want to keep just that one)
and which one is the system default python that should never be deleted.
In the following I'm first going to explain how I got to the point that I am at now and then explain the specific details of the problem that I'm facing trying to delete the correct versions (as in the versions that I dont need anymore) in the TLDR section.
I was trying to upgrade my version of python3 in WSL2 from 3.8 to 3.11. I tried a few guides to install python3.11.
First I followed this one which installs python3.11 using sudo add-apt-repository ppa:deadsnakes/ppa , sudo apt install python3.11 and sudo apt update.
It then proceeds to set the default python version using sudo apt update-alternatives and a few other commands. This didn't work for me however, since WSL2 said that there were no update-alternatives.
When I executed python3 --version it still said that I have python3.8.
Therefore I then proceeded to follow this guide which uses:
$ tar xvf Python-3.11.tgz
$ cd Python-3.11
$ ./configure --enable-optimizations --with-ensurepip=install
$ make -j 8
$ sudo make altinstall
After installation however python3 --version still showed python3.8. Then I found this guide to change my system to use python3.11 using symbolic links. Now it works and python3 --version shows python3.11 and my system uses it.
TLDR: Now on to my problem of not knowing which versions to delete and how to delete them properly / thorougly / without breaking anything:
which python3 shows /usr/bin/python3 and which python3.11 shows /usr/local/bin/python3.11. My /usr/bin/ and my /bin/ folder folder look like this now and my /usr/local/bin/ folder looks like this.
I honestly am so confused even, as to what folder is what, as navigating like this:
$ cd ~
$ cd /
$ cd home/myusername/
leads me back to being at ~ and inside the / folder there is the bin/ folder containing all this stuff but then inside /usr/local/bin/ there is all this stuff again. Then in /home/myusername/ there is nothing even though there should be a bin folder too according to most websites. Instead it leads me back to ~.
So any help would be much appreciated in figuring out which of those python versions I can delete now, how I do this and what all the confusion with the directories means.
Thanks in advance!

How do I replace a Python installed from source with a packaged version?

As Python 3.7 was being prepared I installed Python 3.7.0b3 from source. Now 3.7 is out and I want to use the version packaged for Ubuntu. So I've done
apt-get install python3.7
but
$ which python3.7
/usr/local/bin/python3.7
$ /usr/local/bin/python3.7 --version
Python 3.7.0b3
How does one uninstall a Python installed from source (on Ubuntu)?
Or how can I replace it with the apt repo packaged version?
Since you opened a bounty, I can't vote to close as a duplicate, but this question would seem to provide a possible solution. Quoting from the accepted answer:
You can use checkinstall to remove Python. The idea is:
Install checkinstall
Use
checkinstall to make a deb of your
Python installation
Use dpkg -r to
remove the deb.
checkinstall basically wraps a make install command and creates a Debian .deb package based on what was installed. Then, you can just uninstall that package to reverse make install completely. To be perfectly safe you may want to uninstall the packaged Python 3.7 first and reinstall it afterwards to avoid any conflicts (I wouldn't expect any, though, since the packaged version lives in /usr while your source version lives in /usr/local).
If you don't have your source files around anymore, you can always download them again (https://www.python.org/downloads/release/python-370b3/) and rebuild Python. Specifically, the checkinstall commands would look something like this:
sudo apt install checkinstall
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0b3.tgz
tar xf Python-3.7.0b3.tgz
cd Python-3.7.0b3
./configure && make
sudo checkinstall -D --fstrans=no make install
sudo dpkg -r Python-3.7.0b3.deb
(-D creates a Debian package, --fstrans=no disables use of a temporary directory for installation).
Since I was also moving to python 3.7, I came across this question and decided to answer it, as well as finish my installation.
This are the two sources, which I used for installing python 3.7 on ubuntu 16.04:
https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get
https://superuser.com/questions/241865/updating-python-on-ubuntu-system
Apparently from the first source the deadsnakes PPA contain Python 3.7 - Link: https://github.com/deadsnakes/python3.7/tree/ubuntu/xenial/Python
So following from my first source, I used the following commands to install Python 3.7:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.7
It installed Python 3.7 successfully but my Python 3.5.2 remains default. You can invoke/use Python 3.7 by running the commands:
python3.7 script.py
It works, so I decided to set Python 3.7 as default by removing python 3.5, but I came across my second source, and just decided to stick with using python3.7, when running my script in the terminal.
Apparently it says:
Ubuntu policies extensively for writing end-user software. So basically, a large part of the system is written in Python. To switch to Python 3.7, there needs to be done a piece of work consisting of updating and re-testing all the scripts.
So to say you can't just switch to Python 3.7 and delete the older version.
Also from a comment from my first source it states deleting the older version might break the system. I haven't been around to deleting my older version in case it might break the system, but since you are asking for how to download Python 3.7, I think my first source and the first part of my answer should to the work.
I hope it helps :)
It seems that your Python built from source is under /usr/local, and your PATH variable has /usr/local/bin before /usr/bin, since running python3.7 gets you the one under /usr/local rather than the packaged one which would be /usr/bin/python3.7.
Look at your PATH to verify this.
echo $PATH
(When you run a program in bash, that particular running bash instance will remember the location and not rescan the directories in the PATH for that program again, so it will not notice a new file that has appeared somewhere earlier in the PATH. You can prevent this by running hash -r to reset the cache or by just exiting the shell and launching it again.)
I presume your goal is for python3.7 (or any of the other commands provided by Python) to run the versions from your packaged install in /usr.
Unfortunately the python build process does not provide an uninstall method -- the only automated way to remove just the files installed by a source Python install requires using other tools ahead of time (such as checkinstall).
So you have some choices :
Change your PATH so that /usr/local/bin is after /usr/bin. To do this, edit your ~/.profile file or whatever other script you have configuring your PATH and logout/login. This will also affect any other commands you run that are available in both /usr/local/bin and /usr/bin.
Remove /usr/local and reinstall anything else you want there. If a Python install is the only thing in your /usr/local, or if you can easily reinstall anything else you had there, this might be the way to go.
Painstakingly figure out what files under /usr/local/bin were part of Python and remove them. You might be able use the corresponding files in /usr/bin from your installed python3 packages as a starting point to figure out the similar names for /usr/local/bin.
One-liner to get the list of files in /usr/bin from installed python3* packages:
$ for pkg in $(dpkg -l 'python3*' | grep '^ii' | cut -f 3 -d' '); do dpkg -L $pkg | grep '^/usr/bin/'; done | sort
This should produce a list of files like:
/usr/bin/2to3-3.x
/usr/bin/chardet3
...
(I've tested this one-liner on Debian, I'm not sure if any changes are required for Ubuntu)

python update to 2.7.13 on centos

I'm new for linux and python ,yesterday I update my python from 2.6.6 to 2.7.13,but when i finished these command "./configure 、make、make install " ,then run "python“ ,I got "command not found ". thanks for help
I completely agree with the answer provided by #holdenweb. Just to add some more details considering for a tyro:
If you need access to a newer version of Python you must compile it yourself and install it side-by-side with the system version.
Here are the steps necessary to install Python 2.7. Execute all the commands below as root. Either log in as root temporarily or use sudo.
Install development tools
In order to compile Python you must first install the development tools:
The first one is :
yum groupinstall "Development tools"
You also need a few extra libraries installed before compiling Python or else you will run into problems later when trying to install various packages:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
Download, compile and install Python
The --no-check-certificate is optional
cd /opt
wget --no-check-certificate https://www.python.org/ftp/python/2.7.6/Python-
2.7.6.tar.xz
tar xf Python-2.7.6.tar.xz
cd Python-2.7.6
./configure --prefix=/usr/local
make && make altinstall
It is important to use altinstall instead of install, otherwise you will end up with two different versions of Python in the filesystem both named python.
After running the commands above your newly installed Python 2.7 interpreter will be available as /usr/local/bin/python2.7 and the system version of Python 2.6.6 will be available as /usr/bin/python and /usr/bin/python2.6.6.
Now Python is an essential component of many operating systems, the safest rule is leave the system's Python to the system, and don't modify it yourself. That way you can be sure you aren't going to interfere with any OS code that depends on a specific version of Python.
If you want some other version of Python to be your own default, install it somewhere like /usr/local/bin (the default installation for most Linux systems will put it there by default) and then put that directory at the front of your shell's PATH to ensure that the python command gets your version instead of the system's (which will remain as /usr/bin/python).
I would recommend that you re-link /usr/bin/python to point to /usr/bin/python2.6 and then the PATH adjustment mentioned above (but in your case adding /usr/local/python2.7.13/bin, which is where you seem to have installed your updated Python) should be all you need to do.

How to install Pip on compiled from source Python

I'm on a raspberry pi running the latest Debian. It comes with 2.7.3 by default but I bought it to run a large Flask home automation app which was all written on 2.7.9 and I want no problems later on ( I know it is a minor version but I am a perfectionist).
So I downloaded the latest Python 2.7.10. And did
./configure && make && make altinstall
So far so good. It has installed the interpreter under /usr/local/bin as I expected. Now I need to to be able to run pip install -r requirements.txt, then I need pip. So I downloaded the get-pip.py from the Python website and tried
/usr/local/bin/Python2.7 get-pip.py
With no luck. I have also tried to create a virtualenv with a different my compiled Python interpreter like
virtualenv -p /usr/local/Python2.7 venv
No luck either. I was able to install easy_install on my compiled Python but it also throws me an error when I try
/usr/local/bin/easy_install pip
What am I doing wrong? I have read about --ensurepip flag for configuring Python at the first place, but Do I need to remove Python and install it all over again just to have pip on my compiled interpreter?
I was facing the same issue. I resolved it by appending an extra parameter to the configure command --with-ensurepip=install followed by make and make install. Then, my installation folder for python has pip there in.

Unable to install Python without sudo access

I extracted, configured and used make for the installation package in my server.
However, I could not use make install. I get the error
[~/wepapps/python/Python-2.6.1]# make install
/usr/bin/install -c python /usr/local/bin/python2.6
/usr/bin/install: cannot create regular file `/usr/local/bin/python2.6': Permission denied
make: *** [altbininstall] Error 1
I run the folder with
chmod +x Python-2.6.1
I get still the same error.
How can I run make install without sudo access?
How can I install to a path under my home directory?
mkdir /home/masi/.local
cd Python-2.6.1
make clean
./configure --prefix=/home/masi/.local
make
make install
Then run using:
/home/masi/.local/bin/python
Similarly if you have scripts (eg. CGI) that require your own user version of Python you have to tell them explicitly:
#!/home/masi/.local/bin/python
instead of using the default system Python which “#!/usr/bin/env python” will choose.
You can alter your PATH setting to make just typing “python” from the console run that version, but it won't help for web apps being run under a different user.
If you compile something that links to Python (eg. mod_wsgi) you have to tell it where to find your Python or it will use the system one instead. This is often done something like:
./configure --prefix=/home/masi/.local --with-python=/home/masi/.local
For other setup.py-based extensions like MySQLdb you simply have to run the setup.py script with the correct version of Python:
/home/masi/.local/bin/python setup.py install
As of year 2020, pyenv is the best choice for installing Python without sudo permission, supposing the system has necessary build dependencies.
# Install pyenv
$ curl https://pyenv.run | bash
# Follow the instruction to modify ~/.bashrc
# Install the latest Python from source code
$ pyenv install 3.8.3
# Check installed Python versions
$ pyenv versions
# Switch Python version
$ pyenv global 3.8.3
# Check where Python is actually installed
$ pyenv prefix
/home/admin/.pyenv/versions/3.8.3
# Check the current Python version
$ python -V
Python 3.8.3
Extending bobince answer, there is an issue if you don't have the readline development package installed in your system, and you don't have root access.
When Python is compiled without readline, your arrow keys won't work in the interpreter. However, you can install the readline standalone package as follows: Adding Readline Functionality Without Recompiling Python
On the other hand, if you prefer to compile python using a local installation of readline, here's how.
Before doing as bobince was telling, compile and install readline. These are the steps to do so:
wget ftp://ftp.cwru.edu/pub/bash/readline-6.2.tar.gz
tar -zxvf readline-6.2.tar.gz
cd readline-6.2
./configure --with-prefix=$HOME/.local
make
make install
Then, add this line to your .bash_profile script:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/.local/lib
Last, but not least, execute the following command
export LDFLAGS="-L$HOME/.local"
I hope this helps someone!
You can't; not to /usr, anyway. Only superusers can write to those directories. Try installing Python to a path under your home directory instead.

Categories